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. LR中获取当前系统时间方法

    方法一:使用loadrunner的参数化获取当前时间使用lr的参数化,非常方便,对lr熟悉的各位朋友也能马上上手,时间格式也有很多,可以自由选择.步骤:1.将复制给aa的值参数化2.选中abc,使用右 ...

  2. 256 terabytes random-access memory

    Computer Systems A Programmer's Perspective Second Edition As we will discuss, the extension of IA32 ...

  3. breadth-first depth-first best-first

    Computer Science An Overview _J. Glenn Brookshear _11th Edition For our example in Figure 11.7, we c ...

  4. Delphi 如何清除动态数组的内存?

    SetLength(glb_IndexConfig,); FreeAndNil(glb_IndexConfig);

  5. 一 mybatis快速入门

    什么是MyBatis MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架. MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索. MyBatis可以使用简 ...

  6. .Net使用CDO发送邮件,需安装注册的组件

    regsvr32 C:\Program Files\Common Files\System\ado\msado15.dll regsvr32 CDOEX.DLL

  7. DOCTYPE、指定语言、字符集

    <!DOCTYPE html> 在HTML的最开始部分声明DOCTYPE文档类型,可以让浏览器或其他用户代理知道你要使用的Html语言类型:无论你打算使用何种类型的HTML语言,DOCTY ...

  8. 一切从IL开始

    IL是什么? IL是Intermediate Language的缩写,是.Net代码转化成机器语言的一个中间语言,因此又把IL语言称之为反汇编语言. IL工具有哪些? 俗话说,工欲善其事必先利其器.了 ...

  9. H264(NAL简介与I帧判断)

    1.NAL全称Network Abstract Layer, 即网络抽象层.         在H.264/AVC视频编码标准中,整个系统框架被分为了两个层面:视频编码层面(VCL)和网络抽象层面(N ...

  10. MAC下配置ZSH

    Mac的Terminal出了bash还配备了zsh模式,相比于bash,zsh的界面更加简单精致,用户名直接省略,用一个小箭头代替,而且箭头的颜色还可以指示命令的对错:路径和文件名的自动补全功能也十分 ...