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. ...
随机推荐
- axios封装,使用拦截器统一处理接口
1.项目路径下,引入axios.qs依赖 npm install axios npm install qs 2.在项目的src路径下新建一个commJs文件夹,在commJs文件夹里新建aps.js和 ...
- JDK源码那些事儿之红黑树基础上篇
说到HashMap,就一定要说到红黑树,红黑树作为一种自平衡二叉查找树,是一种用途较广的数据结构,在jdk1.8中使用红黑树提升HashMap的性能,今天就来说一说红黑树. 前言 限于篇幅,本文只对红 ...
- 表单文本字段预期描述(placeholder="请输入产品名称"以及prompt:'输入价格')
普通html文本标签设置: <input id="xxx" placeholder="请输入产品名称"/> 带有jQueryEasyUI插件的htm ...
- django创建路径导航
路径导航 : 1.怎样设置需要登录但又不需要验证权限的路径 : 在settings中定义一个列表,列表中以正则的方式放入需要登录但无需验证的权限的项.在 ...
- C# Contract诊断
命名空间 : using System.Diagnostics.Contracts; 属性标记 : [ContractOption(category: "runtime", set ...
- [2019牛客多校第四场][G. Tree]
题目链接:https://ac.nowcoder.com/acm/contest/884/G 题目大意:给定一个树\(A\),再给出\(t\)次询问,问\(A\)中有多少连通子图与树\(B_i\)同构 ...
- 7、组件注册-@Conditional-按照条件注册bean
7.组件注册-@Conditional-按照条件注册bean @Conditional 按照一定的条件进行判断,满足条件给容器注入bean 按照条件进行动态装配. Spring 4 开始提供的一个注解 ...
- CentOS:去掉警报声音
vi /etc/inputrc 然后将set bell-style none前面的#删掉 none 改为off :wq 保存退出 vim /etc/bashrc 在开始的地方加上一句 setterm ...
- jvm参考(生产使用)
#4g JAVA_OPTS=-Xms3g -Xmx3g -XX:+PrintFlagsFinal -XX:+UnlockDiagnosticVMOptions -XX:NewRatio=2 -XX:P ...
- vue编辑、新增弹框(引用外部页面)
vue编辑.新增弹框(引用外部页面) 2018年06月15日 09:37:20 会飞的猪biubiu 阅读数 10265 版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...