//有个神奇的东西
class Solution { public: int strStr(string haystack, string needle) { if(needle == "")return 0; int lenn = haystack.size()-needle.size(); //如果不用这个直接把haystack.size()-needle.size()用进去的话,会出错!!应该算是个bug吧 if(lenn < 0)return -1; for(int i=0;i <= lenn;i++){ bool flag = true; if(haystack[i] == needle[0]){ int temp = i; for(int j=0;j < needle.size();j++,temp++){ if(haystack[temp] != needle[j]){flag = false;break;} } if(flag == true) return i; } } return -1; } };