Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
题目:这题目是要找出第一个相同串的位置。
思路:相当简单,遍历字符串,根据第二个串的长度找相同长的串比较就行,找到第一个即结束遍历。
public int strStr(String haystack, String needle) {
if(haystack.equals("") && needle.equals("")) {
return 0;
}
int count = -1;
for(int i = 0;i <= haystack.length() - needle.length();i++) {
if(needle.equals(haystack.substring(i, i+needle.length()))){
count = i;
break;
}
}
return count;
}