1
+ {
2
+ "metadata" : {
3
+ "language_info" : {
4
+ "codemirror_mode" : {
5
+ "name" : " ipython" ,
6
+ "version" : 3
7
+ },
8
+ "file_extension" : " .py" ,
9
+ "mimetype" : " text/x-python" ,
10
+ "name" : " python" ,
11
+ "nbconvert_exporter" : " python" ,
12
+ "pygments_lexer" : " ipython3" ,
13
+ "version" : " 3.8.5-final"
14
+ },
15
+ "orig_nbformat" : 2 ,
16
+ "kernelspec" : {
17
+ "name" : " python3" ,
18
+ "display_name" : " Python 3.8.5 64-bit" ,
19
+ "metadata" : {
20
+ "interpreter" : {
21
+ "hash" : " 31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
22
+ }
23
+ }
24
+ }
25
+ },
26
+ "nbformat" : 4 ,
27
+ "nbformat_minor" : 2 ,
28
+ "cells" : [
29
+ {
30
+ "source" : [
31
+ " 找出数组中重复的数字。\n " ,
32
+ " \n " ,
33
+ " 在一个长度为 $n$ 的数组 `nums` 里的所有数字都在 0ドル〜n-1$ 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。\n " ,
34
+ " \n " ,
35
+ " 示例 1: \n " ,
36
+ " 输入:$[2, 3, 1, 0, 2, 5, 3]$ \n " ,
37
+ " 输出:2ドル$ 或 3ドル$ \n " ,
38
+ " \n " ,
39
+ " 限制: \n " ,
40
+ " 2ドル \\ leq n \\ leq 100000$\n " ,
41
+ " \n " ,
42
+ " 来源:力扣(LeetCode) \n " ,
43
+ " 链接:https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof \n " ,
44
+ " 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。"
45
+ ],
46
+ "cell_type" : " markdown" ,
47
+ "metadata" : {}
48
+ },
49
+ {
50
+ "source" : [
51
+ " ### 计数法\n " ,
52
+ " \n " ,
53
+ " 借助 [Counter 模块](https://matnoble.me/tech/programming/python/collections/#counter) `计数`,返回`计数`最多的数"
54
+ ],
55
+ "cell_type" : " markdown" ,
56
+ "metadata" : {}
57
+ },
58
+ {
59
+ "cell_type" : " code" ,
60
+ "execution_count" : 1 ,
61
+ "metadata" : {},
62
+ "outputs" : [],
63
+ "source" : [
64
+ " from collections import Counter \n " ,
65
+ " class Solution:\n " ,
66
+ " def findRepeatNumber(self, nums):\n " ,
67
+ " # 计数法\n " ,
68
+ " return Counter(nums).most_common(1)[0][0]"
69
+ ]
70
+ },
71
+ {
72
+ "cell_type" : " code" ,
73
+ "execution_count" : 2 ,
74
+ "metadata" : {},
75
+ "outputs" : [
76
+ {
77
+ "output_type" : " execute_result" ,
78
+ "data" : {
79
+ "text/plain" : [
80
+ " 2"
81
+ ]
82
+ },
83
+ "metadata" : {},
84
+ "execution_count" : 2
85
+ }
86
+ ],
87
+ "source" : [
88
+ " # 测试\n " ,
89
+ " nums = [2, 3, 1, 0, 2, 5, 3]\n " ,
90
+ " mat = Solution()\n " ,
91
+ " mat.findRepeatNumber(nums)"
92
+ ]
93
+ },
94
+ {
95
+ "source" : [
96
+ " ### 哈希表\n " ,
97
+ " \n " ,
98
+ " 用`哈希表`查找元素,时间复杂度$O(1)$"
99
+ ],
100
+ "cell_type" : " markdown" ,
101
+ "metadata" : {}
102
+ },
103
+ {
104
+ "cell_type" : " code" ,
105
+ "execution_count" : 3 ,
106
+ "metadata" : {},
107
+ "outputs" : [],
108
+ "source" : [
109
+ " class Solution:\n " ,
110
+ " def findRepeatNumber(self, nums):\n " ,
111
+ " # 哈希表\n " ,
112
+ " d = set()\n " ,
113
+ " for num in nums:\n " ,
114
+ " if num in d: return num\n " ,
115
+ " d.add(num)"
116
+ ]
117
+ },
118
+ {
119
+ "cell_type" : " code" ,
120
+ "execution_count" : 4 ,
121
+ "metadata" : {},
122
+ "outputs" : [
123
+ {
124
+ "output_type" : " execute_result" ,
125
+ "data" : {
126
+ "text/plain" : [
127
+ " 2"
128
+ ]
129
+ },
130
+ "metadata" : {},
131
+ "execution_count" : 4
132
+ }
133
+ ],
134
+ "source" : [
135
+ " # 测试\n " ,
136
+ " nums = [2, 3, 1, 0, 2, 5, 3]\n " ,
137
+ " mat = Solution()\n " ,
138
+ " mat.findRepeatNumber(nums)"
139
+ ]
140
+ },
141
+ {
142
+ "source" : [
143
+ " ### 原地置换 环\n " ,
144
+ " \n " ,
145
+ " <img src=\" https://cdn.jsdelivr.net/gh/MatNoble/Images/20210327105657.png\" width=400/>"
146
+ ],
147
+ "cell_type" : " markdown" ,
148
+ "metadata" : {}
149
+ },
150
+ {
151
+ "cell_type" : " code" ,
152
+ "execution_count" : 5 ,
153
+ "metadata" : {},
154
+ "outputs" : [],
155
+ "source" : [
156
+ " class Solution:\n " ,
157
+ " def findRepeatNumber(self, nums):\n " ,
158
+ " # 原地置换 环\n " ,
159
+ " i = 0\n " ,
160
+ " while i < len(nums):\n " ,
161
+ " if nums[i] == i: \n " ,
162
+ " i += 1\n " ,
163
+ " continue\n " ,
164
+ " if nums[nums[i]] == nums[i]: \n " ,
165
+ " return nums[i] # 返回环入口\n " ,
166
+ " # swap i <- -> j\n " ,
167
+ " j = nums[i]\n " ,
168
+ " nums[i], nums[j] = nums[j], nums[i]"
169
+ ]
170
+ },
171
+ {
172
+ "cell_type" : " code" ,
173
+ "execution_count" : 6 ,
174
+ "metadata" : {},
175
+ "outputs" : [
176
+ {
177
+ "output_type" : " execute_result" ,
178
+ "data" : {
179
+ "text/plain" : [
180
+ " 2"
181
+ ]
182
+ },
183
+ "metadata" : {},
184
+ "execution_count" : 6
185
+ }
186
+ ],
187
+ "source" : [
188
+ " # 测试\n " ,
189
+ " nums = [2, 3, 1, 0, 2, 5, 3]\n " ,
190
+ " mat = Solution()\n " ,
191
+ " mat.findRepeatNumber(nums)"
192
+ ]
193
+ }
194
+ ]
195
+ }
0 commit comments