Skip to content

Commit 064df49

Browse files
committed
Single Number
1 parent ea407ef commit 064df49

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Single_Number.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Given a non-empty array of integers, every element appears twice except for one. Find that single one.
2+
#
3+
# Note:
4+
#
5+
# Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
6+
#
7+
# Example 1:
8+
#
9+
# Input: [2,2,1]
10+
# Output: 1
11+
# Example 2:
12+
#
13+
# Input: [4,1,2,1,2]
14+
# Output: 4
15+
16+
17+
class Solution:
18+
def singleNumber(self, nums):
19+
res = 0
20+
for num in nums:
21+
res = res ^ num
22+
23+
return res

0 commit comments

Comments
 (0)