LeetCode 167. Sum of Two Numbers II-Input Ordered Array, 15. Sum of Three Numbers

topic

Given an ordered array that has been arranged in ascending order, find two numbers so that their sum is equal to the target number.

The function should return these two subscript values ​​index1 and index2, where index1 must be less than index2. link

Explanation: The
returned subscript values ​​(index1 and index2) are not zero-based.
You can assume that each input only corresponds to a unique answer, and you cannot reuse the same elements.

Ideas

HashMap, O(n)Scan, findtarget - 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;
    }   
}

However, Map requires extra space, and dual pointers are better. There is also a two-point solution, but it for循环is fixed in one numbers[i]and then two-point search, it feels not as good as a direct double pointer.

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;
    }
}

topic

Given an array nums containing n integers, determine whether there are three elements a, b, c in nums, such that a + b + c = 0? Please find all triples that meet the conditions and are not repeated.

Note: The answer cannot contain repeated triples. link

Ideas

Since the conditions for sorting the array are required here, the two questions are put together. It's still the idea of ​​two-number and double-pointer, but it needs to be judged again when traversing.

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--;
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_42007742/article/details/107460753