The sum of two numbers LeetCode 01

Sum of two numbers


Topic link:

https://leetcode-cn.com/problems/two-sum/

Title description

Given an integer array nums and a target value target, please find the two integers whose sum is the target value in the array and return their array subscripts.

You can assume that each input will only correspond to one answer. However, you cannot reuse the same elements in this array.

Example:

Given nums = [2, 7, 11, 15], target = 9

because nums[0] + nums[1] = 2 + 7 = 9 so return [0, 1]

Detailed topic

This question gives us an array, which is full of integers, and then gives us a number target . We need to find out which two numbers in the array are the sum of the target . Note that you cannot reuse the same element in this array, which means that the number at each position can only be used once, such as the array [1,1,3], you can use the first and second 1, But you cannot use the first 1 twice.

Problem solving plan

Idea 1: Time complexity: O(N^2) Space complexity: O(1)

This method is very simple, it is a violent solution, double loop traversal is enough. The outer loop takes the element num[i] with the index i from the array, and the inner loop takes the element nums[j] after the i and adds the element with the index i one by one to determine whether the result is target . Only one question is required, so once found, return directly. The N in the time complexity represents the length of the nums list.

Let's look at the code below:

class Solution {
    
    
    public int[] twoSum(int[] nums, int target) {
    
    
        // 第一轮遍历
        for (int i = 0; i < nums.length; i++) {
    
    
            // 第二轮遍历不能重复计算了
            for (int j = i + 1; j < nums.length; j++) {
    
    
                if (nums[i] + nums[j] == target) {
    
    
                    // 注意 leetcode 中要求返回的是索引位置
                    return new int[]{
    
    i, j};
                }
            }
        }
        return null;
    }
}

Idea 2: Time complexity: O(N) Space complexity: O(N)

The above idea 1 time complexity is too high, it is a typical way to speed up time, this is not advisable. In fact, we can sacrifice space in exchange for time.

We hope that when we traverse sequentially to obtain a number num[1] , we will know whether the number target-num1 matched with it is in our nums , and there is not only one. For example, the target is 4 and nums is [2,3]. Assuming that the num1 we obtained at this time is 2, then the 2 paired with it is indeed in nums , but the number 2 only appears once in nums , and we cannot get two Times, so it won’t work.

So we have the following steps :

  1. Create a HashMap , key stores nums[i] , and value stores index i ;
  2. Analyzing HashMap whether the presence of target - cur , the current value of cur and a HashMap of key values and the sum of target ;
  3. If it exists, return the index of target-cur and the index of current value cur ;
  4. If it does not exist , save the current number cur as the key and the current number index as the value in the HashMap .

Let's look at the code below:

class Solution {
    
    
    public int[] twoSum(int[] nums, int target) {
    
    
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
    
    
            // 将原本为两个目标值切换为一个目标值,只需要每次从 map 中寻找目标值即可
            int num = target - nums[i];
            if (map.containsKey(num)) {
    
    
                return new int[]{
    
    map.get(num), i};
            }
            // 每次遍历过的值都存储到 map 中,这样之后就能从 map 中寻找需要的目标值
            map.put(nums[i], i);
        }
        return null;
    }
}

Guess you like

Origin blog.csdn.net/weixin_38478780/article/details/108404755