LeetCode 28. 实现 strStr()

class Solution {
    public int strStr(String haystack, String needle) {
        if(needle.length()==0)
            return 0;
        int i=0;
        for(;i<haystack.length()-needle.length()+1;i++)
        {
            if(haystack.charAt(i)==needle.charAt(0))
            {
                int j=0;
                while(j<needle.length()&&haystack.charAt(i+j)==needle.charAt(j))
                {
                    j++;
                }
                if(j==needle.length())
                    return i;
            }
        }
        return -1;
    }
}

你可能感兴趣的:(LeetCode)