Skip to content

Commit 9e9ae6a

Browse files
Add files via upload
1 parent 1db8baf commit 9e9ae6a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Single Number/Single_Number.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 第一种思路,使用数学
2+
# 60ms 28.31%
3+
class Solution:
4+
def singleNumber(self, nums):
5+
"""
6+
:type nums: List[int]
7+
:rtype: int
8+
"""
9+
return 2 * sum(set(nums)) - sum(nums)
10+
11+
# 第二种思路,异或,线性的时间复杂度
12+
# 异或是满足交互律的
13+
# 40ms 99.09%
14+
class Solution:
15+
def singleNumber(self, nums):
16+
"""
17+
:type nums: List[int]
18+
:rtype: int
19+
"""
20+
temp = 0
21+
for num in nums:
22+
temp ^= num
23+
return temp

0 commit comments

Comments
 (0)