Cleanest possible C++ Code


  • -2
    S
    class Solution {
    public:
        vector<vector<int>> threeSum(vector<int>& a){
            int n = a.size();
            sort(a.begin(),a.end());
            vector< vector<int> > ans;
            
            for(int i=0;i<(n-2);i++){
                if((i!=0) && (a[i]==a[i-1])){
                    continue;
                }
                for(int j=i+1,k=n-1;j<k;){
                    if((i+1)!=j && (a[j] == a[j-1])){
                        j++;continue;
                    }else if((k+1)!=n && (a[k] == a[k+1])){
                        k--;continue;
                    }else if((a[i] + a[j] + a[k]) == 0){
                        vector<int> v;
                        v.push_back(a[i]);v.push_back(a[j]);v.push_back(a[k]);
                        ans.push_back(v);
                        j++,k--;
                    }else if(((a[i] + a[j] + a[k]) > 0)){
                        k--;
                    }else{
                        j++;
                    }
                }
            }
            
            return ans;
        }
    };

Log in to reply
 

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