设计一个算法,找出数组中两数之和为指定值的所有整数对。一个数只能属于一个数对。
示例 1:
输入: nums = [5,6,5], target = 11
输出: [[5,6]]
示例 2:
输入: nums = [5,6,5,6], target = 11
输出: [[5,6],[5,6]]
提示:
nums.length <= 100000
-10^5 <= nums[i], target <= 10^5
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<List<Integer>> pairSums(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
if (nums == null || nums.length == 0) {
return result;
}
Arrays.sort(nums);
int start = 0;
int end = nums.length - 1;
while (start < end) {
if (nums[start] + nums[end] == target) {
List<Integer> one = new ArrayList<>();
one.add(nums[start]);
one.add(nums[end]);
result.add(one);
start++;
end--;
} else if (nums[start] + nums[end] < target) {
start++;
} else {
end--;
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)