No.009 Palindrome Number
9. Palindrome Number
- Total Accepted: 136330
- Total Submissions: 418995
- Difficulty: Easy
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
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.
思路:
本题解题很简单,首先判断负数和0,然后我们计算得到最大基数,如果x为一个两位数,则基数base=100,三位数则为1000等,然后我们每次分别取这个数的最高位和最低位进行比较,如果不相等,则直接返回false,相等则去掉最左边和最右边的数后进行下一轮比较。代码如下:
public boolean isPalindrome(int x) {
if(x < 0){
return false ;
}
if(x == 0 ){
return true ;
} int base = 1 ;
while(x/base >= 10){
base *= 10 ;
} while(x != 0){
int leftDigit = x/base ;
int rightDigit = x%10 ;
if(leftDigit != rightDigit){
return false ;
}
x = x%base/10 ;
base /= 100 ;
}
return true ;
}
No.009 Palindrome Number的更多相关文章
- LeetCode--No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
- 【JAVA、C++】LeetCode 009 Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 解题思路一: 双指针法,逐位判断 Java代码如下 ...
- 【LeetCode】009. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- [Leetcode]009.Palindrome Number
public class Solution { public boolean isPalindrome(int x) { if (x<0 || (x!=0 && x%10==0) ...
- 009 Palindrome Number 判断一个正整数是否是回文数
详见:https://leetcode.com/problems/palindrome-number/description/ 实现语言:Java 方法一: class Solution { publ ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- 有趣的数-回文数(Palindrome number)
文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...
- 9. Palindrome Number
/* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...
随机推荐
- Dictionary 的使用
1. 定义字典变量,并初始化 // 元素值字典 Dictionary<string, string> dic = new Dictionary<string, string>( ...
- virtual修饰符
virtual(C# 参考) virtual 关键字用于修饰方法.属性.索引器或事件声明,并使它们可以在派生类中被重写. 例如,此方法可被任何继承它的类重写. public virtual doubl ...
- Swift与OC之间的选择
1.稳定性 在Swift2.0出来的时候,1.0的代码基本上改了个遍. 2.必要性 目前app store上大概有100w个是oc写的,如果是单纯的爱好,可以学习Swift,如果从事职业开发,那么还是 ...
- Oracle Erp常用网站
2014-01-01 Created By BaoXinjian
- dede忽略错误
一.修改php.ini中下面代码 ;extension=php_mbstring.dll 改为 extension=php_mbstring.dll ;mbstring.func_overload = ...
- MVC项目发布错误
下面各个方法尝试采用: 重装Framework 32位的Windows: --------------------------------------------------------------- ...
- 使用UltraEdit+BCC5.5搭建C语言学习环境(转)
今天闲来无聊,想起以前学的C都差不多忘光了,想练练,先搭环境吧,vc bc之类都太大了,我以前在borland下过一个命令行编译工具不错,好像以前看到有人用ultraedit配合命令行工具做过一个开发 ...
- php序列化,反序列化
serialize("数组"); //序列化的函数 序列化示范:serialize(array('1'=>1235622,'2'=>'4142122')); unser ...
- MYSQL批量插入数据库实现语句性能分析
假定我们的表结构如下 代码如下 CREATE TABLE example ( example_id INT NOT NULL, name VARCHAR( 50 ) NOT NULL, value ...
- Android学习笔记01
一. 创建Activity的要点: 1.继承Activity类2.需要重写onCreate方法3.需要在AndroidManifest.xml注册4.为Activity添加控件和内容5.setCont ...