Same code, sometimes compile error, sometimes accepted


  • 0
    G

    Here is my c++ code. Sometimes it says compile error, yet sometimes it says accepted. Not understand why. Can anyone help? Thanks a lot!

    class Solution {
    public:
        int threeSumSmaller(vector<int>& nums, int target) {
            if(nums.size()<3)  return 0;
            int result = 0;
            sort(nums.begin(),nums.end());
            for(int i=0;i<nums.size();i++){
                int left = i+1, right = nums.size()-1;
                while(left<right){
                    if((nums[i]+nums[left]+nums[right])<target){
                        result = result + right-left;
                        left++;
                    }
                    else
                        right--;
                }
            }
            return result;
        }
    };
    

Log in to reply
 

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