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)的解题思路。Java代码如下:

/**
 * 功能说明:LeetCode 9 - Palindrome Number
 * 开发人员:Tsybius
 * 开发时间:2015年9月24日
 */
public class Solution {
    
    /**
     * 判断某数是否为回文数
     * @param x 数字
     * @return true:是,false:否
     */
    public boolean isPalindrome(int x) {
        
        //负数不能为回文数
        if (x < 0) {
            return false;
        }
        
        //反转数字观察是否相等
        if (x == reverse(x)) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 反转数字
     * @param x 被反转数字
     * @return 反转后数字
     */
    public int reverse(int x) {
        
        long result = 0;

        while (x!=0) {
            result = result * 10 + x % 10;
            x /= 10;
        }
        
        return (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) ? 0 : (int)result;
    }
}

5、解题方法2

另一个方法是将数字转换为字符串,再通过StringBuilder类反转字符串,判断两字符串相等。Java代码如下:

/**
 * 功能说明:LeetCode 9 - Palindrome Number
 * 开发人员:Tsybius
 * 开发时间:2015年9月24日
 */
public class Solution {
    
    /**
     * 判断某数是否为回文数
     * @param x 数字
     * @return true:是,false:否
     */
    public boolean isPalindrome(int x) {

        if (x < 0) return false;
        if (x < 10) return true;
        
        String str1 = String.valueOf(x);
        String str2 = (new StringBuilder(str1)).reverse().toString(); 
        
        if (str1.equals(str2)) {
            return true;
        } else {
            return false;
        }
    }
}

6、解题方法3

还有一个方法,就是直接转换为字符串,然后再分别对字符串对称位置字符进行比较。Java代码如下:

/**
 * 功能说明:LeetCode 9 - Palindrome Number
 * 开发人员:Tsybius
 * 开发时间:2015年9月24日
 */
public class Solution {
    
    /**
     * 判断某数是否为回文数
     * @param x 数字
     * @return true:是,false:否
     */
    public boolean isPalindrome(int x) {

        if (x < 0) return false;
        if (x < 10) return true;
        
        String str = String.valueOf(x);
        for (int i = 0; i < str.length() / 2; i++) {
            if (str.charAt(i) != str.charAt(str.length() - i - 1)) {
                return false;
            } 
        }
        
        return true;
    }
}

END

时间: 2024-08-02 02:48:42

LeetCode:Palindrome Number - 回文数的相关文章

leetcode 9 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 using ext

LeetCode Problem 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 could a

Leetcode——3 Palindrome Number(回文数)

Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过,不过从基础开始尝试吧. Solution: public class Solution { public boolean isPalindrome(int x) { int n=1; int copyx=x; if(x<0)return false; if(x<10)return true; w

[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

Palindrome Number 回文数

判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出. 1 public class Solution { 2 public boolean isPalindrome(int x) { 3 if(x<0) return false; 4 5 int temp = x, reverseX = 0; 6 while (temp > 0) { 7 reverseX

9. Palindrome Number 回文 my second leetcode 20170807

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 ext

LeetCode 9 Palindrome Number 回文数字

题目:Determine whether an integer is a palindrome. Do this without extra space. 翻译:判断一个数字是否是回文数,不要额外空间. 解题思路:因为数字既然传过去了,就不会有越界的问题.每次只需要取最前面和最后面的那一位数字进行比较,相同则继续,不同则返回. 首先要获取数字的位数,假设数字是12344321,一共有8位. 其次是要每次取前后各一位来进行比较,用数字除以1后面7个0得到第一位,用数字对10取余数得到最后一位. 此

LeetCode Golang 9.回文数

9. 回文数 第一种办法 :itoa 转换为字符串进行处理: package main import ( "strconv" "fmt" ) //判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. // //示例 1: // //输入: 121 //输出: true //示例 2: // //输入: -121 //输出: false //解释: 从左向右读, 为 -121 . 从右向左读, 为 121- .因此它不是一个回文数.

Leetcode 479.最大回文数乘积

最大回文数乘积 你需要找到由两个 n 位数的乘积组成的最大回文数. 由于结果会很大,你只需返回最大回文数 mod 1337得到的结果. 示例: 输入: 2 输出: 987 解释: 99 x 91 = 9009, 9009 % 1337 = 987 说明: n 的取值范围为 [1,8]. 1 class Solution { 2 public int largestPalindrome(int n) { 3 if(n == 1) return 9; 4 int upper = (int)Math.