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的更多相关文章

  1. Leetcode 9. Palindrome Number(水)

    9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...

  2. leetcode 9 Palindrome Number 回文数

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

  3. LeetCode 9. Palindrome Number (回文数字)

    Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...

  4. Leetcode 9. Palindrome Number(判断回文数字)

    Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...

  5. [LeetCode][Python]Palindrome Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...

  6. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  7. [LeetCode] 9.Palindrome Number - Swift

    Determine whether an integer is a palindrome. Do this without extra space. 题目意思:判断一个整数是否是回文数 例如:1232 ...

  8. [LeetCode] 9. Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  9. 【leetcode】Palindrome Number

    题目简述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...

随机推荐

  1. 基于 Unity 的一种透明通道压缩处理

    由于 Android 平台各种硬件标准的不统一,为了开发的软件项目能够在大部分 Android 机上完美运行,我们需要以较差的硬件支持为基础做准备. Android 平台基本上都支持对不带 Alpha ...

  2. Eureka集群

    Eureka集群搭建 高可用集群配置 当注册中心扛不住高并发的时候,这时候 要用集群来扛: 普通操作 我们再新建两个module  microservice-eureka-server-2002  m ...

  3. Selenium(十四):自动化测试模型介绍、模块化驱动测试案例、数据驱动测试案例

    1. 自动化测试模型介绍 随着自动化测试技术的发展,演化为了集中模型:线性测试.模块化驱动测试.数据驱动测试和关键字驱动测试. 下面分别介绍这几种自动化测试模型的特点. 1.1 线性测试 通过录制或编 ...

  4. flush() 缓冲输出流的缓冲区问题

    package seday06; import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.io. ...

  5. Java的三种代理模式&完整源码分析

    Java的三种代理模式&完整源码分析 参考资料: 博客园-Java的三种代理模式 简书-JDK动态代理-超详细源码分析 [博客园-WeakCache缓存的实现机制](https://www.c ...

  6. MySQl看这一篇就够了

    MySQL分享 一.数据库结构 语句 DDL(Data Definition Languages):数据定义语句,常用的语句关键字主要包括 create.drop.alter等操作表结构 DML(Da ...

  7. word-break、word-wrap、white-space区别

    <div id="box"> Hi  , This is a incomprehensibilities long word. </br> 你好  , 这 ...

  8. angularjs实现购物车批量删除,filter模糊查询,排序

    数据源 $scope.data=[ {num:1234,name:"ipad",price:3400.00,count:10}, {num:1235,name:"ipho ...

  9. linux服务器下oracle开机自启动设置

    1.首先切换到Oracle用户 [oracle@oracletest ~]$ cd /u01/app/oracle/product/11.2.0/db_1/bin/       -----标红字体部分 ...

  10. Python语法速查: 2. 列表、元组、字典、集合操作

    返回目录 (1)通用序列操作 “序列”表示索引为非负整数的有序对象集合,列表.元组.字符串都属于序列.区别在于:列表是可变的,而元组和字符串是不可变的.序列的通用操作他们都可以用. 操作或方法 简述 ...