Solution 1:简单粗暴的转换成String再转成数组

class Solution {
    public boolean isPalindrome(int x) {
        if(x<0)return false;
        char []a=String.valueOf(x).toCharArray();
        int i=0;
        while(i<a.length){
            if(a[i]!=a[a.length-i-1]){
                return false;
            }
            i++;
        }
        
        return true;
       
    }
}

Solution 2:直接对数字进行判断,这种方法非要快

[leetcode]9. Palindrome Number

class Solution {
    public boolean isPalindrome(int x) {
        if(x<0)return false;
        
        int m=0;
        int temp=x;
       while(temp!=0){
           m++;
           temp/=10;
       }
        
        int a=0,b=0;
        while(m>0){
            a=x/(int)Math.pow(10,m-1);
            b=x%10;
          
            if(a!=b)return false;
            
            x=x%(int)Math.pow(10,m-1)/10;
            m=m-2;
        }
       return true;
    }
}

相关文章:

  • 2021-09-17
  • 2021-11-09
  • 2021-12-12
  • 2021-11-01
  • 2022-01-19
  • 2021-08-12
  • 2022-01-18
  • 2021-11-17
猜你喜欢
  • 2021-08-16
  • 2021-12-12
  • 2021-09-22
  • 2021-04-02
  • 2021-07-24
  • 2021-08-03
  • 2021-11-25
相关资源
相似解决方案