Skip to content

Commit 4971e8a

Browse files
committed
single number ii
1 parent 6605ba9 commit 4971e8a

File tree

3 files changed

+78
-33
lines changed

3 files changed

+78
-33
lines changed

.idea/workspace.xml

Lines changed: 40 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,4 @@ codes may not be optimized
388388
1. [Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/description/)
389389
1. [Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/description/)
390390
1. [Maximum Product of Word Length](https://leetcode.com/problems/maximum-product-of-word-lengths/description/)
391+
1. [Single Number II](https://leetcode.com/problems/single-number-ii/description/)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.chen0040.leetcode.day18.medium;
2+
3+
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
8+
/**
9+
* Created by xschen on 13/8/2017.
10+
*
11+
* link: https://leetcode.com/problems/single-number-ii/description/
12+
*/
13+
public class SingleNumberII {
14+
public class Solution {
15+
public int singleNumber(int[] nums) {
16+
Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
17+
for(int i=0; i < nums.length; ++i) {
18+
int num = nums[i];
19+
if(counts.containsKey(num)) {
20+
int count = counts.get(num) - 1;
21+
if(count == 0) {
22+
counts.remove(num);
23+
} else {
24+
counts.put(num, count);
25+
}
26+
} else {
27+
counts.put(num, 2);
28+
}
29+
}
30+
31+
for(Integer num : counts.keySet()) {
32+
return num;
33+
}
34+
return -1;
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)