Implement strStr()

来源:互联网 发布:全国网络教育统考网 编辑:程序博客网 时间:2024/06/05 11:04
public class Solution {    public int strStr(String haystack, String needle) {        if(0 == haystack.length() && 0 == needle.length()) return 0;        else if(haystack.length() < needle.length()) return -1;        else if(0 == needle.length()) return 0;        for(int i = 0; i < haystack.length(); ++i) {            if(haystack.charAt(i) != needle.charAt(0)) continue;            else if(haystack.length() - i >= needle.length()) {                for(int j = 0, k = i; j < needle.length(); ++j, ++k) {                    if(haystack.charAt(k) != needle.charAt(j)) break;                    if(j ==needle.length() - 1) return i;                }                               }else                return -1;        }        return -1;    }}
0 0