LeetCodeEasy- [Interview Question 03. Repeated numbers in the array]

Find duplicate numbers in the array.


All numbers in an array nums of length n are in the range of 0 to n-1. Some numbers in the array are repeated, but I do not know how many numbers are repeated, nor how many times each number is repeated. Please find any duplicate number in the array.

Example 1:

Input:
[2, 3, 1, 0, 2, 5, 3]
Output: 2 or 3 

 

limit:

2 <= n <= 100000

Source: LeetCode (LeetCode)
link: https://leetcode-cn.com/problems/shu-zu-zhong-zhong-zhong-fu-de-shu-zi-lcof
copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

answer:

Idea 1: Array

Idea 2: Dictionary

Idea 3: set collection

Idea 4: In-place replacement algorithm


Idea 1: Array

You only need to find a repeated number, you can use an array to simulate, use the subscript to correspond to the number, the value in the array is used as the number of occurrences.

Both time and space complexity are O (n)

class Solution:
    def findRepeatNumber(self, nums: List[int]) -> int:
        length = len(nums)
        count = [0] * length
        for i in range(length):
            count[nums[i]] += 1
            if count[nums[i]] > 1:
                return nums[i]
        return -1

Idea 2: Dictionary

Just replace the above array with a dictionary, and you can still count the number of times.

Time complexity: O (n)

Space complexity: O (n), should be less than the above array.

class Solution:
    def findRepeatNumber(self, nums: List[int]) -> int:
        length = len(nums)
        count = {}
        for i in range(length):
            if nums[i] in count:
                return nums[i]
            else:
                count[nums[i]] = 1
        return -1

Idea 3: set collection

Replace the above array with a set collection, and determine whether it already exists with the collection.

Time complexity: O (n)

Space complexity: O (n)

class Solution:
    def findRepeatNumber(self, nums: List[int]) -> int:
        length = len(nums)
        count = set()
        for i in range(length):
            if nums[i] in count:
                return nums[i]
            else:
                count.add(nums[i])
        return -1

Idea 4: In-place replacement algorithm

This method is very clever, based on the topic: the value in the array of size N is in [0, n-1], then if the array is not repeated, you should be able to correspond to a number in the table below The corresponding is the subscript i = num [i]. So we can use this point to judge, if a certain subscript corresponds to two values, then there are duplicate numbers.

Processing method: It is to traverse the array, adjust the order, match i and nums [i], if i! = Nums [i] are not equal, you need to put nums [i] into the subscript nums [i] Position, and replace the value with the index nums [i] to the position with the index i, if i is not equal to nums [i] at this time, continue the above operation.

Examples are as follows:

Table below: 0 1 2 3

Value: 1 3 0 2

You can comprehend it manually by following the calculation method above.

The ingenious point of this question is that no additional storage space is needed, and the time is basically the same as above, and a sorting method under a specific environment is provided .

Time complexity: O (n)

Space complexity: O (1)

class Solution:
    def findRepeatNumber(self, nums: List[int]) -> int:
        length = len(nums)
        # 原地交换
        for i in range(length):
            while i != nums[i]: # 说明num[i]应该放到下标为num[i]的位置
                if nums[nums[i]] == nums[i]: # 该位置已经匹配好了,则说明存在重复
                    return nums[i]
                t = nums[nums[i]]
                nums[nums[i]] = nums[i]
                nums[i] = t
        return -1

 

Published 314 original articles · 22 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/qq_39451578/article/details/105306594