[leetcode]Palindrome Number

题目描述如下:

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

Easy题,判断一个整数是否为回文的。

当然可以将int型的数字转换为string类型操作,这里不表。

不采用string方法,可以使用一个数来标定给定数的位数,有了位数之后判断其实不难。

代码如下:

public class Solution {
    public boolean isPalindrome(int x) {
        if(x < 0) return false;
        int i = 1, j = 1, digit;
        boolean flag = true;
        digit = String.valueOf(x).length();
        while(digit > 1){
            i *= 10;
            digit --;
        }
        while(i >= j){
            int head = x / i % 10;
            int tail = x / j % 10;
            if(head != tail){
                flag = false;
                break;
            }
            i /= 10;
            j *= 10;
        }
        if(flag) return true;
        else return false;
    }
}

题目链接:https://leetcode.com/problems/palindrome-number/

你可能感兴趣的:(LeetCode)