首页 > 其他 > 详细

09.回文数

时间:2019-09-01 16:47:47      阅读:79      评论:0      收藏:0      [点我收藏+]

题目:

技术分享图片

 

 提交01: 目的是先实现,后优化

class Solution {
    public boolean isPalindrome(int x) {
        if(x<0){
            return false;
        }
        boolean res = false;
        int[] ins = new int[20];
        int index =0;
        while(x!=0){
            ins[index++] = x%10;
            x = x/10;
        }
        int i=0,j=index-1;
        for(;i<j&&ins[i]==ins[j];i++,j--);
        if(i>=j){
            res= true;
        }
        return res;
    }
} 

技术分享图片

 

 提交02:优化

class Solution {
    public boolean isPalindrome(int x) {
        if(x<0){
            return false;
        } 
        int y=0,ty = 0;
        int tx =x;
        while(tx!=0){
            ty = tx%10;
            y=y*10+ty;
            tx = tx/10;
        }
        return x==y;
    }
} 

技术分享图片

 

09.回文数

原文:https://www.cnblogs.com/baizhuang/p/11442271.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!