leetcode-28-implement-strstr

Description

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

1
2
Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

1
2
Input: haystack = "aaaaa", needle = "bba"
Output: -1

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C’s strstr() and Java’s indexOf()).

思路

題意是從主串中找到子串的索引,如果找不到則返回-1,當子串長度大於主串,直接返回-1,然後我們只需要遍歷比較即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int strStr(String haystack, String needle) {
int l1 = haystack.length(), l2 = needle.length();
if (l1 < l2) return -1;
for (int i = 0; ; i++) {
if (i + l2 > l1) return -1;
for (int j = 0; ; j++) {
if (j == l2) return i;
if (haystack.charAt(i + j) != needle.charAt(j)) break;
}
}
}
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×