palindrome number

1. Question

确定一个数是否是回文数。要求不使用额外空间。

Determine whether an integer is a palindrome. Do this without extra space.

2. Solution

如果是负数,就不是回文数。

2.1 reverse integer

采用反转整数的方法,注意处理溢出(采用long存储数据)

public class Solution {
    public boolean isPalindrome(int x) {
        if( x < 0 )
            return false;
        long rev = 0;
        int y = x;
        while( y != 0 ){
            rev = rev * 10 + y % 10;
            y = y / 10;
        }
        if( rev == (long)x )
            return true;
        return false;
    }
}

2.2 整除取余计算

结合采用整除取余,同时从数的两端开始计算来判断(需要先确定位数)

public class Solution {
    public boolean isPalindrome(int x) {
        if( x < 0 )
            return false;
        int bits = 1;
        for( int y=x/10; y!=0; y/=10,bits++ );
        for( int i=1; i<=bits/2; i++ ){
            if( ( ( x / (int)Math.pow( 10, bits-i ) ) % 10 )  != ( ( x / (int)Math.pow( 10, i-1 ) ) % 10 ) )
                return false;
        }
        return true;
    }
}

3. 复杂度

O(n)

时间: 2024-10-21 16:58:45

palindrome number的相关文章

Palindrome Number (回文数)

回文数是指这样的数字:正读和倒读都是一样的.如:595,2332都是回文数,234不是回文数. 注意:负数不是回文数 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

LeetCode【9】. Palindrome Number --java的实现

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra spa

Leetcode 数 Palindrome Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Palindrome Number Total Accepted: 12165 Total Submissions: 41736 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integ

【LeetCode】Palindrome Number

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restri

65. Reverse Integer &amp;&amp; Palindrome Number

Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alr

[LeetCode] [Palindrome Number 2012-01-04]

Determine whether an integer is a palindrome. Do this without extra space. if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the r

LeetCode 009 Palindrome Number

[题目] Determine whether an integer is a palindrome. Do this without extra space. [题意] 题意判断一个整数是否是回文数 注意一下几点: 1. 不能用额外的空间 2. 负数不是回文数 [思路1] 依次比较首位值,需要注意处理类似1002001这样的数,当比较完首位值之后余下的数变成200, 2之前的两个0会自动清除. [代码] class Solution { public: int _size(int x){ int

【Leet Code】Palindrome Number

Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an integer is a palindrome. Do this without extra space. 判断一个数整数是不是回文?例如121,1221就是回文,好吧,直接利用前面写过的[Leet Code]Reverse Integer--"%"你真的懂吗? 不过这里要考虑翻转后,数值

Palindrome Number leetcode java

题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using