[LeetCode] Implement strStr()

来源:互联网 发布:4g网络摄像头哪家好 编辑:程序博客网 时间:2024/05/29 10:06

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

class Solution {public:    int strStr(char *haystack, char *needle) {        int i,j;        for(i = 0,j = 0;haystack[i] && needle[j];){            if(haystack[i] == needle[j]){                i ++;                j ++;            }else{                i = i - j + 1;                j = 0;            }        }        return needle[j] == '\0' ? i - j : -1;    }};


0 0
原创粉丝点击