Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- /**
- * @param nums: an array of integer
- * @param target: an integer
- * @return: an integer
- */
- public int twoSum2(int[] nums, int target) {
- if (nums == null || nums.length < 2) {
- return 0;
- }
- Arrays.sort(nums);
- int left = 0, right = nums.length - 1;
- int count = 0;
- while (left < right) {
- if (nums[left] + nums[right] <= target) {
- left++;
- } else {
- count += right - left;
- right--;
- }
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement