Leetcode练习题 Palindrome Number
9. Palindrome Number
Question:
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?
Solution:
解法一:
使用回文数解法,将输入x值分割存储在数组中,然后再取出判断。
class Solution {
public boolean isPalindrome(int x) {
if(x<0)
{
return false;
}
if(x==0)
{
return true;
}
if(x>0)
{
String string= x+"";
int length = string.length();
int[] index = new int[length];
int reserve = 0;
int i =0 ;
while(x!=0)
{
index[i++] = x%10;
x/=10;
}
for(int j=0;j<length/2;j++)
{
if(index[j]!=index[length-1-j])
{
return false;
}
}
return true;
}
return false;
}
}
解法二:
将x转化成字符串,然后利用charAt分割求character类型,在进行比较;虽然不符合题意,但是用到双指针知识,个人认为比较简洁。
public boolean isPalindrome(int x) {
String str = String.valueOf(x);
int start = 0;
int end = str.length() - 1;
while(start < end){
if(str.charAt(start++) != str.charAt(end--)) return false;
}
return true;
}
解法三:
转换一半的数字:
Revert half of the number
Intuition
The first idea that comes to mind is to convert the number into string, and check if the string is a palindrome, but this would require extra non-constant space for creating the string which is not allowed by the problem description.
Second idea would be reverting the number itself, and then compare the number with original number, if they are the same, then the number is a palindrome. However, if the reversed number is larger than \text{int.MAX}int.MAX, we will hit integer overflow problem.
Following the thoughts based on the second idea, to avoid the overflow issue of the reverted number, what if we only revert half of the int number? After all, the reverse of the last half of the palindrome should be the same as the first half of the number, if the number is a palindrome.
For example, if the input is 1221, if we can revert the last part of the number "1221" from "21" to "12", and compare it with the first half of the number "12", since 12 is the same as 12, we know that the number is a palindrome.
Let's see how we could translate this idea into an algorithm.
Algorithm
First of all we should take care of some edge cases. All negative numbers are not palindrome, for example: -123 is not a palindrome since the '-' does not equal to '3'. So we can return false for all negative numbers.
Now let's think about how to revert the last half of the number. For number 1221, if we do 1221 % 10, we get the last digit 1, to get the second to the last digit, we need to remove the last digit from 1221, we could do so by dividing it by 10, 1221 / 10 = 122. Then we can get the last digit again by doing a modulus by 10, 122 % 10 = 2, and if we multiply the last digit by 10 and add the second last digit, 1 * 10 + 2 = 12, it gives us the reverted number we want. Continuing this process would give us the reverted number with more digits.
Now the question is, how do we know that we've reached the half of the number?
Since we divided the number by 10, and multiplied the reversed number by 10, when the original number is less than the reversed number, it means we've processed half of the number digits.
public boolean isPalindrome(int num){
if(num < 0) return false;
int reversed = 0, remainder, original = num;
while(num != 0) {
remainder = num % 10; // reversed integer is stored in variable
reversed = reversed * 10 + remainder; //multiply reversed by 10 then add the remainder so it gets stored at next decimal place.
num /= 10; //the last digit is removed from num after division by 10.
}
// palindrome if original and reversed are equal
return original == reversed;
}
Leetcode练习题 Palindrome Number的更多相关文章
- Leetcode 9. Palindrome Number(水)
9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- [LeetCode][Python]Palindrome Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- [LeetCode] 9.Palindrome Number - Swift
Determine whether an integer is a palindrome. Do this without extra space. 题目意思:判断一个整数是否是回文数 例如:1232 ...
- [LeetCode] 9. Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【leetcode】Palindrome Number
题目简述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...
随机推荐
- Docker-compose安装和应用
安装 1.为什么要使用docker-compose? 使用 Docker Compose 可以轻松.高效的管理容器,它是一个用于定义和运行多容器 Docker 的应用程序工具 2.其他前置条件需要 ...
- mybatis入门视频总结
1.基础知识 2. 原生态jdbc问题总结 3.mybatis是什么 4.工程结构 5.SqlMapConfig.xml文件 6.sqlmap 包下新建user.xml文件(namespace,par ...
- linux上安装jenkins过程
最近在学到jenkins分布式构建时,需要一台部署jenkins的主机服务器master,自己用的win10作为slave,所以我想在虚拟机上先部署jenkins. centos还是ubuntu呢,算 ...
- 观察者模式Vs发布订阅模式
1)观察者模式 观察者模式通俗的讲就是我们平事件调用(click/change等等) 大家先看这个图片.我们被观察者Subject(监听某个事件)发生改变时,观察者Observer监听到没改变做出调整 ...
- [译]OpenSSL Cookbook
记录个人学习过程吧,顺便翻译一下.另外,本文并不会包括原连接中的所有内容,仅包括个人在工作中会经常遇到的. 参考:OpenSSL Cookbook 前言 由于协议特性和实现的复杂性,有时很难确定安全服 ...
- go语言的错误处理
1.系统自己抛异常 //go语言抛异常 func test3_1() { l := [5] int {0,1,2,3,4} var index int = 6 fmt.Println(l) l[ind ...
- ASP.NET中的身份验证
身份验证方式windows passport form none授权allow 允许deny 拒绝特殊符号 *代表所有用户 ?代表匿名用户跳转配置<system.web><autho ...
- Linux系统:centos7下搭建ZooKeeper3.4中间件,常用命令总结
本文源码:GitHub·点这里 || GitEE·点这里 一.下载解压 1.Zookeeper简介 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供 ...
- 蓝色大气简约立体答辩ppt模板推荐
小编个人非常喜欢这个模版,大气深蓝色,具有科技感,非常适合学生的毕业答辩PPT模板. 模版来源:http://ppt.dede58.com/gongzuohuibao/26496.html
- 关于javascript中变量及函数的提升
javascript中变量以及函数的提升,在我们平时的项目中其实还是挺常用的,尤其是大型项目中,不知不觉就会顺手添加一些变量,而有时候自己的不小心就会酿成一些不必要错误,趁有时间整理一下自己对于js中 ...