File tree Expand file tree Collapse file tree 3 files changed +72
-2
lines changed
剑指 Offer II 003. 前 n 个数字二进制中 1 的个数
剑指 Offer II 004. 只出现一次的数字
剑指 Offer II 005. 单词长度的最大乘积 Expand file tree Collapse file tree 3 files changed +72
-2
lines changed Original file line number Diff line number Diff line change @@ -7,4 +7,17 @@ class Solution {
7
7
}
8
8
return f;
9
9
}
10
- };
10
+ };
11
+
12
+ class Solution {
13
+ public:
14
+ vector<int > countBits (int n) {
15
+ vector<int > ans;
16
+ ans.push_back (0 );
17
+ for (int i = 1 ; i <= n; i++)
18
+ {
19
+ ans.push_back (ans[i & (i - 1 )] +1 );
20
+ }
21
+ return ans;
22
+ }
23
+ };
Original file line number Diff line number Diff line change @@ -12,4 +12,30 @@ class Solution {
12
12
}
13
13
return ans;
14
14
}
15
- };
15
+ };
16
+
17
+ class Solution {
18
+ public:
19
+ int singleNumber (vector<int >& nums) {
20
+ vector<int > array (32 );
21
+ 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
+ for (int i = 0 ; i < 32 ; i++)
33
+ {
34
+ if (array[i] % 3 )
35
+ {
36
+ ret += ((array[i] % 3 ) << i);
37
+ }
38
+ }
39
+ return ret;
40
+ }
41
+ };
Original file line number Diff line number Diff line change @@ -19,4 +19,35 @@ class Solution {
19
19
}
20
20
return ans;
21
21
}
22
+ };
23
+
24
+ int string_to_int (string word)
25
+ {
26
+ int ret = 0 ;
27
+ for (int i = 0 ; i < word.size (); i++)
28
+ {
29
+ ret |= 1 << (word[i] - ' a' );
30
+ }
31
+ return ret;
32
+ }
33
+
34
+ class Solution {
35
+ public:
36
+ int maxProduct (vector<string>& words) {
37
+ int max = 0 , numa = 0 , numb = 0 ;
38
+ int i, j;
39
+ for (i = 0 ; i < words.size () - 1 ; i++)
40
+ {
41
+ numa = string_to_int (words[i]);
42
+ for (j = i + 1 ;j < words.size (); j++)
43
+ {
44
+ numb = string_to_int (words[j]);
45
+ if ( !(numa & numb))
46
+ {
47
+ max = max > words[i].size () * words[j].size () ? max : words[i].size () * words[j].size ();
48
+ }
49
+ }
50
+ }
51
+ return max;
52
+ }
22
53
};
You can’t perform that action at this time.
0 commit comments