Leetcode: Palindrome Number

本文提供了一种不使用额外空间的方法来判断整数是否为回文数,通过逐步反转整数的一半并与原始整数进行比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

bool isPalindrome(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(x<0)return false;
		int left = 1, right = 10, tx = x/10;
		while(tx>0)
		{ tx /= 10; left *= 10;}
		while(left>=right)
		{
			if(x/left != x%right/(right/10))return false;
			x %= left; x =x - x%right; left /= 10; right *= 10;
		}
		return true;
    }

class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0) return false;
        long long xx = x;
        long long yy = 0;
        while(true){
            yy = yy * 10 + xx % 10;
            xx /= 10;
            if(xx < 1) break;
        }
        return x == yy;
    }
};


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值