3Sum Smaller

http://www.lintcode.com/zh-cn/problem/3sum-smaller/

public class Solution {
    /**
     * @param nums:   an array of n integers
     * @param target: a target
     * @return: the number of index triplets satisfy the condition nums[i] + nums[j] + nums[k] <
     * target
     */
    public int threeSumSmaller(int[] nums, int target) {
        // Write your code here
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            int s1 = nums[i];
            for (int j = i + 1; j < nums.length; j++) {
                int s2 = s1 + nums[j];
                for (int k = j + 1; k < nums.length; k++) {
                    int s3 = s2 + nums[k];
                    if (s3 < target) {
                        res++;
                    }
                }
            }

        }
        return res;
    }
}

你可能感兴趣的:(3Sum Smaller)