File tree Expand file tree Collapse file tree 3 files changed +56
-0
lines changed
src/com/andavid/leetcode/_028 Expand file tree Collapse file tree 3 files changed +56
-0
lines changed Original file line number Diff line number Diff line change 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** 其他**
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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments