LeetCode(9)判断回文数

问题:

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Coud you solve it without converting the integer to a string?

在真实面试中也是遇到过的问题。

采用java解决

class Solution {
    /**
     * 思路
     * 1,创建一个list集合,遍历原数的每一位,推入集合
     * 2,遍历集合,判断每一位与最后一位回退的数是否相等,不等直接返回false
     * 3,最后直接返回true
     */
    //创建存放每一位的集合
    private List list = new ArrayList();
    //创建推入集合每位数的方法
    public void pushInt(int x){
        if(x < 10){
            this.list.add(x);
        }else{
            while(x != 0){
                this.list.add(x % 10);
                x /= 10;
            }
        }
    }
    //创建遍历集合,判断是否为回文数的方法
    public boolean isListPalindrome(int x){
        int size = this.list.size();
        int mid = size/2;
        boolean flag = true;
        for(int i = 0; i <= mid; i++){
            if(this.list.get(i) != this.list.get(size-1-i)){
                flag = false;
                return flag;
            }
        }
        return flag;
    }
    public boolean isPalindrome(int x) {
        if(x < 0){
            return false;
        }else if(x < 10){
            return true;
        }else{
            this.pushInt(x);
            return this.isListPalindrome(x);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值