LeetCode 09 判断回文数字

9. Palindrome Number    难度:Easy

 

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.

这个挺简单,判断一个数字是不是回文数字,前几天在 LeetCode 05 解题过程中也写了一个类似的函数。

l

package pers.leetcode;

/**
 * LeetCode 09 判断回文数字
 * 难易程度: Easy
 *
 * @author 朱景辉
 * @date 2019/3/20 11:39
 */
public class PalindromeNumber {
    public static void main(String[] args) {
        int x = 10;
        System.out.println(isPalindrome(x));
    }

    /**
     * 判断回文数字
     *
     * @param x int 型数字
     * @return boolean
     */
    public static boolean isPalindrome(int x){
        if (x < 0)
            return false;
        String str = x + "";
        for (int i=0; i<str.length()/2; i++){
            if (str.charAt(i) != str.charAt(str.length()-i-1))
                return false;
        }
        return true;
    }
}

 

 

LeetCode 09 判断回文数字