题目内容:
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 static int strStr(String haystack, String needle) {
for(int i=0;;i++){
for(int j=0;;j++){
if(j==needle.length())return i;
if(i+j==haystack.length())return -1;
if(needle.charAt(j)!=haystack.charAt(i+j)) break;
}
}
}
}