鍍金池/ 教程/ Java/ 3 Sum Closest(最接近的 3 個數(shù)的和)
ZigZag Conversion(Z型轉(zhuǎn)換)
String to Integer (atoi)(轉(zhuǎn)換到整型)
Generate Parentheses(生成括號)
Longest Common Prefix(最長公共前綴)
Remove Duplicates from Sorted Array(從已排序數(shù)組中移除重復(fù)元素)
Swap Nodes in Pairs(交換序列中的結(jié)點)
Valid Parentheses(有效的括號)
4 Sum(4 個數(shù)的和)
3 Sum(3 個數(shù)的和)
3 Sum Closest(最接近的 3 個數(shù)的和)
Reverse Nodes in k-Group(在K組鏈表中反轉(zhuǎn)結(jié)點)
Regular Expression Matching (正則表達(dá)式匹配)
Two Sum
Container With Most Water(最大水容器)
Merge Two Sorted Lists(合并兩個已排序的數(shù)組)
Remove Nth Node From End of List(從列表尾部刪除第 N 個結(jié)點)
String to Integer (atoi)(轉(zhuǎn)換到整型)
Palindrome Number(回文數(shù))
Longest Substring Without Repeating Characters
Roman to Integer(羅馬數(shù)到整型數(shù))
Integer to Roman(整型數(shù)到羅馬數(shù))
Reverse Integer(翻轉(zhuǎn)整數(shù))
Merge k Sorted Lists(合并 K 個已排序鏈表)
Implement strStr()(實現(xiàn) strStr() 函數(shù))
Longest Palindromic Substring(最大回文子字符串)
Add Two Numbers
Letter Combinations of a Phone Number(電話號碼的字母組合)

3 Sum Closest(最接近的 3 個數(shù)的和)

翻譯

給定一個有 n 個整數(shù)的數(shù)組 S,找出 S 中 3 個數(shù),使其和等于一個給定的數(shù),target。

返回這 3 個數(shù)的和,你可以假定每個輸入都有且只有一個結(jié)果。

例如,給定 S = {-1 2 1 -4},和 target = 1。

那么最接近 target 的和是 2。(-1 + 2 + 1 = 2)。

原文

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 1 -4}, and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

思考

也許我已經(jīng)開始體會到上一題中別人寫的方法的思想了。

在這個題目中,我們要做以下幾件事:

  • 用 sort 對輸入的數(shù)組進(jìn)行排序
  • 求出長度 len,current 之所以要小于 len?2,是因為后面需要留兩個位置給 front 和 back
  • 始終保證 front 小于 back
  • 計算索引為 current、front、back 的數(shù)的和,分別有比 target 更小、更大、相等三種情況
  • 更?。喝绻嚯x小于 close,那么close 便等于 target?sum,而結(jié)果就是 sum。更大的情況同理
  • 如果相等,那么要記得將 0 賦值給 close,result 就直接等于 target 了
  • 隨后為了避免計算重復(fù)的數(shù)字,用三個 do/while 循環(huán)遞增或遞減它們

代碼

class Solution
{
public:
    int threeSumClosest(vector<int>& nums, int target) {
        sort(nums.begin(), nums.end());
        int len = nums.size();
        int result = INT_MAX, close = INT_MAX;
        for (int current = 0; current < len - 2; current++) {
            int front = current + 1, back = len - 1;
            while (front < back) {
                int sum = nums[current] + nums[front] + nums[back];
                if (sum < target) {
                    if (target - sum < close) {
                        close = target - sum;
                        result = sum;
                    }
                    front++;
                }
                else if (sum > target) {
                    if (sum - target < close) {
                        close = sum - target;
                        result = sum;
                    }
                    back--;
                }
                else {
                    close = 0;
                    result = target;
                    do {
                        front++;
                    } while (front < back&&nums[front - 1] == nums[front]);
                    do {
                        back--;
                    } while (front < back&&nums[back + 1] == nums[back]);
                }
            }
            while (current < len - 2 && nums[current + 1] == nums[current]) {
                current++;
            }
        }
        return result;
    }
};

和本道題關(guān)聯(lián)密切的題目推薦:

傳送門:LeetCode 15 3 Sum(3 個數(shù)的和)
傳送門:LeetCode 18 4 Sum(4 個數(shù)的和)