首页 > 代码库 > [LeetCode]28 Implement strStr()
[LeetCode]28 Implement strStr()
https://oj.leetcode.com/problems/implement-strstr/
http://fisherlei.blogspot.com/2012/12/leetcode-implement-strstr.html
public class Solution { public int strStr(String haystack, String needle) { // 遍历haystack,对每一个字符,匹配needle if (haystack == null || needle == null) return -1; char[] h = haystack.toCharArray(); char[] n = needle.toCharArray(); for (int i = 0 ; i < h.length - n.length + 1 ; i ++) { if (match(h, i, n)) return i; } return -1; } private boolean match(char[] h, int start, char[] n) { for (int i = 0 ; i < n.length ; i ++) { if ((start + i >= h.length) || (h[start + i] != n[i])) return false; } return true; } // Check KMP }
[LeetCode]28 Implement strStr()
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。