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

Commit1fe9056

Browse files
committed
add: 016
1 parent7aa9d88 commit1fe9056

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
| 11|[Container With Most Water][011]| Array, Two Pointers|
7676
| 12|[Integer to Roman][012]| Math, String|
7777
| 15|[3Sum][015]| Array, Two Pointers|
78+
| 15|[3Sum Closest][016]| Array, Two Pointers|
7879
| 17|[Letter Combinations of a Phone Number][017]| String, Backtracking|
7980
| 19|[Remove Nth Node From End of List][019]| Linked List, Two Pointers|
8081
| 33|[Search in Rotated Sorted Array][033]| Arrays, Binary Search|
@@ -146,6 +147,7 @@
146147
[011]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/011/README.md
147148
[012]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/012/README.md
148149
[015]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/015/README.md
150+
[016]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/016/README.md
149151
[017]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/017/README.md
150152
[019]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md
151153
[033]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/033/README.md

‎note/016/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#[3Sum Closest][title]
2+
3+
##Description
4+
5+
Given an array*S* of*n* integers, find three integers in*S* such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
6+
7+
```
8+
For example, given array S = {-1 2 1 -4}, and target = 1.
9+
10+
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
11+
```
12+
13+
**Tags:** Array, Two Pointers
14+
15+
16+
##思路
17+
18+
这道题和[3Sum][015] 的思路基本一样,先对数组进行排序,然后遍历这个排序数组,用两个指针分别指向当前元素的下一个和数组尾部,判断三者的和与`target` 的差值来移动两个指针,并更新其结果,当然,如果三者的和直接与`target` 值相同,那么返回`target` 结果即可。
19+
20+
```java
21+
publicclassSolution {
22+
publicintthreeSumClosest(int[]nums,inttarget) {
23+
int delta=0x7fffffff, res=0;
24+
Arrays.sort(nums);
25+
int len= nums.length-2;
26+
for (int i=0; i< len; i++) {
27+
int st= i+1, end= nums.length-1;
28+
while (st< end) {
29+
int sum= nums[i]+ nums[st]+ nums[end];
30+
int curDelta=Math.abs(sum- target);
31+
if (curDelta==0)return sum;
32+
if (curDelta< delta) {
33+
delta= curDelta;
34+
res= sum;
35+
}
36+
if (sum> target)--end;
37+
else++st;
38+
}
39+
}
40+
return res;
41+
}
42+
}
43+
```
44+
45+
46+
##结语
47+
48+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-java-leetcode][ajl]
49+
50+
51+
52+
[015]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/015/README.md
53+
[title]:https://leetcode.com/problems/3sum-closest
54+
[ajl]:https://github.com/Blankj/awesome-java-leetcode
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
packagecom.blankj.medium._016;
2+
3+
importjava.util.Arrays;
4+
5+
/**
6+
* <pre>
7+
* author: Blankj
8+
* blog : http://blankj.com
9+
* time : 2018/01/25
10+
* desc :
11+
* </pre>
12+
*/
13+
publicclassSolution {
14+
publicintthreeSumClosest(int[]nums,inttarget) {
15+
intdelta =0x7fffffff,res =0;
16+
Arrays.sort(nums);
17+
intlen =nums.length -2;
18+
for (inti =0;i <len;i++) {
19+
intst =i +1,end =nums.length -1;
20+
while (st <end) {
21+
intsum =nums[i] +nums[st] +nums[end];
22+
intcurDelta =Math.abs(sum -target);
23+
if (curDelta ==0)returnsum;
24+
if (curDelta <delta) {
25+
delta =curDelta;
26+
res =sum;
27+
}
28+
if (sum >target) --end;
29+
else ++st;
30+
}
31+
}
32+
returnres;
33+
}
34+
35+
publicstaticvoidmain(String[]args) {
36+
Solutionsolution =newSolution();
37+
System.out.println(solution.threeSumClosest(newint[]{-1,2,1, -4},1));
38+
}
39+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp