当前位置:网站首页>leetcode 2119. Numbers reversed twice
leetcode 2119. Numbers reversed twice
2022-08-03 20:11:00 【Luna programming】
反转 一个整数意味着倒置它的所有位.
例如,反转 2021 得到 1202 .反转 12300 得到 321 ,不保留前导零 .
给你一个整数 num ,反转 num 得到 reversed1 ,接着反转 reversed1 得到 reversed2 .如果 reversed2 等于 num ,返回 true ;否则,返回 false .
示例 1:
输入:num = 526
输出:true
解释:反转 num 得到 625 ,接着反转 625 得到 526 ,等于 num .
示例 2:
输入:num = 1800
输出:false
解释:反转 num 得到 81 ,接着反转 81 得到 18 ,不等于 num .
示例 3:
输入:num = 0
输出:true
解释:反转 num 得到 0 ,接着反转 0 得到 0 ,等于 num .
提示:
0 <= num <= 106
思路:
The condition for the non-negative integer to be reversed twice is unchanged:该整数为 0 or the integer ends without 0.
As long as it's not the first one at the beginning0Or the last digit at the end is not0,经过反转2After the times are equal to the original number.
class Solution {
public:
bool isSameAfterReversals(int num) {
return num==0 || num%10!=0; //数为0时直接返回true,And the last one doesn't0也是true
}
};
Another way is to actually reverse the number2次 (我就是这么干的,I deeply felt my own humiliation)
class Solution {
public:
int exchange(int num){
int ans=0;
int x=num,k;
while(x){
k=x%10;
ans*=10;
ans+=k;
x/=10;
}
return ans;
}
bool isSameAfterReversals(int num) {
int w;
w=exchange(num);
w=exchange(w);
return num==w ? true : false;
}
};
边栏推荐
猜你喜欢
随机推荐
ThreadLocal详解
xss.haozi练习通关详解
JMeter笔记5 |Badboy使用和录制
leetcode 461. 汉明距离
阿洛的反思
深入理解JVM-内存结构
Pytorch GPU 训练环境搭建
Go语言为任意类型添加方法
高位套牢机构,用友网络的信任危机是如何产生的?
List类的超详细解析!(超2w+字)
2022 年值得尝试的 7 个 MQTT 客户端工具
JWT详解
pytorch框架实现老照片修复功能详细演示(GPU版)
百利药业IPO过会:扣非后年亏1.5亿 奥博资本是股东
微导纳米IPO过会:年营收4.28亿 君联与高瓴是股东
【飞控开发高级教程3】疯壳·开源编队无人机-定高、定点、悬停
Detailed explanation of JWT
高效目标检测:动态候选较大程度提升检测精度(附论文下载)
Detailed steps for tensorflow-gpu2.4.1 installation and configuration
「学习笔记」高斯消元