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 翻译: 不使用额外空 ...
随机推荐
- Android 【问题汇总】列表数组越界的问题
遇到了一个诡异的问题,ListView发生数组越界(偶尔会),程序崩溃. 错误信息如下: W/dalvikvm( ): threadid=: thread exiting with uncaught ...
- Spring_MVC_教程_快速入门_深入分析
Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门 资源下载: Spring_MVC_教程_快速入门_深入分析V1.1.pdf Spring ...
- Combiner
如果job 设置了 combiner ,则job的每个map运行的数据会先进入combiner,然后再通过patitioner分发到reduce.通过combiner能减少reduce的计算.空间压力 ...
- http 服务
今天把一个功能模块做成了http服务,这还是第一次写http服务,纪录下来. package com.chuntent.itemsearch; import java.io.BufferedReade ...
- 格子布局 js
<!doctype html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- USACO Section 3.1: Agri-Net
minimal spanning tree的经典题 /* ID: yingzho1 LANG: C++ TASK: agrinet */ #include <iostream> #incl ...
- poj 1180 斜率优化dp
这个题目要是顺着dp的话很难做,但是倒着推就很容易退出比较简单的关系式了. dp[i]=min(dp[u]+(sum[u-1]-sum[i-1]+s)*f[i]);dp[i]代表从i到结尾需要花费的代 ...
- ubuntu samba服务器多用户配置【转】
转自:http://www.2cto.com/os/201204/127043.html ubuntu samba服务器多用户配置 在/home/下有多个用户目录A.B...,现通过samba共享 ...
- 对Java“一切皆对象”的理念的理解
在从HelloWorld到面向对象中,我们将int, float, double, boolean等称为基本类型(primitive type),也就是特殊的类.我们可以将一个整数理解称为一个int类 ...
- 【Android】MTK Android 编译命令
命令格式:./maketek [option] [project] [action] [modules] Option: -t ,-tee :输出log信息到当前终端 -o , -opt=-- : 编 ...