3 Sum

code1 is better than code2; cause it saves a if statement; please compare them carefully

code1


class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> ret;
        if(nums.size() == 0)
        {
            return ret;
        }

        sort(nums.begin(), nums.end());
        int n = nums.size();

        for(int i = 0; i<n-2; i++)
        {
            if(i>0 && nums[i] == nums[i-1])
            {
                continue;
            }

            int j = i+1;
            int k = n-1;
            while(j < k)
            {
                int target = nums[j] + nums[k];

                if(target + nums[i] == 0)
                {
                    vector<int> tmp;
                    tmp.push_back(nums[i]);
                    tmp.push_back(nums[j]);
                    tmp.push_back(nums[k]);
                    ret.push_back(tmp);
                    j++;
                    k--;

                    while(j != i+1 && j < k && nums[j] == nums[j-1])
                    {
                        j++;
                    }

                    while(k != n-1 && j < k && nums[k] == nums[k+1])
                    {
                        k--;
                    }
                }
                else if(target + nums[i] > 0)
                {
                    k--;
                }
                else
                {
                    j++;
                }
            }
        }

        return ret;
    }
};

code2


class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> ret;
        if(nums.size() == 0)
        {
            return ret;
        }

        sort(nums.begin(), nums.end());
        int n = nums.size();

        for(int i = 0; i<n; i++)
        {
            if(i>0 && nums[i] == nums[i-1])
            {
                continue;
            }

            int j = i+1;
            int k = n-1;
            while(j < k)
            {
                while(j != i+1 && j < k && nums[j] == nums[j-1])
                {
                    j++;
                }

                while(k != n-1 && j < k && nums[k] == nums[k+1])
                {
                    k--;
                }

                if(j >= k)
                {
                    break;
                }

                int target = nums[j] + nums[k];

                if(target + nums[i] == 0)
                {
                    vector<int> tmp;
                    tmp.push_back(nums[i]);
                    tmp.push_back(nums[j]);
                    tmp.push_back(nums[k]);
                    ret.push_back(tmp);
                    j++;
                    k--;
                }
                else if(target + nums[i] > 0)
                {
                    k--;
                }
                else
                {
                    j++;
                }
            }
        }

        return ret;
    }
};

results matching ""

    No results matching ""