2024每日刷题(126)
Leetcode—1056. 易混淆数
💩山实现代码
class Solution {
public:
bool confusingNumber(int n) {
int arr[10] = {0};
int notNum = 0;
int arr2[12] = {0};
int size = 0;
while(n) {
int x = n % 10;
arr[x] += 1;
arr2[size++] = x;
if(x == 2 || x == 3 || x == 4 || x == 5 || x == 7) {
return false;
}
n /= 10;
}
bool flag = false;
for(int i = 0; i < size / 2; i++) {
if(arr2[i] != arr2[size - i - 1]) {
if((arr2[i] == 6 && arr2[size - i - 1] == 9) || (arr2[i] == 9 && arr2[size - i - 1] == 6)) {
flag = true;
continue;
}
if(((arr2[i] == 6 || arr2[i] == 9) && (arr2[size - i - 1] != 9 && arr2[size - i - 1] != 6)) || ((arr2[i] != 6 && arr2[i] != 9) && (arr2[size - i - 1] == 9 || arr2[size - i - 1] == 6))) {
return true;
}
return true;
} else {
flag = false;
}
}
// 奇数如果是6或者9必混淆
if(size % 2 && !flag) {
if(arr2[size / 2] == 6 || arr2[size / 2] == 9) {
return true;
}
}
return false;
}
};
运行结果
巧用字符串实现代码
class Solution {
public:
bool confusingNumber(int n) {
string raw = to_string(n);
const vector<char> rotated = {'0', '1', 'x', 'x', 'x', 'x', '9', 'x', '8', '6'};
string res;
int len = raw.length();
for(int i = len - 1; i >= 0; i--) {
if(rotated[raw[i] - '0'] == 'x') {
return false;
}
res += rotated[raw[i] - '0'];
}
return res != raw;
}
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!