Leetcode-01-Sum of Two Numbers

Keywords: less

Title Description

Given an array of integers nums and a target value, you can find the two integers that sum to the target value in the array and return their array subscripts. You can assume that each input corresponds to only one answer. However, you can't reuse the same elements in this array.
Example:
Given nums = 2, 7, 11, 15], target = 9
Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]

Solving problems

//Solution 1: Violence law, two cycles
    public int[] twosum(Integer[] numbers, int target) {
        int[] res = new int[2];
        for (int i = 0; i < numbers.length; i++) {
            int v = numbers[i];

            for (int j = i; j < numbers.length; j++) {
                if (numbers[j] == target - v) {
                    if (i == j) {
                        continue;
                    }
                    res[0] = i + 1;
                    res[1] = j + 1;
                    return res;
                }
            }

        }

        return res;
    }
//Solution 2: Use hash to record the location of target - v
    public int[] twosum2(Integer[] numbers, int target) {
        int[] res = new int[2];
        if (target == 0) {
            int j = 0;
            for (int i = 0; i < numbers.length; i++) {
                if (numbers[i] == 0) {
                    res[j++] = i + 1;
                    if (j == 2) {
                        return res;
                    }
                }
            }
        }

        Map<Integer, Integer> map = new HashMap<>();
        for (int i = numbers.length - 1; i >= 0; i--) {
            map.put(numbers[i], i);
            if (map.containsKey(target - numbers[i])) {
                res[0] = i + 1;
                res[1] = map.get(target - numbers[i]) + 1;

                if (res[0] == res[1]) {
                    continue;
                }
                return res;
            }

        }


        return null;
    }

Expand

The sum of two numbers of an ordered array

Given an ordered array arranged in ascending order, find two numbers so that the sum of them equals the target number.

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

Explain:

  • The returned subscripts (index1 and index2) are not zero-based.
  • You can assume that each input corresponds to only one answer, and you can't reuse the same elements.

Example:

Input: numbers = [2, 7, 11, 15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 equals the target number 9. So index1 = 1, index2 = 2.

   public int[] twoSum(int[] numbers, int target) {
            if (numbers == null || numbers.length == 0) {
                return new int[]{-1, -1};
            }
    
            int i = 0;
            int j = numbers.length - 1;
    
            while (i < j) {
                int sum = numbers[i] + numbers[j];
                if (sum == target) {
                    return new int[]{i+1, j+1};
                } else if (sum < target) {
                    i++;
                } else {
                    j--;
                }
            }
            return new int[]{-1, -1};
        }

Posted by stuffradio on Sat, 20 Apr 2019 01:27:33 -0700