LeetCode----28. Implement strStr()
2016-09-26 14:03
357 查看
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
public class Solution {
public int strStr(String haystack, String needle) {
if(haystack==null||needle==null||needle.length()<0||haystack.length()<0||needle.length()>haystack.length()){return -1;}
for(int i=0;i<haystack.length()-needle.length()+1;i++){
int j=0;
for(j=0;j<needle.length();j++){
if(haystack.charAt(i+j)!=needle.charAt(j)){
break;
}
}
if(j==needle.length()){return i;}
}
return -1;
}
}
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
public class Solution {
public int strStr(String haystack, String needle) {
if(haystack==null||needle==null||needle.length()<0||haystack.length()<0||needle.length()>haystack.length()){return -1;}
for(int i=0;i<haystack.length()-needle.length()+1;i++){
int j=0;
for(j=0;j<needle.length();j++){
if(haystack.charAt(i+j)!=needle.charAt(j)){
break;
}
}
if(j==needle.length()){return i;}
}
return -1;
}
}
相关文章推荐
- Leetcode题解 28. Implement strStr()
- 个人记录-LeetCode 28. Implement strStr()
- (Java)LeetCode-28. Implement strStr()
- leetcode 28. Implement strStr()
- leetcode 28. Implement strStr()
- leetcode28. Implement strStr()
- 【leetcode】28. Implement strStr()
- LeetCode 28. Implement strStr()
- [leetcode]#28. Implement strStr()
- [leetcode]28. Implement strStr()
- [LeetCode] 28. Implement strStr()
- leetcode:28. Implement strStr()
- 【一天一道LeetCode】#28. Implement strStr()
- LeetCode 28. Implement strStr()
- Leetcode-28. Implement strStr()
- Leetcode28. Implement strStr()
- [LeetCode]28. Implement strStr()
- Leetcode 28. Implement strStr()
- leetcode 28. Implement strStr()
- [leetcode] 28. Implement strStr()