Punch card question 13

#左耳音风 ARST check-in activity restart#

Table of contents

 1. Problems

2. Problem solving method one

 3. Problem solving method 2

Fourth, the difference between methods 


 Interpretation of ARTS - Complete one ARTS per week:
● Algorithm: Do at least one LeetCode algorithm problem per week
● Review: Read and comment on at least one English technical article
● Tips: Learn at least one technical skill
● Share: Share one Technical articles with opinions and thoughts

It is hoped that through this event, a wave of people who love technology can be gathered, and the spirit of curiosity, exploration, practice, and sharing can be continued.
 


 1. Problems

You are given an array nums of n integers, where nums[i] is in the interval [1, n]. Please find all the numbers in the range [1, n] but not in nums, and return the result in the form of an array.

Example 1:

Input: nums = [4,3,2,7,8,2,3,1]
Output: [5,6]


Example 2:

Input: nums = [1,1]
Output: [2]
 

hint:

n == nums.length
1 <= n <= 105
1 <= nums[i] <= n

2. Problem solving method one

def findDisappearedNumbers(nums):
    n = len(nums)
    for num in nums:
        x = (num - 1) % n
        nums[x] += n
    result = []
    for i in range(n):
        if nums[i] <= n:
            result.append(i + 1)
    return result

This code implements a function `findDisappearedNumbers`, which is used to find all numbers in the given array that are
in the range [1, n] but do not appear in the original array. The implementation of this function is as follows:

  1. First, we get the length `n` of the input array `nums`, and define a variable `x`. For each number `num`, we subtract 1 from it and modulo the length of the array, the result is where it should appear in the original array. Then, we add the length of the array to the element at this position, so that the number that originally appeared at this position can be changed to a state that is not in the array. Specifically, assuming that the element at the i-th position in the original array is a[i], after the above operations, a[i] - n will become a negative number, indicating that the number is no longer in the original array.

  2. Next, we traverse the entire array `nums` to find all subscripts that are still less than or equal to the length of the array. The numbers corresponding to these subscripts are numbers that did not appear in the original array. Specifically, we use a loop to traverse the array `nums`, for each subscript `i`, if `nums[i]` is less than or equal to `n`, it means that the number does not appear in the original array, we will it Add one and add to the result array `result`.

  3. Finally, we return the result array `result`, which contains all the numbers in the interval [1, n] but not in the original array.

It should be noted that the time complexity of this function is O(n), and the space complexity is O(1).

 3. Problem solving method 2

class Solution:
    def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
        n = len(nums)
        for num in nums:
            x = (num - 1) % n
            nums[x] += n
        return [i + 1 for i in range(n) if nums[i] <= n]

This solution uses a hash table to record the number of times each number occurs. Specifically, we first define a class Solutionand implement a findDisappearedNumbersmethod called in it. This method accepts an integer array numsas a parameter and returns an integer list, which contains all the numbers in the interval [1, n] but not in the original array.

In the method, we first get numsthe length of the input array nand define a variable x. For each number num, we subtract 1 from it and modulo the length of the array to get the position where it should appear in the original array. Then, we add the length of the array to the element at this position, so that the number that originally appeared at this position can be changed to a state that is not in the array. Specifically, assuming that the element at the i-th position in the original array is a[i], after the above operations, a[i] - n will become a negative number, indicating that the number is no longer in the original array.

Next, we use a loop to traverse the entire array nums. For each subscript i, if it is nums[i]less than or equal to n, it means that the number does not appear in the original array. In order to determine whether a number has appeared, we need to check whether the corresponding key value in the hash table is greater than zero. Therefore, we look for the item whose key value is in the hash table nums[i]. If we find it and its value is greater than zero, it means that the number has appeared; otherwise, it means that the number has not appeared. Finally, we add all the numbers that have not appeared to the result list and return the result list.

Fourth, the difference between methods 

The main difference between these two problem-solving methods is the implementation and time complexity.

The first method uses an array to record the number of occurrences of each number, and iterates through the array to find all numbers that did not appear in the original array. The time complexity of this method is O(n), and the space complexity is O(1).

The second method uses a hash table to record the number of occurrences of each number, and finds all numbers that do not appear in the original array by traversing the hash table. The time complexity of this method is also O(n), but since the lookup operation of the hash table is at a constant level, the space complexity is O(n).

In practical applications, if the size of the input array is relatively small, the time complexity of the two methods is not much different, and you can choose either one; if the size of the input array is relatively large, the space complexity of the second method will be higher than that of The first method is high, so it may cause out-of-memory problems. In this case, you can consider using the first method to solve the problem.

 

Guess you like

Origin blog.csdn.net/m0_49914128/article/details/132015563
Recommended