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

尝试用两头分别比较的方法,结果发现无法解决1000021这种问题

 public class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false;
int lastbit=x%10;
int firstbit;
int num=x;
int bits=0;
while(num/10!=0){
num=num/10;
bits++;
}
firstbit=num;
if(firstbit!=lastbit) return false;
else{
x=x-firstbit*((int)(Math.pow(10,bits)));
x=x/10;
if(x==0) return true;
else return isPalindrome(x);
}
}
}

查看了论坛的解答,看到一个很好的solution, 它怎么想到13行的 div/=100的,给跪了,这样刚好解决了中间有0的问题,比如1021会被判定为false, 而121会被判定为true。 解答详见:http://leetcode.com/2012/01/palindrome-number.html

 public class Solution {
public boolean isPalindrome(int x) {
if (x < 0) return false;
int div = 1;
while (x / div >= 10) {
div *= 10;
}
while (x != 0) {
int l = x / div;
int r = x % 10;
if (l != r) return false;
x = (x % div) / 10;
div /= 100;
}
return true;
}
}

Leetcode: Palindrome Numbers的更多相关文章

  1. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  2. POJ2402/UVA 12050 Palindrome Numbers 数学思维

    A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...

  3. LeetCode: Palindrome Partition

    LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...

  4. [LeetCode] Palindrome Partitioning II 解题笔记

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

  5. Palindrome Numbers(LA2889)第n个回文数是?

     J - Palindrome Numbers Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu ...

  6. Uva - 12050 Palindrome Numbers【数论】

    题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然 ...

  7. LeetCode: Palindrome 回文相关题目

    LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...

  8. E - Palindrome Numbers

    题目链接:https://vjudge.net/contest/237394#problem/E A palindrome is a word, number, or phrase that read ...

  9. UVa 12050 - Palindrome Numbers (回文数)

    A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, th ...

随机推荐

  1. The "get" method should be used when the form is idempotent---正交的两个概念---

    https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.1 17.13.1 Form submission method The me ...

  2. Peeking into Apache Flink's Engine Room

    http://flink.apache.org/news/2015/03/13/peeking-into-Apache-Flinks-Engine-Room.html   Join Processin ...

  3. 在Delphi下,如何使ShowMessage中的按钮中文化

    如果你使用messagedlg(对showmessage也适用)可以汉化定义按钮caption的常量,具体操作步骤如下: 1.   打开文件consts.pas(在Delphi安装目录的/source ...

  4. ubuntu挂载其他分区到/home下,将当前分区内容替换

    有时候,我们装系统时,可能因为没注意,把某一个分区分小了,导致到最后,我们的那个盘容不下了, 这时,面临的两个选择就是:要么卸载一些软件,要么重新分区,重装系统,其实,还可以这样,去把其他 多余的盘分 ...

  5. CSS :before和:after (转)

    前几天的晚上较全面的去看了下css的一些文档和资料,大部分的样式运用都没什么大问题了,只是有些许较陌生,但是也知道他们的存在和实现的是什么样式.今天主要想在这篇学习笔记中写的也不多,主要是针对:bef ...

  6. JS 去字符串空格 总结

    str为要去除空格的字符串: 去除所有空格: str = str.replace(/\s+/g,""); 去除两头空格: str = str.replace(/^\s+|\s+$/ ...

  7. php练习:给指定商品添加特殊效果

    老板说: 我只要在文本框填1|3 ,那第1和第3个商品都要标注! 思考过程: 有了重要信息1和3,而循环商品时$key按顺序自动递增,那么只要key和信息匹配的话,就判断商品添加标注,这个可以把位置信 ...

  8. [Stanford 2011] MVC introduction

    以下是课程笔记,仅供以后复习之便. 1.什么是MVC? (1) Model:如飞机激战的游戏中,太空中的飞船,什么机型,每个飞船有多少机枪,多少护甲,这些硬件组成是model. (2)Controll ...

  9. ArcGIS API for Silverlight 地图元素点闪烁,线流动显示的处理方式

    原文:ArcGIS API for Silverlight 地图元素点闪烁,线流动显示的处理方式 <Grid x:Name="LayoutRoot" Background=& ...

  10. JS-001-单选复选按钮操作

    此文主要针对 web 页面中常见元素(例如:单选按钮.复选按钮)的 JavaScript 操作,进行简单的源码示例演示,敬请小主们参阅.若有不足之处,敬请大神指正,不胜感激! 话不多言了,直接上码: ...