File tree Expand file tree Collapse file tree 2 files changed +23
-13
lines changed
lcof2/剑指 Offer II 004. 只出现一次的数字 Expand file tree Collapse file tree 2 files changed +23
-13
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < vector>
3
+ using namespace std ;
4
+
1
5
class Solution {
2
6
public:
3
7
int singleNumber (vector<int >& nums) {
@@ -18,24 +22,30 @@ class Solution {
18
22
public:
19
23
int singleNumber (vector<int >& nums) {
20
24
vector<int > array (32 );
25
+ array.assign (array.size (), 0 );
21
26
int ret = 0 ;
22
- for (int i = 0 ; i < nums.size (); i++)
23
- {
24
- for (int j = 0 ; j < 32 ; j++)
25
- {
26
- if (nums[i] & (1 << j))
27
- {
28
- array[j]++;
29
- }
30
- }
31
- }
32
27
for (int i = 0 ; i < 32 ; i++)
33
28
{
34
- if (array[i] % 3 )
29
+ for ( int j = 0 ; j < nums. size (); j++ )
35
30
{
36
- ret += ((array[i] % 3 ) << i) ;
31
+ array[i] += (nums[j] >> i) & 1 ;
37
32
}
33
+ array[i] %= 3 ;
34
+ ret |= array[i] << i;
38
35
}
39
36
return ret;
40
37
}
41
- };
38
+ };
39
+
40
+ int main ()
41
+ {
42
+ Solution solution;
43
+ int a[4 ] = {2 ,2 ,3 ,2 };
44
+ vector<int > b;
45
+ b.insert (b.begin (),a,a+4 );
46
+ int result = solution.singleNumber (b);
47
+ cout << " Result of Solution is: " << result << endl;
48
+
49
+ return 0 ;
50
+ }
51
+
You can’t perform that action at this time.
0 commit comments