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. ...
随机推荐
- SQL server 语句执行分析
- 登录授权、TCP/IP、HTTPS
今天继续纯理论的东东,比较枯燥,但是又很重要,坚持.. 登录和授权 登录和授权的区别: 登录:身份认证,即确认「你是你」的过程. 授权:由身份或持有的令牌确认享有某些权限(例如获取用户信息).登录过程 ...
- springboot中,使用redisTemplate操作redis
知识点: springboot中整合redis springboot中redisTemplate的使用 redis存数据时,key出现乱码问题 一:springboot中整合redis (1)pom. ...
- 一款超好用的第三方评论插件--Gittalk
使用GITALK的背景: 1. 最近在做一个基于Java的个人博客系统,已经基本完工了,突然发现怎么没有评论的操作,如果再从头开始从数据库开始写的话,花费的代价有点大,于是乎我就在网上寻找一款适合我的 ...
- 用Qt 画一个心形
MainWindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTi ...
- Jest自动化测试
最近一直很苦恼,测试这一块的内容,一直想提升测试效率,从慕课网了解到了自动化测试jest ,准备试下下,记录一下学习笔记,方便别人使用 1 什么是 Jest? Jest是 Facebook 的一套开源 ...
- 使List<userClass>.Contains可以查找重復的對象
List.Contains实现对比 http://blog.csdn.net/yswucn/article/details/4091469
- AbstractWrapper ,EntityWrapper, QueryWrapper, UpdateWrappe
https://blog.csdn.net/qq_42112846/article/details/88086035 https://blog.csdn.net/m0_37034294/article ...
- php foreach 中使用&时注意
结果: 没看懂的可以参考 :https://blog.csdn.net/ghostyusheng/article/details/79925351
- lodop打印设计
<template> <div class="dashboard-container"> <form id="form1"> ...