LeetCode--No.9--Palindrome Number

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

click to show spoilers.

Palindrome Number的意思就是:对称的数字。负数不算。

public class Solution {
    public boolean isPalindrome(int x) {
        if (x<0)
            return false;
        int k = 0;
        int y = x;
        while(y>0){
            y /= 10;
            k++;
        }
        for(int i = 1; i <= k/2; i++){
            int j = k+1-i;
            if(digitAt(x,i) != digitAt(x,j))
                return false;
        }
        return true;
    }
}

 但是关于这个代码有一个问题。如果最后一个函数写成这样。。。就会报错。说不能double 转int,但是这是为什么呢??来个大神告诉我吧=。=oh my god。 
   
    public int digitAt (int x, int n){
        x = x/Math.pow(10,n-1);
        return x%10;
    }


Anyway,这个问题就先记下来吧。今天的刷题任务基本结束。
我好像知道了。Math.pow这个函数的返回值是double。 Math.pow(2.5,2)这样也可以。
如果需要转换为int的话,需要 x = (int) (x/Math.pow(10,n-1));
如果是int/int的话,结果依然是int。但如果int与double之间进行运算的话,结果为double
施廷懋和何姿加油加油!!

你可能感兴趣的:(LeetCode--Easy)