Implement strStr()

Implement strStr()

问题:

Implement strStr().

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

思路:

  string的简易操作

我的代码:

public class Solution {
    public int strStr(String haystack, String needle) {
        if(haystack == null || needle == null)  return -1;
        for(int i = 0; i <= haystack.length() - needle.length(); i++)
        {
            int j = 0;
            for(; j < needle.length(); j++)
            {
                if(needle.charAt(j) != haystack.charAt(i+j))
                    break;
            }
            if(j == needle.length())    return i;
        }
        return -1;
    }
}
View Code
原文地址:https://www.cnblogs.com/sunshisonghit/p/4375355.html