标签:art -- this lint ons 相加 blog tco 第一个
Given an array of integers, find how many pairs in the array such that their sum is less than or equal to
a specific target number. Please return the number of pairs.
Given nums = [2, 7, 11, 15]
, target = 24
.
Return 5
.
2 + 7 < 24
2 + 11 < 24
2 + 15 < 24
7 + 11 < 24
7 + 15 < 25
public class Solution { /** * @param nums an array of integer * @param target an integer * @return an integer */ public int twoSum5(int[] nums, int target) { if (nums == null || nums.length < 2) { return 0; } Arrays.sort(nums); int start = 0; int end = nums.length - 1; int count = 0; while (start < end) { if (nums[start] + nums[end] <= target) { count += end - start; start++; } else { end--; } } return count; } }
Two Sum - Less than or equal to target Lintcode
标签:art -- this lint ons 相加 blog tco 第一个
原文地址:http://www.cnblogs.com/aprilyang/p/6697330.html