File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
1
+ package leetcode ._28_ ;
2
+
3
+ class Solution {
4
+ public int strStr (String haystack , String needle ) {
5
+ return haystack .indexOf (needle );
6
+ }
7
+ }
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments