leetcode 28. Implement strStr()

题目:

 

  Implement strStr().

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

 

  

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

 

posted @ 2016-10-19 17:12  prog123  阅读(171)  评论(0编辑  收藏  举报