Skip to content

Commit 0bef15e

Browse files
committed
feat: add python solution to leetcode problem: 0136. Single Number
1 parent f56936f commit 0bef15e

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

solution/0100-0199/0136.Single Number/README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,37 @@
2626
## 解法
2727
<!-- 这里可写通用的实现逻辑 -->
2828

29+
异或运算求解。
30+
31+
首先明确,两个相同的数异或之后的结果为 0。对该数组所有元素进行异或运算,结果就是那个只出现一次的数字。
2932

3033
<!-- tabs:start -->
3134

3235
### **Python3**
3336
<!-- 这里可写当前语言的特殊实现逻辑 -->
3437

3538
```python
36-
39+
class Solution:
40+
def singleNumber(self, nums: List[int]) -> int:
41+
res = 0
42+
for num in nums:
43+
res ^= num
44+
return res
3745
```
3846

3947
### **Java**
4048
<!-- 这里可写当前语言的特殊实现逻辑 -->
4149

4250
```java
43-
51+
class Solution {
52+
public int singleNumber(int[] nums) {
53+
int res = 0;
54+
for (int num : nums) {
55+
res ^= num;
56+
}
57+
return res;
58+
}
59+
}
4460
```
4561

4662
### **...**

solution/0100-0199/0136.Single Number/README_EN.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,26 @@
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def singleNumber(self, nums: List[int]) -> int:
57+
res = 0
58+
for num in nums:
59+
res ^= num
60+
return res
5661
```
5762

5863
### **Java**
5964

6065
```java
61-
66+
class Solution {
67+
public int singleNumber(int[] nums) {
68+
int res = 0;
69+
for (int num : nums) {
70+
res ^= num;
71+
}
72+
return res;
73+
}
74+
}
6275
```
6376

6477
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
class Solution:
2-
def singleNumber(self, nums):
3-
"""
4-
:type nums: List[int]
5-
:rtype: int
6-
"""
7-
res=0
8-
for i in nums:
9-
res = res^i
10-
return res
2+
def singleNumber(self, nums: List[int]) -> int:
3+
res = 0
4+
for num in nums:
5+
res ^= num
6+
return res

0 commit comments

Comments
 (0)