FTPrep, 28 Implement strStr()
2017-08-07 14:51
253 查看
思路挺简单的,做了一些edge case的判断,其实都可以包括在代码里,比如
if(shortLen==0 && longLen==0) return 0;
if(shortLen==0 || shortLen>longLen) return -1;
public class Solution {
public int strStr(String haystack, String needle) {
int shortLen=needle.length();
int longLen=haystack.length();
for(int head=0; head<longLen-shortLen+1; ++head){
if(haystack.substring(head, head+shortLen).equals(needle)) // 对于空串"", substring(0,0) 还是空串
return head;
}
return -1;
}
}
// 这题就是要记住substring(start, end) 前开后闭的方式就好。
if(shortLen==0 && longLen==0) return 0;
if(shortLen==0 || shortLen>longLen) return -1;
public class Solution {
public int strStr(String haystack, String needle) {
int shortLen=needle.length();
int longLen=haystack.length();
for(int head=0; head<longLen-shortLen+1; ++head){
if(haystack.substring(head, head+shortLen).equals(needle)) // 对于空串"", substring(0,0) 还是空串
return head;
}
return -1;
}
}
// 这题就是要记住substring(start, end) 前开后闭的方式就好。
相关文章推荐
- LeetCode 28 : Implement strStr() (Java)
- LeetCode-28-Implement strStr()(Sting/穷举)-Easy
- 28. Implement strStr()
- [Leetcode]#28 Implement strStr()
- LeetCode 28 Implement strStr()
- LeetCode(28)--Implement strStr()
- 【LeetCode OJ 28】Implement strStr()
- 28 Implement strStr()
- [LeetCode28] Implement strStr()
- Lettcode_28_Implement strStr()——字符串中第一次出现给定字符串的位置
- Leetcode#28 Implement strStr()
- [Leetcode] #28 Implement strStr()
- [LeedCode OJ]#28 Implement strStr()
- Leetcode-28: Implement strStr()
- 28. Implement strStr()
- 【28】Implement strStr()
- [LeetCode]28 Implement strStr()
- leetcode 28:Implement strStr()
- Leetcode 28 Implement strStr()
- 【LeetCode28】【 Implement strStr()】length,length(),size()