leetcode--28. Implement strStr()

来源:互联网 发布:在这网络里认识你 编辑:程序博客网 时间:2024/06/04 19:34

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(string haystack, string needle) {        for(int i = 0; ; ++i){            for(int j = 0; ; ++j){                if(j == needle.length()) return i;                if(i + j == haystack.length()) return -1;                if(needle[j] != haystack[i + j]) break;            }        }    }};
0 0
原创粉丝点击