给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。
input:haystack = "hello", needle = "ll" output:2input:haystack = "", needle = "" output:0
class Solution {
public int strStr(String haystack, String needle) {
int hLen = haystack.length(),nLen = needle.length();
if(nLen == 0) return 0;
for(int i = 0;(i + nLen) <= hLen;i ++){
boolean flag = true;
for(int j = 0;j < nLen;j ++){
if(haystack.charAt(i+j) != needle.charAt(j)){
flag = false;
break;
}
}
if(flag) return i;
}
return -1;
}
}