LintCode 三數之和

1. 三數之和

給出一個有n個整數的數組S,在S中找到三個整數a, b, c,找到所有使得a + b + c = 0的三元組。
樣例
如S = {-1 0 1 2 -1 -4}, 你需要返回的三元組集合的是:
(-1, 0, 1)
(-1, -1, 2)
注意
在三元組(a, b, c),要求a <= b <= c。結果不能包含重複的三元組。

solution:
因爲三元組的元素是按照遞增的順序,因此我們首先需要對S進行排序。然後保持兩個前後索引來進行比較。
code

class Solution {
public:    
    /**
     * @param numbers : Give an array numbers of n integer
     * @return : Find all unique triplets in the array which gives the sum of zero.
     */
    vector<vector<int> > threeSum(vector<int> &nums) {
        // write your code here   
        vector<vector<int>> res;
        sort(nums.begin(), nums.end()); //對數組進行排序
        int i = 0 , last = nums.size()-1;
        while (i < last) {
            int a = nums[i], j = i + 1, k = last;
            while (j < k) {
                int b = nums[j], c = nums[k];
                int sum = a + b + c;
                if (sum == 0)  res.push_back({a,b,c});
                if (sum <= 0)  //注意不能含有相同的索引
                    while (nums[j] == b && j < k ) ++j;
                if (sum >= 0)
                    while (nums[k] == c && j < k) --k;
            }
            while (nums[i] == a && i < last) ++i;
        }
        return res;        
    }
};

2. 三數之和 II

給一個包含n個整數的數組S, 找到和與給定整數target最接近的三元組,返回這三個數的和。
樣例
例如S = [-1, 2, 1, -4] and target = 1. 和最接近1的三元組是 -1 + 2 + 1 = 2.
注意
只需要返回三元組之和,無需返回三元組本身

solution: 只需要把前面的問題與0進行比較改成target就可以了。
code:

class Solution {
public:    
    /**
     * @param numbers: Give an array numbers of n integer
     * @param target: An integer
     * @return: return the sum of the three integers, the sum closest target.
     */
    int threeSumClosest(vector<int> nums, int target) {
        // write your code here
        if (nums.size() < 3)
            return accumulate(nums.begin(), nums.end(), 0);
        sort(nums.begin(), nums.end());
        int ans = nums[0] + nums[1] + nums[2];
        int i = 0, last = nums.size()-1;
        int diff = 0;
        while (i < last) {
            int a = nums[i], j = i + 1, k = last;
            while (j < k) {
                int b = nums[j], c = nums[k];
                int sum = a + b + c;
                if (abs(sum - target) < abs(ans - target)) 
                    ans = sum;
                if (ans == target)
                    return ans;
                if (sum < target)
                    while (nums[j] == b && j < k) ++j;
                if (sum > target)
                    while (nums[k] == c && j < k) --k;
            }
            while (nums[i] == a && i < last) ++i;
        }
        return ans;
    }
};

3. 四數之和

給一個包含n個數的整數數組S,在S中找到所有使得和爲給定整數target的四元組(a, b, c, d)。
樣例
例如,對於給定的整數數組S=[1, 0, -1, 0, -2, 2] 和 target=0. 滿足要求的四元組集合爲:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
注意
四元組(a, b, c, d)中,需要滿足a <= b <= c <= d
答案中不可以包含重複的四元組。

solution:思路和前面的三數之和是一樣的,算法複雜度O(n^3).

class Solution {
public:
    /**
     * @param numbers: Give an array numbersbers of n integer
     * @param target: you need to find four elements that's sum of target
     * @return: Find all unique quadruplets in the array which gives the sum of 
     *          zero.
     */
    vector<vector<int> > fourSum(vector<int> nums, int target) {
        // write your code here
        sort(nums.begin(), nums.end());
        vector<vector<int>> res;
        int i = 0, last = nums.size()-1;
        while (i < last) {
            int a = nums[i], j = i + 1;
            while (j < last) {
                int b = nums[j], k = j + 1, l = last;
                while (k < l) {
                    int c = nums[k], d = nums[l];
                    int sum = a + b + c + d;
                    if (sum == target) res.push_back({a,b,c,d});
                    if (sum <= target) 
                        while (nums[k] == c && k < l) ++k;
                    if (sum >= target)
                        while (nums[l] == d && k < l) --l;
                }
                while (nums[j] == b && j < last) ++j;
            }
            while (nums[i] == a && i < last) ++i;
        }
        return res;
    }
};
發佈了49 篇原創文章 · 獲贊 22 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章