Skip to content

Commit 2b4533d

Browse files
[N-0] refactor 136
1 parent 00ef0cf commit 2b4533d

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ Your ideas/fixes/algorithms are more than welcome!
510510
|139|[Word Break](https://leetcode.com/problems/word-break/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_139.java)| O(n^2)|O(n) | Medium| DP, Pruning
511511
|138|[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_138.java)| O(n)|O(n) | Medium| LinkedList, HashMap
512512
|137|[Single Number II](https://leetcode.com/problems/single-number-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_137.java)| O(n)|O(n) | Medium|
513-
|136|[Single Number](https://leetcode.com/problems/single-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_136.java)| O(n)|O(n) | Medium|
513+
|136|[Single Number](https://leetcode.com/problems/single-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_136.java)| O(n)|O(1) | Easy | Bit Manipulation
514514
|135|[Candy](https://leetcode.com/problems/candy/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_135.java)| O(n)|O(1) | Hard| Greedy
515515
|134|[Gas Station](https://leetcode.com/problems/gas-station/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_134.java)| O(n)|O(1) | Medium| Greedy
516516
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_133.java)| O(n)|O(n) | Medium| HashMap, BFS, Graph

src/main/java/com/fishercoder/solutions/_136.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,38 @@
99
1010
Note:
1111
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
12-
1312
*/
1413

1514
public class _136 {
16-
//approach 1: use set, since this problem explicitly states that every element appears twice and only one appears once
17-
//so, we could safely remove the ones that are already in the set, O(n) time and O(n) space.
18-
//HashTable approach works similarly like this one, but it could be more easily extend to follow-up questions.
19-
public int singleNumber_using_set(int[] nums) {
20-
Set<Integer> set = new HashSet();
21-
for (int i : nums) {
22-
if (!set.add(i)) {
23-
set.remove(i);
15+
16+
public static class Solution1 {
17+
/**approach 1: use set, since this problem explicitly states that every element appears twice and only one appears once
18+
so, we could safely remove the ones that are already in the set, O(n) time and O(n) space.
19+
HashTable approach works similarly like this one, but it could be more easily extend to follow-up questions.*/
20+
public int singleNumber(int[] nums) {
21+
Set<Integer> set = new HashSet();
22+
for (int i : nums) {
23+
if (!set.add(i)) {
24+
set.remove(i);
25+
}
2426
}
27+
Iterator<Integer> it = set.iterator();
28+
return it.next();
2529
}
26-
Iterator<Integer> it = set.iterator();
27-
return it.next();
2830
}
2931

3032

31-
//approach 2: bit manipulation, use exclusive or ^ to solve this problem:
32-
//we're using the trick here: every number ^ itself will become zero, so, the only remaining element will be the one that
33-
//appeared only once.
34-
public int singleNumber_using_bit(int[] nums) {
35-
int res = 0;
36-
for (int i : nums) {
37-
res ^= i;
33+
public static class Solution2 {
34+
/**approach 2: bit manipulation, use exclusive or ^ to solve this problem:
35+
we're using the trick here: every number ^ itself will become zero, so, the only remaining element will be the one that
36+
appeared only once.*/
37+
public int singleNumber(int[] nums) {
38+
int res = 0;
39+
for (int i : nums) {
40+
res ^= i;
41+
}
42+
return res;
3843
}
39-
return res;
4044
}
4145

4246
}

0 commit comments

Comments
 (0)