leetcode:Palindrome Number
Question:
Determine whether an integer is a palindrome. Do this without extra space.
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
判断一个数是不是回文数,时间复杂度要求为O(1)
注意的地方:负数是不是回文数?不是的话返回-1(其实负数不是回文数),如果你在想把整数转化为字符串,注意空间复杂度的限制,你当然可以想去把这个整数反序,但是你得考虑反序后的整数有可能发生溢出,你怎么去解决这个问题?有更多一般的方法可以去解决这个问题。
算法思路:① 对于负数进行判断,是的话返回不是回文数,0单独拿出,返回是回文数;
② 先求得数的长度length,如果length==1那么说明数一位,返回是回文数,如果length>1,则取这个整数后半部分的数,并反序;
③ 判断经过处理后反序的数和前一部分是否相等,length为奇数或者偶数要分开处理,相等的话返回回文数。
代码实现(java):
class Solution {
public boolean isPalindrome(int x) {
if(x<0)
return false;//如果是负数,返回不是回文数
if(0==x)
return true;
int length=0; //记录回文数位数
int xx=x;//xx作为x的备份
while(x>0){
x/=10;
length++;
}//求整数的位数
// System.out.println(length);
if(1==length)
return true;//一位数返回是回文数
int i=0;
int sum=0;
while(i<length/2){
sum=(sum+xx%10)*10;
xx/=10;
i++;
}//注意这里得到的sum多乘以了个10
// System.out.println(xx);
// System.out.println(sum);
if((length&0x1)==0&&xx==sum/10){ //如果数的长度是偶数,并且xx==sum/10那么返回是回文数
return true;
}
if((length&0x1)==1&&xx/10==sum/10){//如果数的长度是奇数,并且xx/10==sum/10那么返回是回文数
return true;
}
return false;//其他情况不是回文数
}
}
leetcode:Palindrome Number的更多相关文章
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- Leetcode练习题 Palindrome Number
9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...
- [LeetCode] 9. Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- Leetcode 9. Palindrome Number(水)
9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...
- Q9:Palindrome Number
9. Palindrome Number 官方的链接:9. Palindrome Number Description : Determine whether an integer is a pali ...
- [LeetCode 题解]:Palindrome Number
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Determine ...
- LeetCode专题-Python实现之第9题:Palindrome Number
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- No.009:Palindrome Number
问题: Determine whether an integer is a palindrome. Do this without extra space. 官方难度: Easy 翻译: 不使用额外空 ...
随机推荐
- Linux命令-tar
tar命令用于对文件打包压缩或解压 格式:tar [选项 -C 指定解压到的目录] [文件] 打包并压缩文件: tar -zcvf 压缩包名.tar.gz 文件名 解压并展开压缩包 tar -zxvf ...
- 《Linux shell变量总结回顾》RHEL6(转)
文章版权:http://www.cnblogs.com/linux-super-meng/ 环境变量路径: [root@localhost ~]# set //查看到的是局部变量和全局变量2种 [ ...
- mac 下tomcat启动报错 unknown host
解决方法:sudo scutil --set HostName localhost
- javascript QUnit 单元测试
<!doctype html> <html> <head lang="zh-CN" dir="ltr"> <meta ...
- dojo 三 类和继承 dojo/_base/declare
这里要讲有关类的定义.继承和实现.官方教程:http://dojotoolkit.org/documentation/tutorials/1.7/declare/类的声明是通过declare 这个方法 ...
- NDK(11)Android.mk编译APK模板
转自 : http://hubingforever.blog.163.com/blog/static/1710405792011656434982/ 以下仅是使用Android.mk编译APK程序的 ...
- [HIHO1039]字符消除(字符串,枚举,模拟)
题目链接:http://hihocoder.com/problemset/problem/1039 思路:枚举所有字符更新的位置和ABC三种修改方案,之后再模拟消除规则,一步一步去消除.直到无法消除, ...
- pl/sql programming 05 循环迭代处理
使用循环应考虑的因素 1. 循环什么时候结束 2. 什么时候测试是否该结束循环 3. 采用这种循环的原因 1. 普通循环(简单循环) 使用场合, 不能确定循环执行多少次, 要求循环至少执行一次. 另外 ...
- Sublime Text 插件列表(整理中...)
作为Java Web的开发者,前端和后端的技术都会用到,用了几款文本编辑器,Uedit32.EditPlus.Sublime Text等,发现还是Sublime Text用起来最方便. 首先安装pac ...
- CakeDC(cakephp company)Git workflow--适合于较大团队大型项目开发
CakeDC Git workflow是一个项目开发和版本发布的工作流,在这个工作流程中开发和版本发布周期是基于几个关键阶段(key phases): Development: 所有活跃的开发活动都由 ...