题目

Determine whether an integer is a palindrome. Do this without extra space.

click to show spoilers.

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.

题解

最开始是用Reverse Integer的方法做的,通过了OJ,不过那个并没有考虑越界溢出问题。遇见这种处理Number和Integer的问题,首要考虑的就是会不会溢出越界。然后再考虑下负数,0。还有就是要心里熟悉\的结果和%的结果。感觉这种题就是考察你对数字敏感不敏感(反正我是很不敏感)

有缺陷的代码如下:

 1     public boolean isPalindrome(int x) {

 2         if(x<0)

 3            return false;

 4         

 5         int count = 0;

 6         int testx = x;

 7         while(testx!=0){

 8            int r = testx%10;

 9            testx = (testx-r)/10;

            count++;

         }

         

         int newx = x;

         int result = 0;

         while(newx!=0){

            int r = newx%10;

            int carry = 1;

            int times = count;

            while(times>1){

                carry = carry*10;

                times--;

            }

            result = result+r*carry;

            newx= (newx-r)/10;

            count--;

         }

         

         return result==x;

         

     }

另外一种方法可以避免造成溢出,就是直接安装PalidromeString的方法,就直接判断第一个和最后一个,循环往复。这样就不会对数字进行修改,而只是判断而已。

代码如下:

 1     public boolean isPalindrome(int x) {
 2         //negative numbers are not palindrome
 3         if (x < 0)
 4             return false;
 5  
 6         // initialize how many zeros
 7         int div = 1;
 8         while (x / div >= 10) {
 9             div *= 10;
         }
  
         while (x != 0) {
             int left = x / div;
             int right = x % 10;
  
             if (left != right)
                 return false;
  
             x = (x % div) / 10;
             div /= 100;
         }
  
         return true;
     }

Reference:

http://www.programcreek.com/2013/02/leetcode-palindrome-number-java/

Palindrome Number leetcode java的更多相关文章

  1. Palindrome Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...

  2. leetcode 第九题 Palindrome Number(java)

    Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...

  3. Letter Combinations of a Phone Number leetcode java

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  4. Palindrome Number ---- LeetCode 009

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

  5. Ugly Number leetcode java

    问题描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...

  6. Single Number leetcode java

    问题描述: Given an array of integers, every element appears twice except for one. Find that single one. ...

  7. Palindrome Partitioning leetcode java

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  8. Valid Number leetcode java

    题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

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

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

随机推荐

  1. Scanner和BufferedReader的区别和用法

    在命令行模式下要输入数据至程序中时,我们可以使用标准输入串对象System.in.但是,我们并不经常直接使用它,因为System.in提供的 read方法每次只能读取一个字节的数据,而我们平时所应用的 ...

  2. [Java]如何把当前时间插入到数据库

    [Java]如何把当前时间插入到数据库 1.在orderDao.java中 /** 设置订单*/ public void setOrder(Order order){ Date time = new ...

  3. 并发编程(二):全视角解析volatile

    一.目录 1.引入话题-发散思考 2.volatile深度解析 3.解决volatile原子性问题 4.volatile应用场景 二.引入话题-发散思考 public class T1 { /*vol ...

  4. GitLab查询当前版本

    gitlab-rake gitlab:env:info 其实还有很多方法可以参考GitLab的帮助文档:https://docs.gitlab.com/omnibus/README.html 参考: ...

  5. 精益软件研发的秘密 IT大咖说 - 大咖干货,不再错过

    精益软件研发的秘密 IT大咖说 - 大咖干货,不再错过   http://www.itdks.com/dakashuo/new/dakalive/detail/3662

  6. .Net 环境下C# 通过托管C++调用本地C++ Dll文件

     综述 : 本文章介绍.Net 环境下C# 通过托管C++调用本地C++ Dll文件, 示例环境为:VS2010, .Net4.0, Win7. 具体事例为测试C++, C#, 及C#调用本地C++D ...

  7. Cortex-M4 Core Registers

    Cortex-M4 Core Registers Goal: visualizing what happens to the Cortex-M4 core registers after reset ...

  8. wordpress入门

    安装bitnami wordpress. 打开仪表盘:开始菜单--Bitnami Wordpress协议栈 Manager Tool -- Go to Appllication -- Access W ...

  9. 最近5年183个Java面试问题列表及答案[最全]

    Java 面试随着时间的改变而改变.在过去的日子里,当你知道 String 和 StringBuilder 的区别(String 类型和 StringBuffer 类型的主要性能区别其实在于 Stri ...

  10. mexHttpBinding协议 【发布元数据终结点】

    我们需要知道很多东西才能使用微软通信基础架构(WCF)来开发应用程序.尽管这本书已经试着囊括普通开发人员需要了解的WCF所有内容,也还是有一些内容没有讨论到.附录的主要目的是填充这些罅隙. 发布元数据 ...