Why exceed time limit


  • 0
    L
    bool compare(int num1, int num2)
    

    {
    return num1 < num2;
    }

    class Solution {
    public:
    vector<vector<int> > threeSum(vector<int> &num) {
    vector<vector<int> > res;
    if (num.size() <= 2) return res;

        sort(num.begin(), num.end(), compare);
        int i = num.size() - 1;
        
        for(i = num.size() -1; i>=2; i--){
            if ((i < num.size() - 1) && (num[i] == num[i+1])) {
                continue;
            }
            int target = -num[i];
            int start = 0;
            int end = i - 1;
            while (start < end) {
                int sum = num[start] + num[end];
                
                if (sum == target) {
                    vector<int> v;
                    v.push_back(num[start]);
                    v.push_back(num[end]);
                    v.push_back(num[i]);
                    res.push_back(v);
                    start++;
                    end--;
                    continue;
                } else if (sum > target) {
                    end--;
                } else {
                    start++;
                }
            }
            i--;
        }
        
    }
    

    };


Log in to reply
 

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