当前位置:网站首页>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;
}
};
边栏推荐
- 【leetcode】剑指 Offer II 008. 和大于等于 target 的最短子数组(滑动窗口,双指针)
- ThreadLocal详解
- codeforces:C. Maximum Subrectangle【前缀和 + 贪心 + 最小子数组和】
- Go语言为任意类型添加方法
- Anaconda 虚拟环境迁移
- 【STM32】标准库-自定义BootLoader
- ES6简介及let、var、const区别
- Edge box + time series database, technology selection behind Midea's digital platform iBuilding
- leetcode 剑指 Offer 15. 二进制中1的个数
- 模板字符串概述
猜你喜欢
随机推荐
子结点的数量(2)
php根据两点经纬度计算距离
leetcode 136. 只出现一次的数字(异或!!)
JWT详解
危化企业双重预防机制数字化建设进入全面实施阶段
1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)
谁的孙子最多II
Hinton2022年RobotBrains访谈记录
染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56
ESP8266-Arduino编程实例-MCP4725数模转换器驱动
149. The largest number on a straight line, and check the set
JMeter笔记5 |Badboy使用和录制
(十六)51单片机——红外遥控
消除对特权账户的依赖使用Kaniko构建镜像
力扣203-移除链表元素——链表
力扣206-反转链表——链表
Go语言类型与接口的关系
List类的超详细解析!(超2w+字)
LeetCode 899. 有序队列
Golang死信队列的使用