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 twoSum6(int[] nums, int target) {
- // Write your code here
- if (nums == null || nums.length < 2)
- return 0;
- Arrays.sort(nums);
- int cnt = 0;
- int left = 0, right = nums.length - 1;
- while (left < right) {
- int v = nums[left] + nums[right];
- if (v == target) {
- cnt ++;
- left ++;
- right --;
- while (left < right && nums[right] == nums[right + 1])
- right --;
- while (left < right && nums[left] == nums[left - 1])
- left ++;
- } else if (v > target) {
- right --;
- } else {
- left ++;
- }
- }
- return cnt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement