Skip to content

Commit d9bf519

Browse files
authored
Update 0136-Single-Number.md
添加C、C++、Java、python代码实现
1 parent 3daa187 commit d9bf519

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

0136-Single-Number/Article/0136-Single-Number.md

+57-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,62 @@
5151

5252
![](../Animation/136.gif)
5353

54+
### 代码实现
55+
#### C
56+
````c
57+
int singleNumber(int* nums, int numsSize){
58+
int res=0;
59+
for(int i=0;i<numsSize;i++)
60+
{
61+
res ^= nums[i];
62+
}
63+
64+
return res;
65+
}
66+
````
67+
68+
#### C++
69+
````c++
70+
class Solution {
71+
public:
72+
int singleNumber(vector<int>& nums) {
73+
int res=0;
74+
for(auto n:nums)
75+
{
76+
// 异或
77+
res ^= n;
78+
}
79+
return res;
80+
}
81+
};
82+
````
83+
84+
#### Java
85+
````java
86+
class Solution {
87+
public int singleNumber(int[] nums) {
88+
int res = 0;
89+
for(int n:nums)
90+
{
91+
// 异或
92+
res ^= n;
93+
}
94+
return res;
95+
}
96+
}
97+
````
98+
99+
#### pyton
100+
````python
101+
class Solution(object):
102+
def singleNumber(self, nums):
103+
return reduce(lambda x,y:x^y, nums)
104+
# reduce用法举例
105+
# 计算列表和:1+2+3+4+5
106+
# 使用 lambda 匿名函数
107+
# reduce(lambda x, y: x+y, [1,2,3,4,5])
108+
````
109+
54110
### 进阶版
55111

56112
有一个 n 个元素的数组,除了两个数只出现一次外,其余元素都出现两次,让你找出这两个只出现一次的数分别是几,要求时间复杂度为 O(n) 且再开辟的内存空间固定(与 n 无关)。
@@ -83,4 +139,4 @@
83139

84140

85141

86-
![](../../Pictures/qrcode.jpg)
142+
![](../../Pictures/qrcode.jpg)

0 commit comments

Comments
 (0)