Skip to content

Commit

Permalink
Merge pull request MisterBooo#112 from ztianming/patch-5
Browse files Browse the repository at this point in the history
Update 0136-Single-Number.md
  • Loading branch information
MisterBooo authored Jul 31, 2020
2 parents 3daa187 + d9bf519 commit 175bbe4
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion 0136-Single-Number/Article/0136-Single-Number.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,62 @@

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

### 代码实现
#### C
````c
int singleNumber(int* nums, int numsSize){
int res=0;
for(int i=0;i<numsSize;i++)
{
res ^= nums[i];
}

return res;
}
````

#### C++
````c++
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res=0;
for(auto n:nums)
{
// 异或
res ^= n;
}
return res;
}
};
````

#### Java
````java
class Solution {
public int singleNumber(int[] nums) {
int res = 0;
for(int n:nums)
{
// 异或
res ^= n;
}
return res;
}
}
````

#### pyton
````python
class Solution(object):
def singleNumber(self, nums):
return reduce(lambda x,y:x^y, nums)
# reduce用法举例
# 计算列表和1+2+3+4+5
# 使用 lambda 匿名函数
# reduce(lambda x, y: x+y, [1,2,3,4,5])
````

### 进阶版

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



![](../../Pictures/qrcode.jpg)
![](../../Pictures/qrcode.jpg)

0 comments on commit 175bbe4

Please sign in to comment.