Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5747297

Browse files
committed
feat: add 015
1 parentaafe9d1 commit5747297

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
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

Lines changed: 32 additions & 0 deletions
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+
classSolution {
36+
publicbooleanisMatch(Strings,Stringp) {
37+
if (p.length()==0)return s.length()==0;
38+
if (p.length()==1) {
39+
if (s.length()<1)returnfalse;
40+
if (p.charAt(0)!= s.charAt(0)&& p.charAt(0)!='.')returnfalse;
41+
returntrue;
42+
}
43+
if (p.charAt(1)!='*') {
44+
if (s.length()<1)returnfalse;
45+
if (p.charAt(0)!= s.charAt(0)&& p.charAt(0)!='.')returnfalse;
46+
return isMatch(s.substring(1), p.substring(1));
47+
}else {
48+
// match 0 preceding element
49+
if (isMatch(s, p.substring(2)))returntrue;
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+
returntrue;
55+
}
56+
++i;
57+
returnfalse;
58+
}
59+
}
60+
}
61+
}
62+
```
63+
64+
3365
```java
3466
classSolution {
3567
publicbooleanisMatch(Strings,Stringp) {

‎note/015/README.md

Lines changed: 59 additions & 0 deletions
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+
classSolution {
28+
publicList<List<Integer>>threeSum(int[]nums) {
29+
Arrays.sort(nums);
30+
List<List<Integer>> list=newArrayList<>();
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+
}elseif (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
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
packagecom.blankj.medium._015;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Arrays;
5+
importjava.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+
publicclassSolution {
16+
publicList<List<Integer>>threeSum(int[]nums) {
17+
Arrays.sort(nums);
18+
List<List<Integer>>list =newArrayList<>();
19+
inti =0;
20+
while (i <nums.length -2) {
21+
if (nums[i] >0)break;
22+
intleft =i +1,right =nums.length -1;
23+
while (left <right) {
24+
intsum =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+
}elseif (sum <0) ++left;
30+
else --right;
31+
}
32+
while (nums[i] ==nums[++i] &&i <nums.length -2) ;
33+
}
34+
returnlist;
35+
}
36+
37+
publicstaticvoidmain(String[]args) {
38+
Solutionsolution =newSolution();
39+
System.out.println(solution.threeSum(newint[]{-1,0,1,2, -1, -4}));
40+
}
41+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp