当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

演讲议题及嘉宾重磅揭晓,TDengine 开发者大会推动数据技术“破局”

Detailed demonstration pytorch framework implementations old photo repair (GPU)

收藏-即时通讯(IM)开源项目OpenIM-功能手册

力扣206-反转链表——链表

RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸

高效目标检测:动态候选较大程度提升检测精度(附论文下载)

tRNA-m5C转运RNA(tRNA)修饰5-甲基胞嘧啶(m5C)|tRNA修饰m1Am2A (2-methyladenosine)

tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)

盘点在线帮助中心对企业能够起到的作用

边缘盒子+时序数据库,美的数字化平台 iBuilding 背后的技术选型
随机推荐
Go语言为任意类型添加方法
算法--交错字符串(Kotlin)
ES6解构赋值--数组解构及对象解构
【leetcode】剑指 Offer II 008. 和大于等于 target 的最短子数组(滑动窗口,双指针)
LeetCode 622. 设计循环队列
2022.8.2
ARMuseum
leetcode 剑指 Offer 15. 二进制中1的个数
JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
alicloud3搭建wordpress
开源生态研究与实践| ChinaOSC
ES6-箭头函数
charles配置客户端请求全部不走缓存
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
ESP8266-Arduino编程实例-WS2812驱动
数学之美 第六章——信息的度量和作用
xss.haozi练习通关详解
leetcode 1837. K 进制表示下的各位数字总和
ES6--剩余参数
CSDN帐号管理规范