Implement strStr()

来源:互联网 发布:linux 关闭oracle进程 编辑:程序博客网 时间:2024/06/04 19:53
坚刚不可夺其志的主子
public int strStr(String haystack, String needle) {        if (haystack == null || needle == null || haystack.length() < needle.length()) {            return -1;        }        //for (int i = 0; i < haystack.length(); i++) {        //for (int i = 0; i < haystack.length() - needle.length(); i++) {        for (int i = 0; i <= haystack.length() - needle.length(); i++) {            int j = 0;            for (; j < needle.length(); j++) {                if (haystack.charAt(i + j) != needle.charAt(j)) {                    break;                }            }            if (j == needle.length()) {                return i;            }        }        return -1;    }


0 0
原创粉丝点击