12ms c++ solution


  • 0
    A
    class Solution {
    public:
        int threeSumSmaller(vector<int>& nums, int target) {
    
            sort(nums.begin(),nums.end());
            int a = 0, b=2, c,maxC= (int)nums.size() - 1,result=0;
    
    
            for(a = 0; a < --b;a++)
            {
                b = a+ 1;
                c= maxC;
                while(c > b && nums[a] + nums[b] + nums[c] >= target ) c--;
                maxC = c;
      
                while(b < c)
                    if(nums[a] + nums[b] + nums[c] >= target) c--;
                    else result += c - (b++);
            }
            return result;
        }
    };

Log in to reply
 

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