Skip to content

Commit ad7e161

Browse files
committed
28. Implement strStr()
1 parent 1863f5f commit ad7e161

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/leetcode/_28_/Main.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode._28_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println(solution.strStr("hello", "ll"));
10+
}
11+
}
12+

src/leetcode/_28_/Solution.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package leetcode._28_;
2+
3+
class Solution {
4+
public int strStr(String haystack, String needle) {
5+
return haystack.indexOf(needle);
6+
}
7+
}

src/leetcode/_28_/solution.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
### [28\. Implement strStr()Copy for Markdown](https://leetcode.com/problems/implement-strstr/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Implement .
7+
8+
Return the index of the first occurrence of needle in haystack, or **-1** if needle is not part of haystack.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: haystack = "hello", needle = "ll"
14+
Output: 2
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: haystack = "aaaaa", needle = "bba"
21+
Output: -1
22+
```
23+
24+
**Clarification:**
25+
26+
What should we return when `needle` is an empty string? This is a great question to ask during an interview.
27+
28+
For the purpose of this problem, we will return 0 when `needle` is an empty string. This is consistent to C's  and Java's .
29+
30+
31+
#### Solution
32+
33+
Language: **Java**
34+
35+
```java
36+
class Solution {
37+
   public int strStr(String haystack, String needle) {
38+
       return haystack.indexOf(needle);
39+
  }
40+
}
41+
```
42+
![](https://ws1.sinaimg.cn/large/006tKfTcgy1g0zym83rlkj31180n40wa.jpg)

0 commit comments

Comments
 (0)