16ms C++ solution


  • 0
    G
    class Solution {
    public:
        int threeSumSmaller(vector<int>& nums, int target) 
    	{
    		if(nums.size()==0) return 0;
    
    		sort(nums.begin(),nums.end());
    
    		int ret=0;
    		for(int i=nums.size()-1;i>=0;i--)
    		{
    			int t=target-nums[i];
    			int p=0;
    			int q=i-1;
    
    			while(p<q)
    			{
    				if(nums[p]+nums[q]<t)
    				{
    					ret+=(q-p);
    					p++;
    				}
    				else
    				{
    					q--;
    				}
    			}
    		}
    
    		return ret;
        }
    };

Log in to reply
 

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