剑指offer - 数组中重复的数字(python)

题目:

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

 

一个比较简单的思路:输入数组a,一个空数组b,判断a中数值未存在于b中时,把值传入b中;存在于b中则返回该数值。

# -*- coding:utf-8 -*-
class Solution:
    # 这里要特别注意~找到任意重复的一个值并赋值到duplication[0]
    # 函数返回True/False
    def duplicate(self, numbers, duplication):
        # write code here
        data2 = []
        for i in numbers:
            if i in data2:
                duplication[0] = i
                return True
            else:
                data2 += [i]
        return False

运行时间:27ms

占用内存:5624k

 

以下来自转载:

————————————————
版权声明:下文为优快云博主「single-coder」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/dake1994/article/details/79856640

 

方法1:

先把输入的数组排序,再从头到尾扫描排序后的数组,如果相邻的两个元素相等,则存在重复数字。时间复杂度O(nlogn).

# -*- coding:utf-8 -*-
class Solution:
    # 这里要特别注意~找到任意重复的一个值并赋值到duplication[0]
    # 函数返回True/False
    def duplicate(self, numbers, duplication):
        # write code here
        numbers = sorted(numbers)
        for i in range(1, len(numbers)):
            if numbers[i] == numbers[i - 1]:
                duplication[0] = numbers[i]
                return True
        return False

运行时间:22ms

占用内存:5836k

 

方法2:

利用哈希表来解决。从头到尾扫描数组的每个元素,每扫描到一个元素,都可以用O(1)的时间来判断哈希表中是否已经存在该数字,如果存在,说明找到了重复元素,如果不存在,则将其加入到哈希表中。时间复杂度O(n),空间复杂度O(n)。

# -*- coding:utf-8 -*-
class Solution:
    # 这里要特别注意~找到任意重复的一个值并赋值到duplication[0]
    # 函数返回True/False
    def duplicate(self, numbers, duplication):
        # write code here
        hash_table = [None] * len(numbers)
        for i in range(len(numbers)):
            hash_value = numbers[i]
            if hash_table[hash_value]:
                duplication[0] = hash_value
                return True
            else:
                hash_table[hash_value] = hash_value
        return False

运行时间:29ms

占用内存:5736k

 

————————————————

思路原文链接:https://blog.nowcoder.net/n/5250b6e4226446f6862d3e66d3753fd4

代码原文链接:https://blog.youkuaiyun.com/dake1994/article/details/79856640

方法3:

要求时间复杂度 O(N),空间复杂度 O(1)。因此不能使用排序的方法,也不能使用额外的标记数组。

对于这种数组元素在 [0, n-1] 范围内的问题,可以将值为 i 的元素调整到第 i 个位置上进行求解。本题要求找出重复的数字,因此在调整过程中,如果第 i 位置上已经有一个值为 i 的元素,就可以知道 i 值重复。

以 (2, 3, 1, 0, 2, 5) 为例,遍历到位置 4 时,该位置上的数为 2,但是第 2 个位置上已经有一个 2 的值了,因此可以知道 2 重复:

# -*- coding:utf-8 -*-
class Solution:
    # 这里要特别注意~找到任意重复的一个值并赋值到duplication[0]
    # 函数返回True/False
    def duplicate(self, numbers, duplication):
        # write code here
        for i in range(len(numbers)):
            while numbers[i] != i:
                if numbers[numbers[i]] == numbers[i]:
                    duplication[0] = numbers[i]
                    return True
                else:
                    numbers[numbers[i]], numbers[i] = numbers[i], numbers[numbers[i]]
        return False

运行时间:23ms

占用内存:5732k

————————————————

range两种使用方法(来自菜鸟):

>>>range(5)

range(0, 5)

>>> for i in range(5):

... print(i)

...

0

1

2

3

4

>>> list(range(5))

[0, 1, 2, 3, 4]

>>> list(range(0))

[]

有两个参数或三个参数的情况(第二种构造方法):

>>>list(range(0, 30, 5))

[0, 5, 10, 15, 20, 25]

>>> list(range(0, 10, 2))

[0, 2, 4, 6, 8]

>>> list(range(0, -10, -1))

[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

>>> list(range(1, 0))

[]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值