题目

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. 网页图表Highcharts实践教程之图表区

    网页图表Highcharts实践教程之图表区 网页图表Highcharts图表区 图表区是图表的基本区域.所有的数据和图形都是绘制在图表区中.从图形绘制范围来分,图表区域分为外层图表区和绘图区.本章将 ...

  2. 【BZOJ 3027】 3027: [Ceoi2004]Sweet (容斥原理+组合计数)

    3027: [Ceoi2004]Sweet Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 71  Solved: 34 Description John ...

  3. BZOJ.2007.[NOI2010]海拔(最小割 对偶图最短路)

    题目链接 想一下能猜出,最优解中海拔只有0和1,且海拔相同的点都在且只在1个连通块中. 这就是个平面图最小割.也可以转必须转对偶图最短路,不然只能T到90分了..边的方向看着定就行. 不能忽略回去的边 ...

  4. [HDU5714]拍照

    [HDU5714]拍照 题目大意: 河上有\(n(n\le10^4)\)个船只,小明希望把尽可能多的船只都完整地拍到一张照片中. 小明位于河的边上,并且可以在河边的任意位置进行拍照,照相机的视野恰好为 ...

  5. C# 读取CSV和EXCEL文件示例

    我们习惯了直接连到数据库上面读取数据表的数据内容: 如果有一天我们需要读取CSV,EXCEL文件的内容的时候,可不可以也像读数据表的方式一样呢?当然可以,使用OleDB ADO.NET是很简单的事情 ...

  6. SPI中的极性CPOL和相位CPHA

    详解SPI中的极性CPOL和相位CPHA SPI由于接口相对简单(只需要4根线),用途算是比较广泛,主要应用在 EEPROM,FLASH, 实时时钟,AD转换器,还有数字信号处理器和数字信号解码器之间 ...

  7. 虫趣:BAD POOL CALLER (par1: 0x20)

    [作者:张佩] [原文:http://www.yiiyee.cn/Blog/0x19-1/] 内核在管理内存的时候,为了提高内存使用效率,对于小片内存的申请(小于一个PAGE大小),都是通过内存池来操 ...

  8. Revit API注册事件

    start using Autodesk.Revit.DB.Events; //http://revit.haotui.com [Autodesk.Revit.Attributes.Transacti ...

  9. ssh 多条命令执行

    格式:ssh user@ip command 单条命令:ssh user@ip command1 多条命令:ssh user@ip "command1;command2" 不加双引 ...

  10. DataGridView 在 WinForms中应用不同的单元格式

    /// <summary> /// Set the cell background colour to make the ups and downs more visible. /// & ...