class Solution {
public int[] twoSum(int[] numbers, int target) {
int left=0, right=numbers.length-1;
while(left < right){
if(numbers[left]+numbers[right]== target){
return new int[]{left+1, right+1};
}
if(numbers[left]+numbers[right] > target){
right--;
}
if(numbers[left]+numbers[right] < target){
left++;
}
}
return new int[]{-1, -1};
}
}
class Solution {
public int maxArea(int[] height) {
int left=0, right=height.length-1;
int max=Integer.MIN_VALUE;
while(left < right ){
max =Math.max(max,(right -left) * Math.min(height[left], height[right]));
if(height[left]>=height[right]){
right--;
}else{
left++;
}
}
return max;
}
}
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<List<Integer>>();
for(int first=0; first< n; first++){
//剔除重复的元素
if(first> 0 && nums[first] == nums[first-1]){
continue;
}
int third = n-1;
int target = -nums[first];
for(int second =first +1; second< n ; second++){
//剔除重复的元素
if(second > first+1 && nums[second] == nums[second-1]){
continue;
}
//移动最后的指针,找到最后一个 nums[second]+ nums[third] <=target的位置
while(second< third && nums[second]+ nums[third] > target){
--third;
}
//循环结束
if(second==third){
break;
}
//添加到列表中
if (nums[second] + nums[third] == target) {
List<Integer> list = new ArrayList<Integer>();
list.add(nums[first]);
list.add(nums[second]);
list.add(nums[third]);
ans.add(list);
}
}
}
return ans;
}
}