题目

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. JDBC之 自增长与事务

    JDBC之 自增长与事务 1.自增长 有这样一个现象:数据库中有两个表格 学生表(学生姓名,所在班级),班级表(班级号(自增长的主键),班级人数). 现在我往班级表插入一条信息, 只提供班级人数,班级 ...

  2. 懒人的福利?教你用set维护斜率优化凸包

    斜率优化题目大家肯定都做得不少了,有一些题目查询插入点的x坐标和查询斜率都不单调,这样就需要维护动态凸包并二分斜率.(例如bzoj1492) 常规的做法是cdq分治或手写平衡树维护凸包,然而如果我不愿 ...

  3. Docker系列之(一):10分钟玩转Docker

    1.前言 进入云计算的时代,各大云提供商AWS,阿里云纷纷推出针对Docker的服务,现在Docker是十分火爆,那么Docker到底是什麽,让我们来体验一下. 2.Docker是什麽 Docker是 ...

  4. 如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接(转)

    前期准备: 关于什么是Hierarchy Viewer,请查看官方文档:http://developer.android.com/tools/debugging/debugging-ui.html.个 ...

  5. C# 读带复选框的excel,写excel并设置字体、边框、背景色

    这是一篇excel读写功能大汇总,通过C#调用Microsoft.Office.Interop.Excel.dll才能完成任何复杂格式excel的读写操作. 本文需要读取的excel格式如下: 可见表 ...

  6. LightOJ 1366 - Pair of Touching Circles (统计矩形内外切圆对)

    1366 - Pair of Touching Circles   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limi ...

  7. SourceTree 的初次使用的两个小问题

    菜鸟才开始使用SourceTree,出现了两个小问题,特此整理一下,希望对各位新手有帮助.刚开始以为装了SourceTree就不用装git了,其实不然,不装git就会出现下面第一个问题: 1.新手使用 ...

  8. svn 迁移到 git 仓库并保留 commit 历史记录

    1.svn 转换为 git(会提示,让你输入先前 svn 的账号与密码) # 切换至 本地项目目录 cd /Users/jianbao/PhpStormProjects/fiisoo/ # 克隆 sv ...

  9. delphi project of object

    http://www.cnblogs.com/ywangzi/archive/2012/08/28/2659811.html 其实要了解这些东西,适当的学些反汇编,WINDOWS内存管理机制,PE结构 ...

  10. Spring Boot 2.0 + zipkin 分布式跟踪系统快速入门

    原文:https://www.jianshu.com/p/9bfe103418e2 注意 Spring Boot 2.0之后,使用EnableZipkinServer创建自定义的zipkin服务器已经 ...