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. sbt设置

    配置中央仓库 vim ~/.sbt/repositories [repositories] local osc: http://maven.oschina.net/content/groups/pub ...

  2. [Bug-IOS] - linker command failed with exit code 1 (use -v to see invocation)

    Ld /Users/Rubert/Library/Developer/Xcode/DerivedData/OC_Language-emftyzftyvhdpuaxipddjmpnpvox/Build/ ...

  3. hibernate级联与反向

    cascade:设置本表与关联表之间的级联操作,如:设置为save-update,则插入或更新对象时同时保存或更新另一端的表,但不会产生关联关系数据,除非inverse为false. inverse: ...

  4. BestCoder Round #86 部分题解

    Price List 题意: 有n件商品,每天只能买一件,并且会记录账本,问有多少次一定记多了? 题解: 就是求和,最后如果大于和就输出1,否则0. 代码: #include <bits/std ...

  5. c#复习整理

    一.基本语法 1.数据类型 整数类型:int.long 浮点类型:float.double.decimal 布尔类型:bool 字符串类型:string 2.类型转换 int a; double b ...

  6. MHI ,运动历史图像的的获取[下载自CSDN]

    #include "cv.h" #include "highgui.h" #include "stdlib.h" #include &quo ...

  7. 把centos 的mysql 重装一下 把原来的lnmp删除,怎么备份还原数据库

    mysqldump --lock-all-tables -u root -p --databases mydb > /opt/database/mydb.sql,或者直接备份mysql的数据存储 ...

  8. [Linux] Netstat 执行过慢,占CPU100%,原因查找

    一.缘由: 待续 二.解决办法: netstat -tlnp netstat -nap|grep pid ss |grep pid ss -s sar -u 1 10 strace -FfT -o n ...

  9. mongoDB索引使用

    for(var i=0;i<10000;i++){ db.user.insert({name:"user"+i,age:i}) }添加这么多数据 db.user.find({ ...

  10. Windows 64位下安装Redis详细教程

    方法/步骤 在D盘新建文件夹[redis],右键解压Redis ZIP包,把所有文件解压到redis文件夹中.(其他盘符也可以滴^_^) 文件介绍: redis-benchmark.exe       ...