首页 > 代码库 > Implement strStr()

Implement strStr()

https://leetcode.com/submissions/detail/108134096/

brute force

 public int strStr(String haystack, String needle) {
        if (haystack.length() < needle.length()) {
            return -1;
        }
        int start = 0;
        while (start <= haystack.length() - needle.length()) {
            int i = start, j = 0;
            while (j < needle.length() && haystack.charAt(i) == needle.charAt(j)) { // 判断
               
                    i++;
                    j++;
                
            }
            if (j == needle.length()) {
                return start;
            }
            start++;
        }
        return -1;
    }

  

Implement strStr()