LeetCode 167. 两数之和 II - 输入有序数组,15. 三数之和

题目

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。

函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。链接

说明:
返回的下标值(index1 和 index2)不是从零开始的。
你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。

思路

HashMapO(n)扫描,查找target - numbers[i]

class Solution {
    
    
    public int[] twoSum(int[] numbers, int target) {
    
    
        Map<Integer, Integer> map = new HashMap<>();
        int[] res = new int[2];
        int left = 0, right = numbers.length - 1;    
        while(left < right){
    
    
            int sum = numbers[left] + numbers[right];
            if(sum < target){
    
    
                left++;
            }else if(sum > target){
    
    
                right--;
            }else{
    
    
                res[0] = left + 1;
                res[1] = right + 1;
                return res;
            }
        }
        return res;
    }   
}

不过Map是需要额外空间,双指针好一点。还有有二分的解法,但是for循环里固定一个numbers[i]再二分查找,感觉不如直接双指针。

class Solution {
    
    
    public int[] twoSum(int[] numbers, int target) {
    
    
        Map<Integer, Integer> map = new HashMap<>();
        int[] res = new int[2];
        int left = 0, right = numbers.length - 1;    
        while(left < right){
    
    
            int sum = numbers[left] + numbers[right];
            if(sum < target){
    
    
                left++;
            }else if(sum > target){
    
    
                right--;
            }else{
    
    
                res[0] = left + 1;
                res[1] = right + 1;
                return res;
            }
        }
        return res;
    }
}

题目

给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。链接

思路

这里由于都需要排序数组的条件,两个题就放一起了。还是两数之和双指针的思路,只不过在遍历的时候需要去重判断。

class Solution {
    
    
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> list;

    public List<List<Integer>> threeSum(int[] nums) {
    
    
        int n = nums.length;
        //先排序,方便后面判断重复
        Arrays.sort(nums);
        for (int first = 0; first < n; ++first) {
    
    
        	//判断重复跳过
            if(first > 0 && nums[first] == nums[first - 1]){
    
    
                continue;
            }
            list = new ArrayList<>();
            twoSum(nums, first + 1, n - 1, 0 - nums[first]);
        }
        return ans;
    }

    void twoSum(int[] arr, int start, int end, int target){
    
    
        int first = start - 1;
        int left = start, right = end;
        while(left < right){
    
    
        	//这里同样判断重复
            if (left > start && arr[left] == arr[left - 1]) {
    
    
                left++;
                continue;
            }
            int sum = arr[left] + arr[right];
            if(sum < target){
    
    
                left++;
            }else if(sum > target){
    
    
                right--;
            }else{
    
    
                list.add(arr[first]);
                list.add(arr[left]);
                list.add(arr[right]);
                ans.add(list);
                list = new ArrayList<>();
                left++;
                right--;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42007742/article/details/107460753