[Leetcode][016] 3Sum Closest (Java)

题目: https://leetcode.com/problems/3sum-closest/

【标签】Array; Two Pointers

【个人分析】

  这道题和它的姊妹题 3Sum 非常类似, 就不再多说了,具体一些的分析可以参考 [Leetcode][015] 3Sum

 1 public class Solution {
 2     public int threeSumClosest(int[] nums, int target) {
 3         int result = target;
 4         int len = nums.length;
 5         if (len < 3) {
 6             return 0;
 7         }
 8         Arrays.sort(nums);
 9         for (int i = 0; i < len; i++) {
10             int number = nums[i];
11
12             int leftIndex = i + 1;
13             int rightIndex = len - 1;
14             while (leftIndex < rightIndex) {
15                 int threeSum = number + nums[leftIndex] + nums[rightIndex];
16                 if (threeSum == target) {
17                     // best result found!
18                     return target;
19                 } else {
20                     // update global result
21                     if ( result == target ||
22                             Math.abs(target - threeSum) < Math.abs(target - result)) {
23                         result = threeSum;
24                     }
25                     if (threeSum < target) {
26                         while (leftIndex < rightIndex &&
27                                 nums[leftIndex] == nums[leftIndex + 1]) {
28                             leftIndex++;
29                         }
30                         leftIndex++;
31                     } else {
32                         while (leftIndex < rightIndex &&
33                                 nums[rightIndex] == nums[rightIndex - 1]) {
34                             rightIndex--;
35                         }
36                         rightIndex--;
37                     }
38                 }
39             }
40         }
41         return result;
42     }
43
44 }
时间: 2024-07-29 05:00:52

[Leetcode][016] 3Sum Closest (Java)的相关文章

LeetCode 016 3Sum Closest

[题目] 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. For example, given array S = {

[LeetCode] 016. 3Sum Closest (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 016.3Sum_Closest (Medium) 链接: 题目:https://oj.leetcode.com/problems/3sum-closest/ 代码(github):https://github.com/illuz/leetcode 题意: 在给定数列中找出三个数,使和最接近 target. 分析:

Java for LeetCode 016 3Sum Closest

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. For example, given array S = {-1 2

[LeetCode][JavaScript]3Sum Closest

3Sum Closest 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. For example, given arr

No.016 3Sum Closest

16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium 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

LeetCode[Array]----3Sum Closest

3Sum Closest 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. For example, given arr

leetCode 16.3Sum Closest (离给定值最近的三数之和) 解题思路和方法

3Sum Closest 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. For example, given arr

Leetcode 16. 3Sum Closest(指针搜索)

16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have e

【LeetCode】3Sum Closest 解题报告 (Java)

[题目] 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. For example, given array S = {