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 翻译: 不使用额外空 ...
随机推荐
- CleanAOP实战系列--WPF中MVVM自动更新
CleanAOP实战系列--WPF中MVVM自动更新 作者: 立地 邮箱: jarvin_g@126.com QQ: 511363759 CleanAOP介绍:https://github.com/J ...
- VI操作--跳到最后一行和跳到最后一行的最后一个字符
vi操作 1.跳到文本的最后一行:按“G”,即“shift+g” 2.跳到最后一行的最后一个字符 : 先重复1的操作即按“G”,之后按“$”键,即“shift+4”. 3.跳到第一行的第一个字符:先按 ...
- [转]“WARNING: soft rlimits too low” in MongoDB with Mac OS X
转自:Programming and Technology If you get this warning when you connect to mongo shell in Mac OX X: * ...
- 动态网页制作PHP常用的正则表达式
匹配中文字符的正则表达式: [u4e00-u9fa5] 匹配双字节字符(包括汉字在内): [^x00-xff] 应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) 匹配空行的正则表达 ...
- 使用 .gitignore来忽略某些文件【转】
转自:http://www.cnblogs.com/shangdawei/archive/2012/09/08/2676493.htmlhttp://blog.csdn.net/richardyste ...
- 自定义View(7)draw与onDraw区别
draw()这个函数本身会做很多事情,可参看源码. * 1. Draw the background * 2. If necessary, save ...
- [HIHO1260]String Problem I(trie树)
题目链接:http://hihocoder.com/problemset/problem/1260 n个字符串,m次询问.每次询问给一个字符串,问这个字符串仅可以在一个地方加一个字母.这样操作后与n个 ...
- js中的json对象详细介绍
JSON一种简单的数据格式,比xml更轻巧,在JavaScript中处理JSON数据不需要任何特殊的API或工具包,下面为大家详细介绍下js中的json对象, 1.JSON(JavaScript Ob ...
- POJ 3107 Godfather (树形dp)
题目链接 虽然题目不难,但是1A还是很爽, 只是刚开始理解错题意了,想了好久. 还有据说这个题用vector会超时,看了以后还是用邻接吧. 题意: 给一颗树,保证是一颗树,求去掉一个点以后的联通块里节 ...
- hdu 1044(bfs+状压)
非常经典的一类题型 没有多个出口.这里题目没有说清楚 Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...