A brush up "to prove safety Offer" - does not modify the array to find duplicate numbers (ideas and Python implementation)

Array duplicate numbers

On a blog "to prove safety Offer" - a topic: find duplicate digital array (Python accomplished in many ways) , the purpose of such practice can be found in fact that is the key element of the array while traversing the side to check the condition of .

Then we blog in the most sophisticated way to learn array (Python dynamic arrays) this blog describes the nature of the structure of the array, and yourself implements a dynamic array.

Today we introduce another channel face questions about the array from "prove safety Offer" - the array is not modified to identify duplicate numbers.

Do not modify the array to find duplicate numbers

Topic two: do not modify the array to find duplicate numbers

Given an array of length n + 1 in all of the numbers are within the scope 0~n, so there is a numeric array of at least duplicate.

Please find an array of any one of the duplicate numbers, but can not modify the input array.

Example:

A given length of the array nums 8 = [2, 3, 5, 4, 3, 2, 6,7]

The output duplicate numbers 2 or 3

Thinking

First we have to focus, questions asked is: do not modify the array, and then still 返回任意一个重复的数字. So in terms of problem-solving ideas compared to fewer:

  1. Hash tables: to keep up with a title like this question can also create a hash table, each number of the original array if the first time, they took him into the hash table to go, that is, the original array size of m numbers hash table should be placed in the position index of the m. Space complexity is \ (O (n-) \) .

  2. Dichotomy: Is there no space complexity (O (n) \) \ algorithm. Assuming no number of repetitions, then between 1 ~ n, each number appears only once. The title, the digital array has at least one repeat, i.e., the number of occurrence is greater than 1.

    Using dichotomous thought of: 1 ~ n is a number from intermediate number m starts divided into two parts, the front half 1 ~ m , the rear half m. 1 n-+ ~ , if the number of digital 1 ~ m appearances in the array is greater than m, then it must be a half duplicate numbers; otherwise, it must contain the other repeating part numbers. Then we continue to repeat the interval containing the numbers into two, until you find duplicate numbers.

A thought: hash table

def find_duplicated_num(nums):
    """hash_map"""
    hash_map = dict()
    for i, val in enumerate(nums):
        if val in hash_map:
            return val
        hash_map[val] = i
    return False

Thinking two: the dichotomy

def reduce_inter(nums2, left, right):
    """ """
    mid = (left + right) // 2
    count = 0
    length = len(nums2)
    for i in range(length):
        if (nums2[i] >= left) and (nums2[i] <= mid):
            count += 1
    if count > mid - left + 1:
        return left, mid
    else:
        return mid+1, right


def find_duplicated_num2(nums2):
    left, right = 1, len(nums2) - 1
    while left != right:
        left, right = reduce_inter(nums2, left, right)
    return left

test

nums = [2, 3, 5, 4, 3, 2, 6, 7]
# nums_n = [5, 4, 3, 2, 6, 7]
print("思路一测试结果: ", find_duplicated_num(nums))
print("思路二测试结果: ", find_duplicated_num2(nums))

result

思路一测试结果:  3
思路二测试结果:  3

to sum up

In fact, this algorithm is not guaranteed to find all duplicate numbers, such as not find out [2, 3, 5, 4, 3, 2, 6, 7] Repeat number 2.

Guess you like

Origin www.cnblogs.com/yuzhou-1su/p/11784181.html