LeetCode_9. Palindrome Number
9. Palindrome Number
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的更多相关文章
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- 有趣的数-回文数(Palindrome number)
文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...
- 9. Palindrome Number
/* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...
- No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
- 【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 ...
- leetcode 第九题 Palindrome Number(java)
Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...
- HDU 5062 Beautiful Palindrome Number(数学)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...
- Reverse Integer - Palindrome Number - 简单模拟
第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...
- leetcode题解 9. Palindrome Number
9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...
随机推荐
- maven常用命令参数
整理了一些maven常用命令参数,以便参考:参考了maven官网和网上其他一些maven追随者的文件,不在此一一列举,但表示感谢! mvn命令参数 mvn -v, --version 显示版本信息; ...
- mysql中对表操作----为所有列插入数据
为所有列插入数据 通常情况下,向数据表中插入数据应包含表中所有字段,也就是为表中所有字段添加数据,为表中所有字段添加数据有以下两种方式. 1.INSERT语句中指定所有字段名 使用INSER ...
- 甘特图、Data Editors控件新玩法—DevExpress WPF v19.2
通过DevExpress WPF Controls,你能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案. 无论是Office办公软件的衍 ...
- poj 2762
Tarjan + TopsortTarjan 缩点Topsort 判断 Topsort 判断:在DAG中若初始状态下存在多于1个入度为0的点则说明这些 入度为0的点之间不会有路径可达若不存在入度为0的 ...
- (RERERERERERERERERERERE) BZOJ 2746: [HEOI2012]旅行问题
二次联通门 : BZOJ 2746: [HEOI2012]旅行问题 神TM STL的vector push_back进一个数后取出时就变成了一个很小的负数.. 调不出来了, 不调了 #include ...
- Mac下Maven的删除和安装
一 删除maven 找到当前的maven路劲:使用mvn -v查看当前maven的安装目录在哪 删掉sudo rm -rf [maven的路径] 二 安装maven 1.下载maven压缩包 mac下 ...
- sigaction()函数
sigaction函数 修改信号处理动作(通常在Linux用其来注册一个信号的捕捉函数) int sigaction(int signum, const struct sigaction *act, ...
- JVM——类加载
一.什么是类加载? JVM将class字节码文件加载到内存中, 并将这些静态数据转换成方法区中的运行时数据结构,在堆中生成一个代表这个类的java.lang.Class 对象,作为方法区类数据的访问入 ...
- UVALive 4394 String painter ——(区间DP)
其实这个dp过程有点似懂非懂...代码如下: #include <stdio.h> #include <algorithm> #include <string.h> ...
- CF1208C
CF1208C 这场杜老师大战tourist的比赛怎么这么多人类智慧题... 题意: 构造一个 $ n \times n $ 的矩阵,使得该矩阵每一行与每一列的元素的异或和全部相等. 解法: 异或的神 ...