mirror of
https://gitee.com/TheAlgorithms/LeetCodeAnimation.git
synced 2024-12-31 15:25:33 +08:00
Update 0137-Single-Number-II.md
添加C++、Java、Python代码实现
This commit is contained in:
parent
f43c0860f7
commit
5c8e809de0
@ -46,5 +46,45 @@
|
|||||||
|
|
||||||
![](../Animation/137.gif)
|
![](../Animation/137.gif)
|
||||||
|
|
||||||
|
### 代码实现
|
||||||
|
#### C++
|
||||||
|
```c++
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int singleNumber(vector<int>& nums) {
|
||||||
|
int one=0, two=0;
|
||||||
|
for(int n:nums)
|
||||||
|
{
|
||||||
|
one = (one ^ n) & (~two);
|
||||||
|
two = (two ^ n) & (~one);
|
||||||
|
}
|
||||||
|
return one;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
#### Java
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int singleNumber(int[] nums) {
|
||||||
|
int one=0, two=0;
|
||||||
|
for(int n:nums)
|
||||||
|
{
|
||||||
|
one = (one ^ n) & (~two);
|
||||||
|
two = (two ^ n) & (~one);
|
||||||
|
}
|
||||||
|
return one;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
#### Python
|
||||||
|
```python
|
||||||
|
class Solution(object):
|
||||||
|
def singleNumber(self, nums):
|
||||||
|
one = two = 0
|
||||||
|
for n in nums:
|
||||||
|
one = (one ^ n) & (~two)
|
||||||
|
two = (two ^ n) & (~one)
|
||||||
|
return one
|
||||||
|
```
|
||||||
|
|
||||||
![](../../Pictures/qrcode.jpg)
|
![](../../Pictures/qrcode.jpg)
|
||||||
|
Loading…
Reference in New Issue
Block a user