-
Notifications
You must be signed in to change notification settings - Fork 2
/
WGki4K.rs
65 lines (59 loc) · 1.38 KB
/
WGki4K.rs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {
assert_eq!(Solution::single_number(vec![2, 2, 3, 2]), 3);
assert_eq!(Solution::single_number(vec![0, 1, 0, 1, 0, 1, 100]), 100);
assert_eq!(
Solution::single_number(vec![-2, -2, 1, 1, 4, 1, 4, 4, -4, -2]),
-4
);
assert_eq!(
Solution::single_number(vec![
43,
16,
45,
89,
45,
-2147483648,
45,
2147483646,
-2147483647,
-2147483648,
43,
2147483647,
-2147483646,
-2147483648,
89,
-2147483646,
89,
-2147483646,
-2147483647,
2147483646,
-2147483647,
16,
16,
2147483646,
43
]),
2147483647
);
}
struct Solution;
impl Solution {
pub fn single_number(nums: Vec<i32>) -> i32 {
let mut v = [0; std::mem::size_of::<i32>() * 8];
for mut i in nums {
for j in 0..32 {
if (i >> j) & 1 == 1 {
v[j] = (v[j] + 1) % 3;
}
}
}
let mut ans = 0;
for i in 0..v.len() {
if v[i] == 1 {
ans += (1 << i);
}
}
ans
}
}