三数之和 - 中等

*************

C++

topic: 15. 三数之和 - 力扣(LeetCode)

*************

Give the topic an inspection.

Just back from 新疆. Xinjiang is so beautiful, both scene and girls there. You gays should go there if you have the chance. And right the day I back, I was called by the police, because I snapped the number plate, which owner is an old and rude lady. She parked her car really terrible in the shopping park and I cannot stand for it so I wricked her number plate. The silly person called 110 and I was finded. I gave the silly lady 150, and that's all. Funny thing. Most of the time, female drivers are dumb. I was unhappy for a time. Because the silly old ugly lady stood on her moral high ground and condemned me, saying that I had caused trouble for the police. At the time, I didn't argue back. I should have cursed her out right then and there. It was you, you stupid woman, who broke the rules first by parking one car across two parking spaces. The police and I were just doing our jobs.

Back to the topic. Iterate the array is really a good way.

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> result;
        int n = nums.size();

        if (n < 3) 
        {
            return result; // 不可能组成三元组
        }

        // 1. 对数组进行排序
        sort(nums.begin(), nums.end());

        // 2. 遍历数组,固定第一个数 nums[i]
        for (int i = 0; i < n - 2; ++i) 
        {
            // 如果 nums[i] > 0,由于数组已排序,后续的数也都会 > 0
            // 那么三个正数之和不可能为 0
            if (nums[i] > 0) 
            {
                break;
            }

            // 跳过重复的 nums[i]
            // 如果 i > 0 (不是第一个元素) 并且当前元素和前一个元素相同
            if (i > 0 && nums[i] == nums[i-1]) 
            {
                continue;
            }

            // 3. 使用双指针在 i+1 到 n-1 的范围内查找
            int left = i + 1;
            int right = n - 1;
            int target = -nums[i]; // 我们要找 nums[left] + nums[right] == target

            while (left < right) 
            {
                int current_sum = nums[left] + nums[right];

                if (current_sum == target) 
                {
                    result.push_back({nums[i], nums[left], nums[right]});

                    // 跳过重复的 nums[left]
                    while (left < right && nums[left] == nums[left+1]) 
                    {
                        left++;
                    }
                    // 跳过重复的 nums[right]
                    while (left < right && nums[right] == nums[right-1]) 
                    {
                        right--;
                    }

                    // 移动指针到下一个不同的元素
                    left++;
                    right--;
                } 
                else if (current_sum < target) 
                {
                    left++; // 和太小,左指针右移
                } 
                else 
                { // current_sum > target
                    right--; // 和太大,右指针左移
                }
            }
        }
        return result;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值