-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle-number.py
More file actions
48 lines (37 loc) · 1.05 KB
/
single-number.py
File metadata and controls
48 lines (37 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
"""
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
"""
from collections import Counter
class Solution:
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
>>> nums = [2,2,1]
>>> s = Solution()
>>> s.singleNumber(nums)
1
>>> nums = [4,1,2,1,2]
>>> s.singleNumber(nums)
4
"""
counter = Counter(nums)
for k, v in counter.items():
if v == 1:
return k
# 这种是不聪明的
# 聪明的是下面这种
# 取原数据集的集合的值乘以2,刚好会比原数据集多出不重复整数的值
# return sum(set(nums)) * 2 - sum(nums)
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)