-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
141 lines (126 loc) · 3.49 KB
/
main.cpp
File metadata and controls
141 lines (126 loc) · 3.49 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Source: https://leetcode.com/problems/single-number-ii
// Title: Single Number II
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an integer array `nums` where every element appears **three times** except for one, which appears **exactly once**. Find the single element and return it.
//
// You must implement a solution with a linear runtime complexity and useonly constant extra space.
//
// **Example 1:**
//
// ```
// Input: nums = [2,2,3,2]
// Output: 3
// ```
//
// **Example 2:**
//
// ```
// Input: nums = [0,1,0,1,0,1,99]
// Output: 99
// ```
//
// **Constraints:**
//
// - `1 <= nums.length <= 3 * 10^4`
// - `-2^31 <= nums[i] <= 2^31 - 1`
// - Each element in `nums` appears exactly **three times** except for one element which appears **once**.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <cstdint>
#include <vector>
using namespace std;
// Bit Count
class Solution {
public:
int singleNumber(const vector<int>& nums) {
int ans = 0;
for (int i = 0; i < 32; i++) {
int count = 0;
uint32_t bit = 1u << i;
for (const int num : nums) {
if (bit & num) count++;
}
if (count % 3) ans |= bit;
}
return ans;
}
};
// Bit Manipulation
//
// Use `once` for bits that occurs once (mod 3).
// Use `twice` for bits that occurs twice (mod 3).
// Note that once are twice are mutually exclusive.
//
// For each bit,
// num=0 => keep old value
// num=1, once=0, twice=0 => once=1, twice=0
// num=1, once=1, twice=0 => once=0, twice=1
// num=1, once=0, twice=1 => once=0, twice=0
class Solution2 {
public:
int singleNumber(vector<int>& nums) {
int once = 0;
int twice = 0;
for (auto num : nums) {
int onceNext = (~num & once) | (num & ~once & ~twice);
int twiceNext = (~num & twice) | (num & once & ~twice);
once = onceNext, twice = twiceNext;
}
return once;
}
};
// Bit Manipulation
//
// Use `once` for bits that occurs once (mod 3).
// Use `twice` for bits that occurs twice (mod 3).
// Note that once are twice are mutually exclusive.
//
// In the following, we fucus on a single bit.
//
// To update `once`:
// Use `num ^ once` to update, meanwhile `twice` must be 0.
//
// To update `twice`:
// Use `num ^ twice` to update. meanwhile `once` must be 1.
// However, we already updated `once`. Therefore `once` must be 0 now.
//
// Therefore,
// once = (num ^ once) & ~twice
// twice = (num ^ twice) & ~once
class Solution3 {
public:
int singleNumber(vector<int>& nums) {
int once = 0;
int twice = 0;
for (int num : nums) {
once = (num ^ once) & ~twice;
twice = (num ^ twice) & ~once;
}
return once;
}
};
// Bit Manipulation
//
// Note that (A ^ B) & C = (A ^ C) & (B ^ C).
//
// First we have `(once ^ num) & ~twice = (once & ~twice) ^ (num & ~twice)`.
// Since `once` and `twice` are mutually exclusive, we have `once & ~twice = once`.
// Therefore `once <- once ^ (num & ~twice)`.
//
// Similarly, we have `(twice ^ num) & ~once = (twice & ~once) ^ (num & ~once)`.
// Also, we have `twice & ~once = twice`
// Therefore `twice <- twice ^ (num & ~once)`.
class Solution4 {
public:
int singleNumber(vector<int>& nums) {
int once = 0;
int twice = 0;
for (int num : nums) {
once ^= num & ~twice;
twice ^= num & ~once;
}
return once;
}
};