码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode028. Implement strStr()

时间:2016-09-17 09:27:17      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

class Solution {
public:
    int strStr(string haystack, string needle) {
        if(needle.empty())return 0;    //needle empty 
        if(haystack.empty()) return -1;    //haystack empty
        for(int i = 0, j = 0; i+j < haystack.size();) {
            if(haystack[i+j] != needle[j])i++, j = 0;     //no equal   needle index to 0, haystack index move to next.
            else j++;   //equal both move to next
            if(j == needle.size())return i;     //thr first time find substr.
        }
        return -1;
    }
};

 

Leetcode028. Implement strStr()

标签:

原文地址:http://www.cnblogs.com/zeroArn/p/5877907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!