【LeetCode】28

Implement strStr().

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

Solution: 

 1 class Solution {
 2 public:
 3     int strStr(string haystack, string needle) {    //runtime:4ms
 4         int len1=haystack.size(), len2=needle.size();
 5         for(int i=0;i<=len1-len2;i++)
 6         {
 7             int j=0;
 8             for(;j<len2;j++){
 9                 if(haystack[i+j]!=needle[j])break;
10             }
11             if(j==len2)return i;
12         }
13         return -1;
14     }
15 };
原文地址:https://www.cnblogs.com/irun/p/4725842.html