Skip to content

Commit 17fc860

Browse files
committed
add 028
1 parent 3329835 commit 17fc860

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
| 242 | [Valid Anagram][242] |
3838
| 125 | [Valid Palindrome][125] |
3939
| 8 | [String to Integer (atoi)][008] |
40+
| 28 | [Implement strStr()][028] |
4041

4142

4243
**其他**
@@ -69,3 +70,4 @@
6970
[242]: https://github.com/andavid/leetcode-java/blob/master/note/242/README.md
7071
[125]: https://github.com/andavid/leetcode-java/blob/master/note/125/README.md
7172
[008]: https://github.com/andavid/leetcode-java/blob/master/note/008/README.md
73+
[028]: https://github.com/andavid/leetcode-java/blob/master/note/028/README.md

note/028/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [Implement strStr()][title]
2+
3+
## Description
4+
5+
Implement strStr().
6+
7+
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
8+
9+
**Example 1:**
10+
11+
```
12+
Input: haystack = "hello", needle = "ll"
13+
Output: 2
14+
```
15+
16+
**Example 2:**
17+
18+
```
19+
Input: haystack = "aaaaa", needle = "bba"
20+
Output: -1
21+
```
22+
23+
## 思路
24+
25+
太简单了吧,indexOf 搞定。
26+
27+
## [完整代码][src]
28+
29+
```java
30+
class Solution {
31+
public int strStr(String haystack, String needle) {
32+
if (null == haystack || null == needle) {
33+
return -1;
34+
}
35+
return haystack.indexOf(needle);
36+
}
37+
}
38+
```
39+
40+
[title]: https://leetcode.com/problems/implement-strstr
41+
[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_028/Solution.java
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int strStr(String haystack, String needle) {
3+
if (null == haystack || null == needle) {
4+
return -1;
5+
}
6+
return haystack.indexOf(needle);
7+
}
8+
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
System.out.println(solution.strStr("hello", "ll"));
12+
}
13+
}

0 commit comments

Comments
 (0)