Skip to content

Commit 4e8f267

Browse files
committed
Add solution 136
1 parent 0b6eec6 commit 4e8f267

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Complete solutions to Leetcode problems, updated daily.
2424
| 053 | [Maximum Subarray](https://github.com/yanglbme/leetcode/tree/master/solution/053.Maximum%20Subarray) | `Array`, `Divide and Conquer`, `Dynamic Programming` |
2525
| 070 | [Climbing Stairs](https://github.com/yanglbme/leetcode/tree/master/solution/070.Climbing%20Stairs) | `Dynamic Programming` |
2626
| 083 | [Remove Duplicates from Sorted List](https://github.com/yanglbme/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List) | `Linked List` |
27+
| 136 | [Single Number](https://github.com/yanglbme/leetcode/tree/master/solution/136.Single%20Number) | `Hash Table`, `Bit Manipulation` |
2728
| 189 | [Rotate Array](https://github.com/yanglbme/leetcode/tree/master/solution/189.Rotate%20Array) | `Array` |
2829
| 198 | [House Robber](https://github.com/yanglbme/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
2930
| 203 | [Remove Linked List Elements](https://github.com/yanglbme/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |

solution/136.Single Number/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## 只出现一次的数字
2+
### 题目描述
3+
4+
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
5+
6+
说明:
7+
8+
你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
9+
10+
示例 1:
11+
```
12+
输入: [2,2,1]
13+
输出: 1
14+
```
15+
16+
示例 2:
17+
```
18+
输入: [4,1,2,1,2]
19+
输出: 4
20+
```
21+
22+
### 解法
23+
任意数与 0 异或,都等于它本身。而任意数与自身异或,都等于 0。
24+
25+
```java
26+
class Solution {
27+
public int singleNumber(int[] nums) {
28+
int res = 0;
29+
for (int num : nums) {
30+
res ^= num;
31+
}
32+
return res;
33+
}
34+
}
35+
```
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int singleNumber(int[] nums) {
3+
int res = 0;
4+
for (int num : nums) {
5+
res ^= num;
6+
}
7+
return res;
8+
}
9+
}

0 commit comments

Comments
 (0)