主页 三数之和
Post
Cancel

三数之和

前言

本文具有强烈的个人感情色彩,如有观看不适,请尽快关闭. 本文仅作为个人学习记录使用,也欢迎在许可协议范围内转载或分享,请尊重版权并且保留原文链接,谢谢您的理解合作. 如果您觉得本站对您能有帮助,您可以使用RSS方式订阅本站,感谢支持!.

如题

给你一个包含n个整数的数组nums,判断nums中是否存在三个元素abc ,使得 a + b + c = 0?请你找出所有和为0且不重复的三元组。

注意:答案中不可以包含重复的三元组。

示例1

1
2
输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]

示例2

1
2
输入:nums = []
输出:[]

示例3

1
2
输入:nums = [0]
输出:[]

Answer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        int n = nums.size();
        sort(nums.begin(),nums.end());
        vector<vector<int>> ans;
        for (int first = 0; first < n; ++first) {
            //check
            if (first > 0 && nums[first] == nums[first - 1]) {
                continue;
            }
            int third = n -1;
            int target = -nums[first];
            for (int second = first + 1; second < n; ++second) {
                if (second > first + 1 && nums[second] == nums[second - 1]) {
                    continue;
                }
                while (second < third && nums[second] + nums[third] > target) {
                    --third;
                }

                if (second == third) {
                    break;
                }
                if (nums[second] + nums[third] == target) {
                    ans.push_back({nums[first],nums[second],nums[third]});
                }
            }
        }
        return ans;
    }
};

15. 三数之和
引用自codetop

该博客文章由作者通过 CC BY 4.0 进行授权。

数组中的第K个最大元素

最大子数组和