LeetCode:strStr 暴力美学
2013-12-30 19:10
267 查看
题目:
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
思路:暴力解决:
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
思路:暴力解决:
public String strStr(String haystack, String needle) { if (needle == null || haystack == null) return null; if (haystack.length() == 0 && needle.length() == 0) return haystack; if (needle.length() == 0) return haystack; for (int i = 0; i < haystack.length() - needle.length()+1; i++) { if (haystack.charAt(i) == needle.charAt(0)) { int x = i + 1; int j = 1; boolean flag = true; while (x < haystack.length() && j < needle.length()) { if (haystack.charAt(x) == needle.charAt(j)) { x++; j++; } else { flag = false; break; } } if (flag) return haystack.substring(i); } } return null; }
相关文章推荐
- LeetCode Implement strStr() 暴力法, KMP法, Boyer-Moore简易版法
- LeetCode 28. Implement strStr暴力搜索和KMP算法
- 28.Implement strStr() leetcode java
- [leetcode]28. Implement strStr()
- (LeetCode)Implement strStr()
- 【LEETCODE】28-Implement strStr()
- LeetCode 028 Implement strStr()
- LeetCode Implement strStr()
- LeetCode -- Implement strStr()
- LeetCode--implement-strstr
- leetcode - Implement strStr()
- [LeetCode] Implement strStr()
- LeetCode (28)Implement strStr()
- LeetCode Implement strStr()
- 【LeetCode】028.Implement strStr()
- [LeetCode] Implement strStr()
- LeetCode 28. Implement strStr()
- LeetCode : Implement strStr() [java]
- #leetcode#Implement strStr()
- 【Leetcode】Implement strStr()