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 翻译: 不使用额外空 ...
随机推荐
- Ajax动态滚动加载数据
看新浪微博,人人网都有这样的效果:滚动条滚动到最下面的时候,新的数据就被自动加载出来了,今天亲自尝试了一下这个效果的实现. 最开始在CSDN上写了一版,功能比较简单,今天又增加了一个小功能:翻页到指定 ...
- Difference between Pragma and Cache-control headers?
Pragma is the HTTP/1.0 implementation and cache-control is the HTTP/1.1 implementation of the same c ...
- 【玩转Ubuntu】09. Ubuntu上安装apktool
下载两个文件 到这里 https://code.google.com/p/android-apktool/downloads/list?q=label:Featured下载这个文件 1. apkt ...
- java.lang.NoSuchFieldError: VERSION_2_3_0 报错解决方案
java.lang.NoSuchFieldError: VERSION_2_3_0 at org.apache.struts2.views.freemarker.FreemarkerManager.c ...
- Mac下无法拷贝文件到移动硬盘
Mac下无法拷贝文件到移动硬盘? 是移动硬盘的文件格式的问题. Mac系统无法识别 NTFS 格式的文件. 将移动硬盘格式化为 exFAT 格式的. 别担心,exFAT 格式的硬盘在Windows下也 ...
- Android Touch(1)事件的传递流程(*)
1,Activity,ViewGroup,View的关系 2,触摸事件 3,传递事件时的重要函数 4,事件传递流程参考图 5,其它参考资料 1,Activity,ViewGroup,View的关系 本 ...
- BZOJ 2751 容易题
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2751 题意:有一个数列A已知对于所有的A[i]都是1到n的自然数,并且知道对于一些A[i ...
- plsql programming 16 动态SQL和动态PLSQL
动态SQL 是指在执行时才构建 SQL 语句, 相对于静态 sql 的编译时就已经构建. 动态PLSQL 是指整个PL/SQL代码块都是动态构建, 然后再编译执行的. 作用: 1. 可以支持 DDL ...
- ural1238. Folding(记忆化)
1238 这算模拟加记忆化吗 找bug找了2个多小时..记忆化部分好想 就是字符串处理部分挫了 一个个复制模拟 各种修改查找 #include <iostream> #include< ...
- RIA技术
Rich Internet Application(富互联网应用程序,简称RIA),一种全新的Web应用程序架构,它结合了桌面软件良好的用户体验和web应用程序易部署的优点,很快获得了企业的青睐. 近 ...