[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 < ) return false;
int div = ;
while (x / div >= ) div *= ;
while (x > ) {
int left = x / div;
int right = x % ;
if (left != right) return false;
x = (x % div) / ;
div /= ;
}
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 < || (x % == && x != )) return false;
int revertNum = ;
while (x > revertNum) {
revertNum = revertNum * + x % ;
x /= ;
}
return x == revertNum || x == revertNum / ;
}
};
下面这种解法由热心网友 zeeng 提供,如果是 palindrome,反转后仍是原数字,就不可能溢出,只要溢出一定不是 palindrome 返回 false 就行。可以参考 Reverse Integer 这题,直接调用 Reverse()。
解法三:
class Solution {
public:
bool isPalindrome(int x) {
if (x < || (x % == && x != )) return false;
return reverse(x) == x;
}
int reverse(int x) {
int res = ;
while (x != ) {
if (res > INT_MAX / ) return -;
res = res * + x % ;
x /= ;
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/9
类似题目:
参考资料:
[LeetCode] 9. Palindrome Number 验证回文数字的更多相关文章
- [LeetCode] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- LeetCode 9 Palindrome Number(回文数字判断)
Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的 ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- [LeetCode]9. Palindrome Number判断回文数字
/* 查看网上的思路有两种: 1.每次取两边的数,然后进行比较 2.取数的倒置数,进行比较 */ public boolean isPalindrome1(int x) { if (x<0) r ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- [LeetCode] Valid Palindrome II 验证回文字符串之二
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- LeetCode OJ Palindrome Number(回文数)
class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...
- LeetCode 9. Palindrome Number(回文数)
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
随机推荐
- Git修改和配置用户名和邮箱
git在push/push to时需要使用到user.name和user.email,切记一定要现配置好查看user.name/user.email git config user.name git ...
- 06Shell并发控制
并发控制 文件描述 文件句柄 File Descriptors (FD,文件描述符)或 文件句柄: 进程使用文件描述符来管理打开的文件 注意 1.如何通过exec打开一个文件 exec 数字<& ...
- Vue.js 源码分析(十九) 指令篇 v-html和v-text指令详解
双大括号会将数据解释为普通文本,而非 HTML 代码.为了输出真正的 HTML,你需要使用 v-html 指令,例如: <!DOCTYPE html> <html lang=&quo ...
- com.alibaba.fastjson.JSONObject之对象与JSON转换方法
com.alibaba.fastjson.JSONObject时经常会用到它的转换方法,包括Java对象转成JSON串.JSON对象,JSON串转成java对象.JSON对象,JSON对象转换Java ...
- 如何给gridControl动态的添加合计
for (int i = 0; i < this.dsHz.Tables[0].Columns.Count; i++) { if (dsHz.Tables[0].Columns[i].DataT ...
- vue+django分离开发的思想和跨域问题的解决
一.介绍 在前后端分离的开发过程中,会涉及到跨域的问题,比如本次个人使用的Django+vue的框架,在vue对Django进行响应,Django再将响应的数据返回给vue,vue在进行渲染,如果不设 ...
- kali渗透综合靶机(十一)--BSides-Vancouver靶机
kali渗透综合靶机(十一)--BSides-Vancouver靶机 靶机下载地址:https://pan.baidu.com/s/1s2ajnWHNVS_NZfnAjGpEvw 一.主机发现 1.n ...
- wpf的控件树
转载自:控件树
- 我的Xamarin开发配置
我用的的是VS2019 步骤1:打开VS→工具→Android→Android SDK 管理器 安装平台的 Android 9.0-pie下的Android SDK Platform 28 和 Goo ...
- minggw 安装
windows上如果没有安装 visual studio, 也就是俗称的vs, 在安装一些带有c或者c++代码的Python模块的时候, 会报错Unable to find vcvarsall.bat ...