You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/com/fishercoder/solutions/_260.java
+6-9Lines changed: 6 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -3,10 +3,7 @@
3
3
importjava.util.HashMap;
4
4
importjava.util.Map;
5
5
6
-
/**260. Single Number III QuestionEditorial Solution My Submissions
7
-
Total Accepted: 42536
8
-
Total Submissions: 92175
9
-
Difficulty: Medium
6
+
/**260. Single Number III
10
7
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
11
8
12
9
For example:
@@ -17,17 +14,16 @@
17
14
The order of the result is not important. So in the above example, [5, 3] is also correct.
18
15
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
19
16
*/
20
-
publicclassSingleNumberIII {
21
-
// TODO: study its bit manipulation way, this is a MUST!
22
-
23
-
17
+
18
+
publicclass_260 {
19
+
24
20
//Approach 1: normal hashmap
25
21
publicint[] singleNumber(int[] nums) {
26
22
Map<Integer, Integer> map = newHashMap();
27
23
for(inti : nums){
28
24
map.put(i, map.getOrDefault(i, 0)+1);
29
25
}
30
-
26
+
31
27
int[] res = newint[2];
32
28
intindex = 0;
33
29
for(intkey : map.keySet()){
@@ -36,4 +32,5 @@ public int[] singleNumber(int[] nums) {
36
32
}
37
33
returnres;
38
34
}
35
+
// TODO: study its bit manipulation way, this is a MUST!
0 commit comments