LeetCode - Two Sum I - II - III - IV、3Sum、3Sum Closest、4Sum、4Sum II 系列学习总结

目录

1 - Two Sum

2 - Two Sum II - Input array is sorted

3 - Two Sum III - Data structure design

4 - Two Sum IV - Input is a BST

5 - 3Sum

6 - 3Sum Closest

7 - 3Sum Smaller

8 - 4Sum

9 - 4Sum II


本文将包括上述9个LeetCode中与数列中的元素求和相关的题目,都不是很难,没有 Hard 的题,但是有的题的细节还是值得总结和学习的。

1 - Two Sum

    就是在一个 vector<int> 中找到两个数相加等于题目给出的 target,不能重复用一个数,并且题目只有一个解。

    Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].

vector<int> twoSum(vector<int>& nums, int target) {
    unordered_map<int, int> mymap;
    vector<int> res;
    for (int i = 0; i < nums.size(); i++)
    {
        // 由于是一边找一边向map中插入,所以 mymap[target - nums[i] 一定小于 i(虽然让这题没要求顺序)
        // 由于题目保证只有一组解,所以不需要担心 5,5,5 找 target = 10的情况下,map值被覆盖
        // 由于是先找,再赋值,也不需要担心 5,5,6 找 target = 10的情况下,第二个 5 修改 mymap[5] 的值
        if (mymap.find(target - nums[i]) != mymap.end())
        {
            res.push_back(mymap[target - nums[i]]);
            res.push_back(i);			
            return res;
        }
        mymap[nums[i]] = i;
    }
    return res;
}

2 - Two Sum II - Input array is sorted

    和第一题一样,区别就是这里的 vector<int> 已经排好序了(sorted in ascending order),找出两个数相加等于target,两个数的索引保证 index1 < index2,并且两个数的索引不是 zero-based 的,也就是索引从1开始。

vector<int> twoSum(vector<int>& numbers, int target)
{
    for(int i = 0; i < numbers.size(); i++)
    {
        if(i > 0 && numbers[i] == numbers[i - 1]) continue;
        // 从 numbers.begin() + i + 1 开始找,加速查找,并且避免找到这个数自己
        auto ite = find(numbers.begin() + i + 1, numbers.end(), target - numbers[i]);
        if(ite != numbers.end())
            return vector<int>({i + 1, ite - numbers.begin() + 1});
        }
    return vector<int>();
}

3 - Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations:add and find.

    add - Add the number to an internal data structure.
    find - Find if there exists any pair of numbers which sum is equal to the value.

    For example,
    add(1); add(3); add(5);
    find(4) -> true
    find(7) -> false

    这题就是把第一题变成了数据结构的设计。

class TwoSum {
public:
    void add(int number) {
        ++m[number];
    }
    bool find(int value) {
        for (auto a : m) {
            int t = value - a.first;
            if ((t != a.first && m.count(t)) || (t == a.first && a.second > 1))
                return true;
        }
        return false;
    }
private:
    unordered_map<int, int> m;
};

4 - Two Sum IV - Input is a BST

    给出一个二叉树,判断是否有两个节点的 val 相加等于 target,返回 true 或 false。二叉树定义如下:

// Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

    dfs代码如下:

bool dfs(TreeNode* root, int k, unordered_set<int>& valset)
{
    if(root == NULL)
        return false;
    if(valset.find(k - root->val) != valset.end())
        return true;
    valset.insert(root->val);
    return(dfs(root->left, k, valset) || dfs(root->right, k, valset));       
}
    
bool findTarget(TreeNode* root, int k)
{
    unordered_set<int> valset;
    return dfs(root, k, valset);
}

5 - 3Sum

    在一个 vector<int> 中找到所有的满足 a + b + c = 0 的 unique 的三元组,比如

    Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]

    直接用一个数 i 从 0 走到 n - 2 的位置,另外两个数从 i + 1 和 n - 1 想内夹逼即可。

vector<vector<int>> threeSum(vector<int>& nums)
{
    vector<vector<int> > res;
    int n = nums.size();
    if (n < 3) return res;
    sort(nums.begin(), nums.end());

    for (int i = 0; i < n - 2; i++)
    {
        if (nums[i] > 0) break;       // 因为是sorted,所以第一个数大于0肯定就过了
        if (i > 0 && nums[i - 1] == nums[i]) continue;    // 跳过相同的数

        int l = i + 1, r = n - 1;	
        while (l < r)
        {
            int a = nums[i], b = nums[l], c = nums[r];
            int sum = a + b + c;
            if (sum == 0)
            {
                res.push_back(vector<int>({ a, b, c }));
                while (b == nums[++l]);                   // 跳过相同的数
                while (c == nums[--r]);                   // 跳过相同的数
                // 相当于
                // while(nums[l + 1] == b) ++l;
                // while(nums[r - 1] == c) --r;
                // ++l; --r;
            }
            else if (sum > 0) --r;
            else ++l;
        }
    }
    return res;
}

6 - 3Sum Closest

    找出一组 a, b, c 三个数加起来与 target 最接近,也就是 abs(a + b + c - target) 最小,题目保证只有一组解。

    这道题和上边的求和等于0一个意思,不过判断条件变了,但是值得注意的是这题不能像 3 sum 一题中用 while (b == nums[++l]) 方式跳过一些相同的数,一个是因为求和 == target 的话就返回了,因为这肯定是最好的情况,另个一个原因是因为比如 -1, 0, 1, 1, 55,当 l 指向第一个1时,不能因为后边也是1就直接向后走,因为可能也会由于 l < n 的限制导致r走不到 1 这个位置。

int threeSumClosest(vector<int>& nums, int target)
{
    int res = -1;
    int diff = 9999;
    int n = nums.size();
    sort(nums.begin(), nums.end());
    for (int i = 0; i < n - 2; i++)
    {
        if(i > 0 && nums[i] == nums[i - 1]) continue;   // 跳过相同的数
        int l = i + 1, r = n - 1;
        while (l < r)
        {
            int ln = nums[l], rn = nums[r];
            int tmp_sum = nums[i] + ln + rn;
            int tmp_diff = abs(tmp_sum - target);
            if (tmp_diff == 0) return target;           // 0肯定是最好的情况了
            if (tmp_diff < diff)
            {
                res = tmp_sum;
                diff = tmp_diff;
            }
            if (tmp_sum < target)      ++l;
            else if (tmp_sum > target) --r;
        }
    }
    return res;
}

7 - 3Sum Smaller

Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.

For example, given nums = [-2, 0, 1, 3], and target = 2.

Return 2. Because there are two triplets which sums are less than 2:

[-2, 0, 1]
[-2, 0, 3]

    题目要求找出所有满足 nums[i] + nums[j] + nums[k] < target 的 i, j, k 的三元组,返回三元组的个数。并且题目要求 O(n2)的时间。

    和上一题一样,只不过判断又变了,三道 3Sum 的题的中心思想都是一个数 i 从 0 走到 n - 2 的位置,另外两个数每次从 i + 1 和 n - 1 想内夹逼,三道题不同的点就是这两个数夹逼的方式变了。

int threeSumSmaller(vector<int>& nums, int target) {
    if (nums.size() < 3) return 0;
    int res = 0, n = nums.size();
    sort(nums.begin(), nums.end());
    for (int i = 0; i < n - 2; ++i) {
        int left = i + 1, right = n - 1;
        while (left < right) {
            if (nums[i] + nums[left] + nums[right] < target) {
                res += right - left; // 如果求和小于target了,那么right从当前位置一直减到left,求和一定都小于target
                ++left;
            }
            else
                --right;
        }
    }
    return res;
}

8 - 4Sum

    和3Sum一样,找出所有相加等于target的不重复的四元组。方法和3Sum也很类似,不过是两个数从左往右,然后两个数在后边夹逼。

vector<vector<int> > fourSum(vector<int> &nums, int target)
{
    set<vector<int> > nset;  // 用set来保证没有重复,其实用vector也没有问题
    int n = nums.size();
    sort(nums.begin(),nums.end());
    for(int i = 0; i < n - 3; i++)
    {
        if(nums[i] > target / 4) break;                // 加上这句会快很多!!
        if(i > 0 && nums[i] == nums[i - 1]) continue;
        for(int j = i + 1; j < n - 2; j++)
        {
            if(j > i + 1 && nums[j] == nums[j - 1]) continue;
            
            int l = j + 1, r = n - 1;
            while(l < r)
            {
                int ln = nums[l], rn = nums[r];
                int tmp_sum = nums[i] + nums[j] + ln + rn;
                if(tmp_sum == target)
                {
                    nset.insert(vector<int>({nums[i], nums[j], ln, rn}));
                    //while(nums[l + 1] == ln) ++l;        // 可以用while跳过重复数字,但是这道题中会变慢
                    //while(nums[r - 1] == rn) --r;
                    ++l; --r;
                }
                else if(tmp_sum > target)
                {
                    --r;
                    //while(nums[r - 1] == rn) --r;       // 可以用while跳过重复数字,但是这道题中会变慢
                }
                else
                {
                    ++l;
                    //while(nums[l + 1] == ln) ++l;       // 可以用while跳过重复数字,但是这道题中会变慢
                }
            }
        }
    }
    return vector<vector<int> >(nset.begin(), nset.end());
}

 9 - 4Sum II

    题目给出4个相同大小的 vector<int>,然后找出四元组 i, j, k, l 满足 A[i] + B[j] + C[k] + D[l] = 0,返回这样玩的四元组的个数。值得注意的是,四元组是不能重复的,不过四个数加起来等于0的组合可以使重复的,这样我们只需要记录这4个数组各有哪些数,然后计算这些数组合加起来等于0的情况,比如1 + 2 - 1 - 2 =0,A中有两个1,那么就是两种四元组。

    代码中,在前两个数组中,对数进行组合,记录组合求和之后的数,比如 1 + 2 和 2 + 1,那么就代表,数组A+B中有两个3,再在A+B这个数组中,遍历查找有没有后边两个数组所有数的组合的相反数(这样四个数组组合加起来就是0了),就可以得到满足条件的组合,并且这样计算 1 + 2 - 2 - 1 和 2 + 1 - 2 - 1,不需要计算两次,只需要知道 3 - 2 - 1 = 0,然后A+B中有两个3就知道有两种组合了。

int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D)
{
    unordered_map<int, int>  abSum;
    for(auto a : A)
        for(auto b : B)
            ++abSum[a+b];
    int count = 0;
    for(auto c : C)
    {
        for(auto d : D)
        {
            auto it = abSum.find(0 - c - d);
            if(it != abSum.end())
                count += it->second;
        }
    }
    return count;
}

    

    

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值