Two Sum II
Question
Analysis
Solution
public class Solution {
/**
* @param nums: an array of integer
* @param target: an integer
* @return: an integer
*/
public int twoSum2(int[] nums, int target) {
// Invalid input or exception
if (nums == null || nums.length == 0) {
return 0;
}
// Sort the Array nums[] to ascending order
Arrays.sort(nums);
// Use two pointers
int count = 0;
int pl = 0;
int pr = nums.length - 1;
while (pl < pr) {
if (nums[pl] + nums[pr] > target) {
// Index from pl to pr - 1 will all be counted
count = count + (pr - pl);
pr--;
} else {
pl++;
}
}
return count;
}
}Last updated