【Leetcode Algorithm】Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Solution {
    public boolean isPalindrome(int x) {
        //負數不是迴文數字
        if(x<0){
            return false;
        }
        //0是迴文數字
        else if(x==0){
            return true;
        }
        //如果x爲正數
        else{
            int tmpx = x;
            int newx = 0;
            //翻轉x
            while(tmpx>0){
                newx = newx*10 + (tmpx%10);
                tmpx = tmpx/10
            }
            //判斷翻轉後的newx和x是否相同
            if(newx==x){
                return true;
            }
            return false;
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章