leetcode-28

 1 class Solution {
 2 public:
 3     int strStr(string haystack, string needle) {
 4         if(haystack==""&&needle!="")return -1;
 5         if(haystack==""||needle=="")return 0;
 6         int m = haystack.size(),n=needle.size();
 7         
 8         for(int i=0;i<m-n+1;i++)
 9          {  int j=0;
10           while(haystack[i+j]==needle[j]){
11               ++j;
12               if(j==n)
13                   return i;
14           }
15     }
16         return -1;}
17 };

思路就是遍历haystack,然后也对needle进行遍历。然后用了8ms,看了一下大佬的题解,自己对c

++的函数没有熟悉啊。

1 class Solution {
2 public:
3     int strStr(string haystack, string needle) {
4      return haystack.find(needle);
5 }
6     
7 };

 然后还有一个kmp算法,但是暂时没有去了解过,改日再去学习!

posted @ 2018-12-19 01:15  keep!  阅读(115)  评论(0编辑  收藏  举报
Live2D