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

3 Sum(3 個(gè)數(shù)的和)

翻譯

給定一個(gè)有 n 個(gè)整數(shù)的數(shù)組 S,是否存在三個(gè)元素 a,b,c 使得 a+b+c=0? 找出該數(shù)組中所有不重復(fù)的 3 個(gè)數(shù),它們的和為 0。

備注:
這三個(gè)元素必須是從小到大進(jìn)行排序。
結(jié)果中不能有重復(fù)的 3 個(gè)數(shù)。

例如,給定數(shù)組 S={-1 0 1 2 -1 4},一個(gè)結(jié)果集為:
(-1, 0, 1)
(-1, -1, 2)

原文

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.

Note:
Elements in a triplet (a,b,c) must be in non-descending order.
(ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.

For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:

(-1, 0, 1)
(-1, -1, 2)

經(jīng)典方法,可惜我并沒有想到這樣寫……

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<vector<int>> result;

        int len = nums.size();      
        for (int current = 0; current < len - 2&&nums[current]<=0;current++)
        {
            int front = current + 1, back = len - 1;
            while (front < back)
            {
                if (nums[current] + nums[front] + nums[back] < 0)
                    front++;
                else if (nums[current] + nums[front] + nums[back] > 0)
                    back--;
                else
                {
                    vector<int> v(3);
                        v.push_back(nums[current]);
                        v.push_back(nums[front]);
                        v.push_back(nums[back]);
                        result.push_back(v);
                        v.clear();
                    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;
    }
};

繼續(xù)努力……

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

傳送門:LeetCode 16 3Sum Closest(最接近的3個(gè)數(shù)的和)
傳送門:LeetCode 18 4Sum(4個(gè)數(shù)的和)