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的更多相关文章

  1. LeetCode--No.009 Palindrome Number

    9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...

  2. 【JAVA、C++】LeetCode 009 Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 解题思路一: 双指针法,逐位判断 Java代码如下 ...

  3. 【LeetCode】009. Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  4. [Leetcode]009.Palindrome Number

    public class Solution { public boolean isPalindrome(int x) { if (x<0 || (x!=0 && x%10==0) ...

  5. 009 Palindrome Number 判断一个正整数是否是回文数

    详见:https://leetcode.com/problems/palindrome-number/description/ 实现语言:Java 方法一: class Solution { publ ...

  6. 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...

  7. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  8. 有趣的数-回文数(Palindrome number)

    文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...

  9. 9. Palindrome Number

    /* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...

随机推荐

  1. ERP_基于Oracle SOA的企业服务总线整合

    2015-01-01 Created By BaoXinjian

  2. 练习JavaScript实现梯形乘法表 效果:

    表格用html中的table,tr,td,然后利用for语句实现,循环输出行和列,再根据行列的数量进行乘法运算,第一个for循环输出9行, 然后内嵌一个for,在条件表达式中取第一个for循环的值然后 ...

  3. CentOS安装apache2(转载)

    From:http://www.onepx.com/centos-apache-246.html 之前服务器 Apache 版本一直是 2.2.x,鉴于 Centos 更新软件的惰性,我看直到 201 ...

  4. SQL Server查询死锁并KILL

    杀掉死锁的sqlserver进程   SELECT request_session_id spid,OBJECT_NAME (resource_associated_entity_id)tableNa ...

  5. async callback z

    public class StackOverflow_5979252 { [ServiceContract(Name = "IMessageCallback")] public i ...

  6. Linux文件夹文件创建、删除

    Linux删除文件夹命令 linux删除目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可.直接rm就可以了,不过要加两个参数-rf 即:rm ...

  7. C++学习48 对ASCII文件的读写操作

    如果文件的每一个字节中均以ASCII代码形式存放数据,即一个字节存放一个字符,这个文件就是ASCII文件(或称字符文件).程序可以从ASCII文件中读入若干个字符,也可以向它输出一些字符. 对ASCI ...

  8. [Java] 读写字节数据,过滤流DataOutputStream和DataInputStream

    package test.stream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io ...

  9. eclipse中debug

      在需要测试的代码行左侧行号列上双击,生成断点 ,再次双击可以取消断点 如图:   然后右键,选择Debug As-Java Application,(注意,不是选择Run As) 开始调试java ...

  10. Delphi 连接mysql 的功能, 去除乱码, 需要设置字符集

    vDataBaseName := aConfiginiFile.ReadString('DataBaseConfig', 'DataBase', CH_IPC712Db); vServer := aC ...