用map记录每一个十进制整型数每个数字出现的次数
原创
©著作权归作者所有:来自51CTO博客作者wx596330ff6d68f的原创作品,请联系作者获取转载授权,否则将追究法律责任
869. Reordered Power of 2
Medium
13767FavoriteShare
Starting with a positive integer N
, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return true
if and only if we can do this in a way such that the resulting number is a power of 2.
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
class Solution {
private:
unordered_map<int,unordered_map<int,int>> Sets;
unordered_map<int,int> Key_value;
public:
bool reorderedPowerOf2(int N){
int i = 0;
while(pow(2,i) < pow(10,9)){
int M = pow(2,i);
unordered_map<int,int> tmp;
while(M > 0){
tmp[(M % 10)]++;
M = M / 10;
}
Sets[pow(2,i)] = tmp;
i++;
}
int temp = N;
while(temp){
Key_value[(temp % 10)] ++;
temp = temp / 10;
}
i = 0;
while(pow(2,i) < pow(10,9)){
int M = pow(2,i);
bool Flag = true;
if(Sets[M].size() != Key_value.size()){
i++;
continue;
}
for(auto Key:Sets[M]){
if(Key_value.find(Key.first) == Key_value.end() || Key_value[Key.first] != Sets[M][Key.first]){
Flag = false;
break;
}
}
if(Flag == true){
return true;
}
else{
i ++;
continue;
}
}
return false;
}
};