zoukankan      html  css  js  c++  java
  • Palindrome Number

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

    方法一:

    public class Solution {
        public boolean isPalindrome(int x) {
            String str = x+"";
            char[] charArray = str.toCharArray();
            int size = charArray.length;
            for(int i=0;i<size/2;i++){
                if(charArray[i]!=charArray[size-1-i]){
                    return false;
                }
            }
            return true;
        }
    }

    这里用了一个额外的数组charArray,但最后也Accept了。

    方法二:

    public class Solution {
        public boolean isPalindrome(int x) {
            if(x<0){
                return false;
            }
            
            if(x==0){
                return true;
            }
            
            int e = 1;
            while(x/e>=10){
                e = e*10;
            }
            
            int highDigit,lowDigit;
            
            while(x!=0){
                highDigit = x/e;
                lowDigit = x%10;
                if(highDigit!=lowDigit){
                    return false; 
                }
                
                x = x-highDigit*e;
                x = x/10;
                e = e/100;
            }
            
            return true;
           
         
        }
    }
  • 相关阅读:
    jquery.md5
    LoginPasswordHelp
    RSA(非对称加密算法、公钥加密算法)
    Swiper 3.4.1
    layer web 弹窗
    操作系统
    查看命令帮助
    软件卸载
    重定向命令
    终端命令格式的组成
  • 原文地址:https://www.cnblogs.com/mrpod2g/p/4261166.html
Copyright © 2011-2022 走看看