leetcode 9. Palindrome Number(C语言,判断是否为回环数)18

贴原题:

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

click to show spoilers.

Some hints: Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the
restriction of using extra space.

You could also try reversing an integer. However, if you have solved
the problem “Reverse Integer”, you know that the reversed integer
might overflow. How would you handle such case?

There is a more generic way of solving this problem.

解析:
  本题就是判断一个数是否为回环,题目中已经给出了提示,只要把一个数翻转之后判断是否与原数相等即可。若相等即是回环数,否则就不是。

贴代码:

bool isPalindrome(int x) {
    if(x>=0)
    {
        int y=0;//翻转x
        int n=x;//保存原本的x值
        while(x)
        {
            y=y*10+x%10;//倒序;把x的最低位依次压入y的最低位
            x/=10;
        }
        if(n==y)//若一个正整数翻转后和原数相等则回环
        {
            return true;
        }
    }
    return false;
}

ps:好几天没更新了,心里很难受。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值