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. ORA-04031案例一则

    ORA-04031这个错误,几乎每一个专业的DBA都遇到过.这是一个相当严重的错误,Oracle进程在向SGA申请内存时,如果申请失败,则会报这个错误.大部分情况下是在向SGA中的shared poo ...

  2. cf380D Sereja and Cinema 组合数学

              time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  3. Python中HTTPS连接

    permike 原文 Python中HTTPS连接 今天写代码时碰到一个问题,花了几个小时的时间google, 首先需要安装openssl,更新到最新版本后,在浏览器里看是否可访问,如果是可以的,所以 ...

  4. python随文档

    UNIX网络编程--socket的keep http://www.68idc.cn/help/opersys/unixbsd/20150731471448.html 云计算学习和实践: 原创<每 ...

  5. 点击Listview弹出PopWindow的用法

    先来张截图: 如图点击listview中的Item在item的下方弹出一个框框,这个框框就是用的Popwindow.    用法很简单:首先写一个PopupWindow并自定义它的布局:       ...

  6. 帝国CMS商城功能高级使用

    一.常见问题1.为什么自定义模型加入购物车提示“非商城表的信息”?答:模型要使用商城的功能,要先到:后台>商城>商城参数设置:“指定使用商肠能的数据表”勾选你这个自定义模型表,才可以使用. ...

  7. mysql 用户权限设置【转】

    在Linux下phpStudy集成开发环境中,要先进入mysql下bin目录,执行mysql ./mysql -u root -p 1.创建新用户 通过root用户登录之后创建 >> gr ...

  8. .NET Session操作

    public class SessionHelper { /// <summary> /// 根据session名获取session对象 /// </summary> /// ...

  9. Microsoft visual studio中文字样输出

    解决办法: 可以尝试下通过: 1.file->高级保存选项-> 2.工具->选项->文本编辑器->自动检测不带签名的UTF-8编码

  10. [ActionScript 3.0] Away3D 官网实例

    /* Dynamic tree generation and placement in a night-time scene Demonstrates: How to create a height ...