LeetCode第九题—— 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.

My solution(10ms,36.5MB)

整形转换成字符型,然后进行颠倒转换

class Solution {
    public boolean isPalindrome(int x) {
        if(x<0){
            return false;
        }else{
            String a = x + "";
            String aReverse = new StringBuilder(a).reverse().toString();
            if(a.equals(aReverse)){
                return true;
            }else{return false;}
        }
    }
}

Other solution(6ms,35.1MB):

这个方法很神奇啊!Ψ( ̄∀ ̄)Ψ

先排除负数,然后把整数分成两个部分,x为前一半,rev为后一半,例:32499423

并且rev还是已经倒转过后的数字,即3249

最后比较一下两个是否相同



如果是奇数个数字,例:12321

则rev为123,x为12

然后比较123/10=12和12是否相等

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

原文地址:https://www.cnblogs.com/mgblog/p/10919361.html

时间: 2024-10-06 13:03:43

LeetCode第九题—— Palindrome Number(判断回文数)的相关文章

(LeetCode)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 ex

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 9. 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 space. You

LeetCode 9 Palindrome Number (回文数)

翻译 确定一个整数是否是回文数.不能使用额外的空间. 一些提示: 负数能不能是回文数呢?(比如,-1) 如果你想将整数转换成字符串,但要注意限制使用额外的空间. 你也可以考虑翻转一个整数. 然而,如果你已经解决了问题"翻转整数(译者注:LeetCode 第七题), 那么你应该知道翻转的整数可能会造成溢出. 你将如何处理这种情况? 这是一个解决该问题更通用的方法. 原文 Determine whether an integer is a palindrome. Do this without ex

LeetCode OJ Palindrome Number(回文数)

1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 int r=0,init=x; 5 if(init==0) return true; 6 if(init<0) return false; 7 while(init!=0){ 8 r=r*10+init%10; 9 init=init/10; 10 } 11 if(r==x) 12 return true; 13 else 14 return false; 15 } 16 };

LeetCode 第九题, 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 restriction of usi

leetcode第九题--Palindrome Number

Problem: 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

LeetCode 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

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha