Skip to content

Commit e3be701

Browse files
refactor 136
1 parent dfc70a9 commit e3be701

File tree

1 file changed

+31
-27
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+31
-27
lines changed

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Set;
66

77
/**136. Single Number
8+
89
Given an array of integers, every element appears twice except for one. Find that single one.
910
1011
Note:
@@ -13,34 +14,37 @@
1314

1415
public class _136 {
1516

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-
}
26-
}
27-
Iterator<Integer> it = set.iterator();
28-
return it.next();
17+
public static class Solution1 {
18+
/**
19+
* Approach 1: use set, since this problem explicitly states that every element appears twice
20+
* and only one appears once so, we could safely remove the ones that are already in the set,
21+
* O(n) time and O(n) space. HashTable approach works similarly like this one, but it could be
22+
* more easily extend to follow-up questions.
23+
*/
24+
public int singleNumber(int[] nums) {
25+
Set<Integer> set = new HashSet();
26+
for (int i : nums) {
27+
if (!set.add(i)) {
28+
set.remove(i);
2929
}
30+
}
31+
Iterator<Integer> it = set.iterator();
32+
return it.next();
3033
}
31-
32-
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;
43-
}
34+
}
35+
36+
public static class Solution2 {
37+
/**
38+
* Approach 2: bit manipulation, use exclusive or ^ to solve this problem: we're using the trick
39+
* here: every number ^ itself will become zero, so, the only remaining element will be the one
40+
* that appeared only once.
41+
*/
42+
public int singleNumber(int[] nums) {
43+
int res = 0;
44+
for (int i : nums) {
45+
res ^= i;
46+
}
47+
return res;
4448
}
45-
49+
}
4650
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy