Skip to content

Commit ac157ad

Browse files
committedOct 13, 2018
Add solution 028 [Java]
1 parent d893c31 commit ac157ad

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
 

‎README.md

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
2626
| 014 | [Longest Common Prefix](https://github.com/doocs/leetcode/tree/master/solution/014.Longest%20Common%20Prefix) | `String` |
2727
| 020 | [Valid Parentheses](https://github.com/doocs/leetcode/tree/master/solution/020.Valid%20Parentheses) | `String`, `Stack` |
2828
| 021 | [Merge Two Sorted Lists](https://github.com/doocs/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists) | `Linked List` |
29+
| 028 | [Implement strStr()](https://github.com/doocs/leetcode/tree/master/solution/028.Implement%20strStr()) | `Two Pointers`, `String` |
2930
| 053 | [Maximum Subarray](https://github.com/doocs/leetcode/tree/master/solution/053.Maximum%20Subarray) | `Array`, `Divide and Conquer`, `Dynamic Programming` |
3031
| 070 | [Climbing Stairs](https://github.com/doocs/leetcode/tree/master/solution/070.Climbing%20Stairs) | `Dynamic Programming` |
3132
| 083 | [Remove Duplicates from Sorted List](https://github.com/doocs/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List) | `Linked List` |
@@ -88,6 +89,8 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http
8889

8990
## Contributors
9091

92+
" If you want to go fast, go alone. If you want to go far, go together. And that's the spirit of [teamwork](https://github.com/doocs/leetcode/graphs/contributors) ".
93+
9194
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
9295
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="80px;"/>](https://github.com/yanglbme)<br />[💻](https://github.com/doocs/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="80px;"/>](https://github.com/chakyam)<br />[💻](https://github.com/doocs/leetcode/commits?author=chakyam "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/10081554?v=4" width="80px;"/>](https://github.com/zhkmxx9302013)<br />[💻](https://github.com/doocs/leetcode/commits?author=zhkmxx9302013 "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/40383345?v=4" width="80px;"/>](https://github.com/MarkKuang1991)<br />[💻](https://github.com/doocs/leetcode/commits?author=MarkKuang1991 "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/12371194?v=4" width="80px;"/>](https://github.com/fonxian)<br />[💻](https://github.com/doocs/leetcode/commits?author=fonxian "Code") </center> |
9396
|---|---|---|--|--|
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## 实现strStr()
2+
### 题目描述
3+
4+
实现 [strStr()](https://baike.baidu.com/item/strstr/811469) 函数。
5+
6+
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
7+
8+
**示例 1:**
9+
```
10+
输入: haystack = "hello", needle = "ll"
11+
输出: 2
12+
```
13+
14+
**示例 2:**
15+
```
16+
输入: haystack = "aaaaa", needle = "bba"
17+
输出: -1
18+
```
19+
20+
**说明:**
21+
22+
`needle` 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
23+
24+
对于本题而言,当 `needle` 是空字符串时我们应当返回 0 。这与C语言的 [strstr()](https://baike.baidu.com/item/strstr/811469) 以及 Java的 `indexOf()` 定义相符。
25+
26+
### 解法
27+
遍历 `haystack``needle`,利用指针 `p`, `q` 分别指向这两个字符串。对于每一个位置对于的字符,如果两字符相等,继续判断下一个位置的字符是否相等;否则 `q` 置为 0,`p` 置为最初匹配的字符的下一个位置,即 `p - q + 1`
28+
29+
```java
30+
class Solution {
31+
public int strStr(String haystack, String needle) {
32+
if ("".equals(needle)) {
33+
return 0;
34+
}
35+
36+
int len1 = haystack.length();
37+
int len2 = needle.length();
38+
int p = 0;
39+
int q = 0;
40+
while (p < len1) {
41+
if (haystack.charAt(p) == needle.charAt(q)) {
42+
if (len2 == 1) {
43+
return p;
44+
}
45+
++p;
46+
++q;
47+
} else {
48+
p -= q - 1;
49+
q = 0;
50+
}
51+
52+
if (q == len2) {
53+
return p - q;
54+
}
55+
}
56+
return -1;
57+
}
58+
}
59+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int strStr(String haystack, String needle) {
3+
if ("".equals(needle)) {
4+
return 0;
5+
}
6+
7+
int len1 = haystack.length();
8+
int len2 = needle.length();
9+
int p = 0;
10+
int q = 0;
11+
while (p < len1) {
12+
if (haystack.charAt(p) == needle.charAt(q)) {
13+
if (len2 == 1) {
14+
return p;
15+
}
16+
++p;
17+
++q;
18+
} else {
19+
p -= q - 1;
20+
q = 0;
21+
}
22+
23+
if (q == len2) {
24+
return p - q;
25+
}
26+
}
27+
return -1;
28+
}
29+
}

0 commit comments

Comments
 (0)