class Solution {
public int[] twoSum(int[] price, int target) {
int left = 0,right = price.length-1,sum = 0;
while (left < right){
sum = price[left] + price[right];
if(sum > target){
right --;
}else if(sum < target){
left ++;
}else{
return new int[]{price[left],price[right]};
}
}
return new int[]{};
}
}
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
int n = nums.length;
for(int i = 0;i < n; ){
if(nums[i] > 0){
break;
}
int left = i + 1,right = n - 1,t = -nums[i];
while(left < right){
int sum = nums[left] + nums[right];
if(sum > t){
right --;
}else if(sum < t){
left ++;
}else{
res.add(new ArrayList<Integer>(Arrays.asList(nums[left],nums[right],nums[i])));
left ++;
right --;
while(left < right && nums[left]==nums[left-1]){
left ++;
}
while(left < right && nums[right]==nums[right+1]){
right --;
}
}
}
i++;
while(i < n && nums[i] == nums[i-1]){
i++;
}
}
return res;
}
}
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
int n = nums.length;
for(int i = 0; i< n;){
for(int j = i+1;j<n;){
int left = j+1,right = n-1;
long aim =(long)target - nums[i] - nums[j];
while(left < right){
int sum = nums[left] + nums[right];
if(sum> aim){
right --;
}else if(sum < aim){
left ++;
}else{
res.add(new ArrayList<Integer>(Arrays.asList(nums[left],nums[right],nums[i],nums[j])));
left ++;
right --;
while(left < right && nums[left]==nums[left-1]){
left ++;
}
while(left < right && nums[right]==nums[right+1]){
right --;
}
}
}
j++;
while(j < n && nums[j] == nums[j-1]){
j++;
}
}
i++;
while(i < n && nums[i] == nums[i-1]){
i++;
}
}
return res;
}
}