9. Palindrome Number

Easy

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

package leetcode.easy;

public class PalindromeNumber {
@org.junit.Test
public void test() {
int number1 = 121;
int number2 = -121;
int number3 = 10;
PalindromeNumber palindromeNumber = new PalindromeNumber();
System.out.println(palindromeNumber.isPalindrome(number1));
System.out.println(palindromeNumber.isPalindrome(number2));
System.out.println(palindromeNumber.isPalindrome(number3));
} public boolean isPalindrome(int x) {
// Special cases:
// As discussed above, when x < 0, x is not a palindrome.
// Also if the last digit of the number is 0, in order to be a
// palindrome,
// the first digit of the number also needs to be 0.
// Only 0 satisfy this property.
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
} int revertedNumber = 0;
while (x > revertedNumber) {
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
} // When the length is an odd number, we can get rid of the middle digit
// by revertedNumber/10
// For example when the input is 12321, at the end of the while loop we
// get x = 12, revertedNumber = 123,
// since the middle digit doesn't matter in palidrome(it will always
// equal to itself), we can simply get rid of it.
return x == revertedNumber || x == revertedNumber / 10;
}
}

LeetCode_9. Palindrome Number的更多相关文章

  1. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  2. 有趣的数-回文数(Palindrome number)

    文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...

  3. 9. Palindrome Number

    /* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...

  4. No.009 Palindrome Number

    9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...

  5. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  6. leetcode 第九题 Palindrome Number(java)

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

  7. HDU 5062 Beautiful Palindrome Number(数学)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...

  8. Reverse Integer - Palindrome Number - 简单模拟

    第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...

  9. leetcode题解 9. Palindrome Number

    9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...

随机推荐

  1. C++ 正则查找

    #include <iostream> #include <regex> using namespace std; int main() { string str; getli ...

  2. partial 部分类

    partial 关键字允许把类.结构.方法或接口放在多个文件中.一般情况下,一个类全部驻留在单个文件中.但有时,多个开发人员需要访问同一个类,或者某种类型的代码生成器生成了一个类的某部分,所以把类放在 ...

  3. BZOJ 4318 OSU! (概率DP)

    题意 中文题面,难得解释了 题目传送门 分析 考虑到概率DPDPDP,显然可以想到f(i,j)f(i,j)f(i,j)表示到第iii位末尾有jjj个111的期望值.最后输出f(n+1,0)f(n+1, ...

  4. 题解 [JOI 2019 Final] 独特的城市

    题面 解析 首先有一个结论, 对一个点\(x\)有贡献的城市 肯定在它到离它较远的直径的端点的链上. 假设离它较远的端点是\(S\), 如果有一个点\(u\)不在\(x\)到\(S\)的链上, 却对\ ...

  5. Python 2--序列

  6. 评估类模型之优劣解距离法Topsis模型

    定义: TOPSIS法是一种常用的综合评价方法,其能充分利用原始数据的信息,其结果能精确地反映各评价方案之间的差距. 层次分析法的局限性: 问题和解决方案: 所以最终评分公式为: 指标正向化,得到正向 ...

  7. 字节(byte)、二进制、字节流、字符流相关概念分析

    https://blog.csdn.net/changwilling/article/details/52065955 1.字节: 字(Byte)节是长度单位.位(bit)也是长度单位. 因为计算机通 ...

  8. [Luogu] 网络

    https://www.luogu.org/problemnew/show/P3250 树链剖分 + 线段树 + 优先队列 要求未被影响的请求中最大的 所以每次将每条路径在整棵树上的补集的每个节点的优 ...

  9. Codeforces 1243 D 0-1 MST

    题面 隐隐感觉N年前做过一道类似的题. 很显然我们只需要考虑,仅有0边的子图有多少个连通块,然后这个数量减去1就是答案了(这个和kruscal过程等价). 然后其实就是妥妥的暴力了...因为1边数量非 ...

  10. lxr看代码的时候出现中文乱码问题

    修改lxr.conf 修改 , 'encoding'    => 'utf-8',为utf-8