Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d218fdd

Browse files
committed
Add C++ solutions for leetcode 869.
1 parent aa4b4d7 commit d218fdd

3 files changed

+130
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<unordered_map>
4+
#include<iostream>
5+
using namespace std;
6+
7+
class Solution {
8+
public:
9+
bool reorderedPowerOf2(int n) {
10+
for (int i = 1; i <= 1e9; i *= 2)
11+
{
12+
if (canOrderToSame(i, n)) return true;
13+
}
14+
return false;
15+
}
16+
bool canOrderToSame(int i, int n) /* 判断整数i是否重新排序后能和n相等 */
17+
{
18+
unordered_map<char, int> dict1, dict2;
19+
for (auto ch : to_string(i)) dict1[ch]++; /* 转为字符串, 统计其中每一个字符出现的次数 */
20+
for (auto ch : to_string(n)) dict2[ch]++;
21+
return dict1 == dict2;
22+
}
23+
};
24+
25+
// Test
26+
int main()
27+
{
28+
Solution sol;
29+
int n = 24;
30+
auto res = sol.reorderedPowerOf2(n);
31+
cout << (res ? "True" : "False") << endl;
32+
33+
return 0;
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<unordered_map>
4+
#include<iostream>
5+
using namespace std;
6+
7+
class Solution {
8+
public:
9+
bool reorderedPowerOf2(int n) {
10+
for (int i = 1; i <= 1e9; i *= 2)
11+
{
12+
if (canOrderToSame(i, n)) return true;
13+
}
14+
return false;
15+
}
16+
bool canOrderToSame(int i, int& n) /* 判断整数i是否重新排序后能和n相等 */
17+
{
18+
for (int d = 0; d <= 9; d++) /* 枚举每一位的数字(0~9之间), 只要出现某一个数字在整数i和n中的数量不相等就返回false */
19+
{
20+
int count1 = countTimes(i, d);
21+
int count2 = countTimes(n, d);
22+
if (count1 != count2) return false;
23+
}
24+
return true;
25+
}
26+
int countTimes(long int x, int d) /* 统计每一位的数digit(简写为d)在整数x中出现的次数 */
27+
{
28+
int count = 0;
29+
while (x)
30+
{
31+
if (x % 10 == d)
32+
count++;
33+
x = x / 10;
34+
}
35+
return count;
36+
}
37+
};
38+
39+
// Test
40+
int main()
41+
{
42+
Solution sol;
43+
int n = 24;
44+
auto res = sol.reorderedPowerOf2(n);
45+
cout << (res ? "True" : "False") << endl;
46+
47+
return 0;
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<unordered_map>
4+
#include<iostream>
5+
using namespace std;
6+
7+
class Solution {
8+
public:
9+
bool reorderedPowerOf2(int n) {
10+
for (int i = 1; i <= 1e9; i *= 2)
11+
{
12+
if (canOrderToSame(i, n)) return true;
13+
}
14+
return false;
15+
}
16+
bool canOrderToSame(int i, int& n) /* 判断整数i是否重新排序后能和n相等 */
17+
{
18+
vector<int> dict1(11), dict2(11);
19+
for (int d = 0; d <= 9; d++)
20+
{
21+
dict1[d] = countTimes(i, d);
22+
dict2[d] = countTimes(n, d);
23+
}
24+
return dict1 == dict2;
25+
}
26+
int countTimes(long int x, int d) /* 计算每一位的数digit(简写为d)在整数x中出现的次数 */
27+
{
28+
int count = 0;
29+
while (x)
30+
{
31+
if (x % 10 == d)
32+
count++;
33+
x = x / 10;
34+
}
35+
return count;
36+
}
37+
};
38+
39+
// Test
40+
int main()
41+
{
42+
Solution sol;
43+
int n = 24;
44+
auto res = sol.reorderedPowerOf2(n);
45+
cout << (res ? "True" : "False") << endl;
46+
47+
return 0;
48+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /