LeetCode 125 Implement strStr()
2014-10-31 20:17
477 查看
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
分析:
从0开始每次截取needle长度的字串来匹配,如果相等则找到。
public class Solution {
public String strStr(String haystack, String needle) {
int len = needle.length();
for(int i=0; i<=haystack.length()-len; i++){
String sub = haystack.substring(i,i+len);
if(sub.equals(needle)){
return haystack.substring(i);
}
}
return null;
}
}
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
分析:
从0开始每次截取needle长度的字串来匹配,如果相等则找到。
public class Solution {
public String strStr(String haystack, String needle) {
int len = needle.length();
for(int i=0; i<=haystack.length()-len; i++){
String sub = haystack.substring(i,i+len);
if(sub.equals(needle)){
return haystack.substring(i);
}
}
return null;
}
}
相关文章推荐
- [LeetCode] Implement strStr()
- [LeetCode] 28 - Implement strStr()
- (leetcode) Implement strStr()
- leetcode——28—— Implement strStr()
- LeetCode之路——LeetCode_28_Implement strStr()
- LeetCode Implement strStr()
- 【LEETCODE】28-Implement strStr()
- 【leetcode刷题笔记】Implement strStr()
- LeetCode Implement strStr()
- 【Leetcode】Implement strStr()
- LeetCode--Implement strStr() Python
- LeetCode-28-Implement strStr()(Sting/穷举)-Easy
- leetcode(57)- Implement strStr()
- 【leetcode】 Implement strStr()
- LeetCode - Implement strStr()
- Implement strStr() -- leetcode
- leetcode (28) - Implement strStr()
- LeetCode : Implement strStr() [java]
- leetcode--Implement strStr()
- leetcode-28 Implement strStr()