leetcode刷题41

今天刷的题是LeetCode第9题,判断一个数是不是回文数

这个题还是比较简单的,一次就通过了,可以看到执行效率比我之前写的还是高很多

我就把两次的代码都贴出来吧

public static boolean solution(int x){
        if (x<0)return false;
        String string =String.valueOf(x);
        int left=0;
        boolean flag=true;
        int right=string.length()-1;
        while (left<right){
            if (string.charAt(left)!=string.charAt(right)){
                flag=false;break;
            }
            left++;
            right--;
        }
        return flag;
    }
class Solution {
    public boolean isPalindrome(int x) {
        if (x<0){
            return false;
        }else if(x==0){
            return true;
        }else if(x%10==0){
            return false;
        }else {
            int num=0;
            int y=x;
            while (y!=0){
                num=num*10+y%10;
                y=y/10;
            }
            if (num==x){
                return true;
            }else {
                return false;
            }
        }
    }
}

原文地址:https://www.cnblogs.com/cquer-xjtuer-lys/p/11611638.html