Conscise Java Solution


  • 0
    C
    public class Solution {
        public int threeSumSmaller(int[] nums, int target) {
            if(nums == null || nums.length == 0) return 0;
            Arrays.sort(nums);
            int res = 0;
            for(int i = 0; i < nums.length; i++) {
                int m = i + 1;
                int n = nums.length - 1;
                while(m < n) {
                    if(nums[i] + nums[m] + nums[n] >= target) {
                        n--;
                    } else {
                        int right = n;
                        while(n > m){
                            res++;
                            n--;
                        }
                        n = right;
                        m++;
                    }
                }
            }
            return res;
        }
    }

Log in to reply
 

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