3Sum(C++)By yangchaojie


  • 0
    Y

    class Solution {
    public:
    vector<vector<int>> threeSum(vector<int>& nums) {
    vector<vector<int>> res;
    sort(nums.begin(), nums.end());
    for (int k = 0; k < nums.size(); ++k) {
    if (nums[k] > 0) break;
    if (k > 0 && nums[k] == nums[k - 1]) continue;
    int target = 0 - nums[k];
    int i = k + 1, j = nums.size() - 1;
    while (i < j) {
    if (nums[i] + nums[j] == target) {
    res.push_back({nums[k], nums[i], nums[j]});
    while (i < j && nums[i] == nums[i + 1]) ++i;
    while (i < j && nums[j] == nums[j - 1]) --j;
    ++i; --j;
    } else if (nums[i] + nums[j] < target) ++i;
    else --j;
    }
    }
    return res;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.