class Solution {
public:
int strStr(string haystack, string needle) {
int m=haystack.length();
int n=needle.length();
if(!n) return 0;
for (int i=0;i<m-n+1;i++)
{
int j=0;
for(;j<n;j++)
if(haystack[i+j]!=needle[j]) //if i change the scope ,the result may be different
//{if(haystack[i+j]!=needle[j])
// break; // if(j==n) // return i; break; if(j==n) return i; }return -1; }};
class Solution {
public:
int strStr(string haystack, string needle) {
int m=haystack.length();
int n=needle.length();
if(!n) return 0;
for (int i=0;i<m-n+1;i++)
{ int j=0;
for(;j<n;j++)
{if(haystack[i+j]!=needle[j])
break;}
if(j==n)
return i;
}return -1;
}
};
看着下面 我误解了作用域。。。。因为如果包在for 语句的执行体里面,那么j就永远不可能等于string的长度了。