Skip to content

Commit 765a293

Browse files
committed
Single Number
1 parent d05b550 commit 765a293

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

136 Single Number.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Given an array of integers, every element appears twice except for one. Find that single one.
3+
4+
Note:
5+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
6+
'''
7+
8+
class Solution(object):
9+
def singleNumber(self, nums):
10+
"""
11+
:type nums: List[int]
12+
:rtype: int
13+
"""
14+
result = nums[0]
15+
for i in nums[1:]:
16+
result ^= i
17+
return result
18+
19+
20+
if __name__ == "__main__":
21+
assert Solution().singleNumber([1, 2, 3, 4, 3, 2, 1]) == 4

0 commit comments

Comments
 (0)