28. Implement strStr()

public class Solution {
    public int strStr(String haystack, String needle) {
        if(haystack.equals("")&&needle.equals(""))
            return 0;
        int size1=haystack.length();
        int size2=needle.length();
        int res=-1;
        for(int i=0;i<=size1-size2;i++)
        {
            int head=i;
            int i1=i;
            int i2=0;
            while(i1<size1&&i2<size2)
            {
                if(haystack.charAt(i1)==needle.charAt(i2))
                {
                    i1++;
                    i2++;
                }
                else
                    break;
            }
            if(i2==size2)
            {
                res=head;
                break;
            }
        }
        return res;
    }
}

 

posted @ 2016-08-10 21:01  阿怪123  阅读(105)  评论(0编辑  收藏  举报