Skip to content

Commit 5747297

Browse files
committed
feat: add 015
1 parent aafe9d1 commit 5747297

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
|2|[Add Two Numbers][002]|Linked List, Math|
6161
|3|[Longest Substring Without Repeating Characters][003]|Hash Table, Two Pointers, String|
6262
|8|[String to Integer (atoi)][008]|Math, String|
63+
|15|[3Sum][015]|Array, Two Pointers|
6364
|19|[Remove Nth Node From End of List][019]|Linked List, Two Pointers|
6465
|554|[Brick Wall][554]|Hash Table|
6566

@@ -115,6 +116,7 @@
115116
[002]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/002/README.md
116117
[003]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/003/README.md
117118
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
119+
[015]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/015/README.md
118120
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md
119121
[554]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/554/README.md
120122

note/010/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,38 @@ isMatch("aab", "c*a*b") → true
3030

3131
题意是
3232

33+
34+
``` java
35+
class Solution {
36+
public boolean isMatch(String s, String p) {
37+
if (p.length() == 0) return s.length() == 0;
38+
if (p.length() == 1) {
39+
if (s.length() < 1) return false;
40+
if (p.charAt(0) != s.charAt(0) && p.charAt(0) != '.') return false;
41+
return true;
42+
}
43+
if (p.charAt(1) != '*') {
44+
if (s.length() < 1) return false;
45+
if (p.charAt(0) != s.charAt(0) && p.charAt(0) != '.') return false;
46+
return isMatch(s.substring(1), p.substring(1));
47+
} else {
48+
// match 0 preceding element
49+
if (isMatch(s, p.substring(2))) return true;
50+
int i = 0;
51+
// match 1 or more preceding element
52+
while (i < s.length() && (p.charAt(0) == s.charAt(i) || p.charAt(0) == '.')) {
53+
if (isMatch(s.substring(i + 1), p.substring(2))) {
54+
return true;
55+
}
56+
++i;
57+
return false;
58+
}
59+
}
60+
}
61+
}
62+
```
63+
64+
3365
``` java
3466
class Solution {
3567
public boolean isMatch(String s, String p) {

note/015/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# [3Sum][title]
2+
3+
## Description
4+
5+
Given an array *S* of *n* integers, are there elements *a*, *b*, *c* in *S* such that *a* + *b* + *c* = 0? Find all unique triplets in the array which gives the sum of zero.
6+
7+
**Note:** The solution set must not contain duplicate triplets.
8+
9+
```
10+
For example, given array S = [-1, 0, 1, 2, -1, -4],
11+
12+
A solution set is:
13+
[
14+
[-1, 0, 1],
15+
[-1, -1, 2]
16+
]
17+
```
18+
19+
**Tags:** Array, Two Pointers
20+
21+
22+
## 思路
23+
24+
题意是让你从数组中找出所有三个和为0的元素构成的非重复序列,这样的话我们可以把数组先做下排序,然后遍历这个排序数组,用两个指针分别指向当前元素的下一个和数组尾部,判断三者和与0的大小来移动两个指针,其中细节操作就是注意去重。
25+
26+
``` java
27+
class Solution {
28+
public List<List<Integer>> threeSum(int[] nums) {
29+
Arrays.sort(nums);
30+
List<List<Integer>> list = new ArrayList<>();
31+
int i = 0;
32+
while (i < nums.length - 2) {
33+
if (nums[i] > 0) break;
34+
int left = i + 1, right = nums.length - 1;
35+
while (left < right) {
36+
int sum = nums[i] + nums[left] + nums[right];
37+
if (sum == 0) {
38+
list.add(Arrays.asList(nums[i], nums[left++], nums[right--]));
39+
while (left < right && nums[left] == nums[left - 1]) ++left;
40+
while (left < right && nums[right] == nums[right + 1]) --right;
41+
} else if (sum < 0) ++left;
42+
else --right;
43+
}
44+
while (nums[i] == nums[++i] && i < nums.length - 2) ;
45+
}
46+
return list;
47+
}
48+
}
49+
```
50+
51+
52+
## 结语
53+
54+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
55+
56+
57+
58+
[title]: https://leetcode.com/problems/3sum
59+
[ajl]: https://github.com/Blankj/awesome-java-leetcode
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.blankj.medium._015;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* <pre>
9+
* author: Blankj
10+
* blog : http://blankj.com
11+
* time : 2017/04/23
12+
* desc :
13+
* </pre>
14+
*/
15+
public class Solution {
16+
public List<List<Integer>> threeSum(int[] nums) {
17+
Arrays.sort(nums);
18+
List<List<Integer>> list = new ArrayList<>();
19+
int i = 0;
20+
while (i < nums.length - 2) {
21+
if (nums[i] > 0) break;
22+
int left = i + 1, right = nums.length - 1;
23+
while (left < right) {
24+
int sum = nums[i] + nums[left] + nums[right];
25+
if (sum == 0) {
26+
list.add(Arrays.asList(nums[i], nums[left++], nums[right--]));
27+
while (left < right && nums[left] == nums[left - 1]) ++left;
28+
while (left < right && nums[right] == nums[right + 1]) --right;
29+
} else if (sum < 0) ++left;
30+
else --right;
31+
}
32+
while (nums[i] == nums[++i] && i < nums.length - 2) ;
33+
}
34+
return list;
35+
}
36+
37+
public static void main(String[] args) {
38+
Solution solution = new Solution();
39+
System.out.println(solution.threeSum(new int[]{-1, 0, 1, 2, -1, -4}));
40+
}
41+
}

0 commit comments

Comments
 (0)