当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
随机推荐
Detailed demonstration pytorch framework implementations old photo repair (GPU)
ESP8266-Arduino编程实例-BH1750FVI环境光传感器驱动
YARN功能介绍、交互流程及调度策略
头条服务端一面经典10道面试题解析
8.3模拟赛总结
leetcode 231. 2 的幂
模板字符串概述
JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
【飞控开发高级教程3】疯壳·开源编队无人机-定高、定点、悬停
List类的超详细解析!(超2w+字)
剑指 Offer II 044. 二叉树每层的最大值-dfs法
若依集成browscap读取浏览器用户代理
高效目标检测:动态候选较大程度提升检测精度(附论文下载)
单调栈及其应用
力扣59-螺旋矩阵 II——边界判断
ESP8266-Arduino编程实例-MCP4725数模转换器驱动
Hinton2022年RobotBrains访谈记录
极验深知v2分析
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56



![【微信小程序2】事件传参与数据同步[03]](/img/d9/73004e6edf800c583231a94dfbd878.png)





