FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc15.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
code
/
lc15.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (40 loc) · 1.46 KB
Breadcrumbs
leetcode
/
code
/
lc15.java
Copy path
File metadata and controls
43 lines (40 loc) · 1.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package
code
;
/*
* 15. 3Sum
* 题意:找出数组中所有和为0的三元组合
* 难度:Medium
* 分类:Array, Two Pointers
* 注意:如何避免 List 重复元素
* Tips:lc15, lc16, lc923
*/
import
java
.
util
.*;
public
class
lc15
{
public
static
void
main
(
String
[]
args
) {
int
[]
nums
= {-
1
,
0
,
1
,
2
, -
1
, -
4
};
System
.
out
.
println
(
threeSum
(
nums
).
toString
());
}
public
static
List
<
List
<
Integer
>>
threeSum
(
int
[]
nums
) {
List
<
List
<
Integer
>>
result
=
new
ArrayList
();
Arrays
.
sort
(
nums
);
for
(
int
i
=
0
;
i
<
nums
.
length
-
2
;
i
++) {
if
(
i
>
0
&&
nums
[
i
]==
nums
[
i
-
1
] )
//防止重复添加相同内容List
continue
;
int
left
=
i
+
1
,
right
=
nums
.
length
-
1
;
while
(
left
<
right
){
if
(
nums
[
i
]+
nums
[
left
]+
nums
[
right
] ==
0
){
result
.
add
(
Arrays
.
asList
(
nums
[
i
],
nums
[
left
],
nums
[
right
]));
//Arrays.asList(int a, int b, intc);
while
(
left
<
right
&&
nums
[
left
]==
nums
[
left
+
1
])
//防止重复添加相同内容List
left
++;
while
(
left
<
right
&&
nums
[
right
]==
nums
[
right
-
1
])
//防止重复添加相同内容List
right
--;
left
++;
right
--;
}
else
if
(
nums
[
i
]+
nums
[
left
]+
nums
[
right
] <
0
)
left
++;
else
right
--;
}
}
return
result
;
}
}
Back
|
FazBrowse Home
|
New Git URL