[LeetCode] Implement strStr()
文章目录
Problem
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Note
有substring,为何不用。
Solution
public class Solution {
public int strStr(String h, String n) {
int len = n.length();
for (int i = 0; i <= h.length()-len; i++) {
if (h.substring(i, i+len).equals(n)) return i;
}
return -1;
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论