Palindrome Number

题目:

Check a positive number is a palindrome or not.

A palindrome number is that if you reverse the whole number you will get exactly the same number.

分析:

这个题目有一个比较坏的地方,如果你直接用arg做,就会改变原始的arg,使得他无法用来和res比较,必须准备一个old copy。

解法:

public class Solution {
    /**
     * @param num a positive number
     * @return true if it's a palindrome or false
     */
    public boolean palindromeNumber(int num) {
        // Write your code here
        int res = 0;
        int oldNum = num;
        while (num != 0) {
            int temp = num % 10;
            num /= 10;
            res = res * 10 + temp;
        }
        return (res == oldNum);
    }
}

results matching ""

    No results matching ""