C++实现LeetCode(9.验证回文数字)
作者:Grandyang 时间:2023-06-21 00:20:38
[LeetCode] 9. Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
这道验证回文数字的题如果将数字转为字符串,就变成了验证回文字符串的题,没啥难度了,我们就直接来做 follow up 吧,不能转为字符串,而是直接对整数进行操作,可以利用取整和取余来获得想要的数字,比如 1221 这个数字,如果 计算 1221 / 1000, 则可得首位1, 如果 1221 % 10, 则可得到末尾1,进行比较,然后把中间的 22 取出继续比较。代码如下:
解法一:
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) return false;
int div = 1;
while (x / div >= 10) div *= 10;
while (x > 0) {
int left = x / div;
int right = x % 10;
if (left != right) return false;
x = (x % div) / 10;
div /= 100;
}
return true;
}
};
再来看一种很巧妙的解法,还是首先判断x是否为负数,这里可以用一个小 trick,因为整数的最高位不能是0,所以回文数的最低位也不能为0,数字0除外,所以如果发现某个正数的末尾是0了,也直接返回 false 即可。好,下面来看具体解法,要验证回文数,那么就需要看前后半段是否对称,如果把后半段翻转一下,就看和前半段是否相等就行了。所以做法就是取出后半段数字,进行翻转,具体做法是,每次通过对 10 取余,取出最低位的数字,然后加到取出数的末尾,就是将 revertNum 乘以 10,再加上这个余数,这样翻转也就同时完成了,每取一个最低位数字,x都要自除以 10。这样当 revertNum 大于等于x的时候循环停止。由于回文数的位数可奇可偶,如果是偶数的话,那么 revertNum 就应该和x相等了;如果是奇数的话,那么最中间的数字就在 revertNum 的最低位上了,除以 10 以后应该和x是相等的,参见代码如下:
解法二:
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0 || (x % 10 == 0 && x != 0)) return false;
int revertNum = 0;
while (x > revertNum) {
revertNum = revertNum * 10 + x % 10;
x /= 10;
}
return x == revertNum || x == revertNum / 10;
}
};
来源:https://www.cnblogs.com/grandyang/p/4125510.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
flutter ExpansionTile 层级菜单的实现
![](https://img.aspxhome.com/file/2023/7/91007_0s.png)
Flutter 容器盒子模型的使用示例
![](https://img.aspxhome.com/file/2023/4/85974_0s.png)
Flutter 如何正确显示SnackBar
iOS新浪微博、腾讯微博分享功能实例
![](https://img.aspxhome.com/file/2023/0/101960_0s.png)
iOS应用中使用Toolbar工具栏方式切换视图的方法详解
![](https://img.aspxhome.com/file/2023/6/98646_0s.png)
flutter TextField换行自适应的实现
Flutter 剪裁组件的使用
![](https://img.aspxhome.com/file/2023/3/96783_0s.png)
Flutter网络请求的3种简单实现方法
flutter实现appbar下选项卡切换
![](https://img.aspxhome.com/file/2023/3/87313_0s.gif)
Flutter瀑布流仿写原生的复用机制详解
![](https://img.aspxhome.com/file/2023/0/98700_0s.jpg)
基于Flutter实现多边形和多角星组件
![](https://img.aspxhome.com/file/2023/7/84747_0s.png)
Flutter实现矩形取色器的封装
![](https://img.aspxhome.com/file/2023/9/96009_0s.jpg)
详解Flutter桌面应用如何进行多分辨率适配
Android开发组件flutter的20个常用技巧示例总结
![](https://img.aspxhome.com/file/2023/3/88093_0s.png)
详解Flutter中视频播放器插件的使用教程
![](https://img.aspxhome.com/file/2023/9/83519_0s.jpg)
使用Flutter实现一个走马灯布局的示例代码
![](https://img.aspxhome.com/file/2023/4/84714_0s.gif)
Flutter开发中的路由参数处理
![](https://img.aspxhome.com/file/2023/4/88724_0s.gif)
Flutter模仿实现微信底部导航栏流程详解
![](https://img.aspxhome.com/file/2023/3/100603_0s.png)
使用flutter创建可移动的stack小部件功能
![](https://img.aspxhome.com/file/2023/0/57150_0s.gif)
详解C++ STL模拟实现forward_list
![](https://img.aspxhome.com/file/2023/0/60100_0s.jpg)