-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleNumberII.java
More file actions
39 lines (39 loc) · 919 Bytes
/
SingleNumberII.java
File metadata and controls
39 lines (39 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package medium;
/**
* ClassName: SingleNumberII.java
* Author: chenyiAlone
* Create Time: 2019/5/23 16:43
* Description: No.137
* 思路:
* 1. 统计每个位置的个数,如果是 cnt % 3 == 1 就证明出现一个次的数个这位为 1
* 2. res |= 1 << i
*
*
*
*
* Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
*
* Note:
*
* Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
*
* Example 1:
*
* Input: [2,2,3,2]
* Output: 3
* Example 2:
*
* Input: [0,1,0,1,0,1,99]
* Output: 99
*
*/
public class SingleNumberII {
public int singleNumber(int[] nums) {
int ones = 0, twos = 0;
for(int x : nums){
ones = (ones ^ x) & ~twos;
twos = (twos ^ x) & ~ones;
}
return ones;
}
}