Skip to content

Commit 42afc82

Browse files
committed
add 136
1 parent 2ec908a commit 42afc82

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
| 122 | [Best Time to Buy and Sell Stock II][122] |
2121
| 189 | [Rotate Array][189] |
2222
| 217 | [Contains Duplicate][217] |
23-
23+
| 136 | [Single Number][136] |
2424

2525
[leetcode]: https://leetcode.com/problemset/all/
2626
[blankj]: https://github.com/Blankj/awesome-java-leetcode
@@ -31,3 +31,4 @@
3131
[122]: https://github.com/andavid/leetcode-java/blob/master/note/122/README.md
3232
[189]: https://github.com/andavid/leetcode-java/blob/master/note/189/README.md
3333
[217]: https://github.com/andavid/leetcode-java/blob/master/note/217/README.md
34+
[136]: https://github.com/andavid/leetcode-java/blob/master/note/136/README.md

note/136/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Single Number][title]
2+
3+
## Description
4+
5+
Given an array of integers, every element appears twice except for one. Find that single one.
6+
7+
Note:
8+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
9+
10+
## 思路
11+
12+
给定一个数组,数组中除了一个数只出现了一次,其他数都出现了两次,找出那个数。
13+
14+
把所有的数字进行异或运算即可。
15+
16+
17+
## [完整代码][src]
18+
19+
```java
20+
class Solution {
21+
public int singleNumber(int[] nums) {
22+
int n = 0;
23+
for (int num : nums) {
24+
n ^= num;
25+
}
26+
return n;
27+
}
28+
}
29+
```
30+
31+
[title]: https://leetcode.com/problems/single-number
32+
[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_136/Solution.java
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int singleNumber(int[] nums) {
3+
int n = 0;
4+
for (int num : nums) {
5+
n ^= num;
6+
}
7+
return n;
8+
}
9+
10+
public static void main(String[] args) {
11+
Solution solution = new Solution();
12+
System.out.println(solution.singleNumber(new int[]{1, 2, 1}));
13+
}
14+
}

0 commit comments

Comments
 (0)