LeetCode9. 回文数

判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。链接如下https://leetcode-cn.com/problems/palindrome-number/

题解:按照题意不用字符串,直接反转数字,然后判断反转数与原数是否相等 注意一个边界数就好了,在这里错了一次

class Solution {
public:
    bool isPalindrome(int s) {
        if (s < 0) {//负数直接flase
            return false;
        }
        else if (s == 0) {//0直接true
            return true;
        }
        else {
            int temp = s;
            int i = 10;//10的倍数;
            long long s1 = 0;//转换后的数;2147483647int型数据的边界处理好,所以用long long
            while (temp) {
                s1 = s1 * i+(temp % 10);
                temp = temp / 10;
            }
            //cout <<"转换后的为:"<< s1 << endl;
            if (s1 == s) { return true; }
            else return false;
        }
    }
};

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值