7. Reverse Integer [LeetCode]

7. Reverse Integer

/**************************************************************************
 * 
 * 7. [Reverse Integer](https://leetcode.com/problems/reverse-integer/)
 * 
 * Given a 32-bit signed integer, reverse digits of an integer.
 * if outside the signed 32-bit integer range [-231, 231 - 1], then return 0
 * 
 * Example:
 *  123 =>  321
 * -123 => -321
 *  120 =>   21
 * -2147483648 => 0
 **************************************************************************/

int reverse(int x){
    long long num = x;
    long long res = 0;

    for ( ; num; num/=10)
        res = res*10 + num % 10;
    
    if (res > INT_MAX || res < INT_MIN)
        return 0;
    else
        return res;
}

 

 

9. Palindrome Number



/**************************************************************************
 * 
 * 9. [Palindrome Number](https://leetcode.com/problems/palindrome-number/)
 * 
 * Given an integer x, return true if x is palindrome integer. 
 * An integer is a palindrome when it reads the same backward as forward. 
 * For example, 121 is palindrome while 123 is not.
 * 
 * Example 1:
 * Input: x = 121
 * Output: true
 * 
 * Example 2:
 * Input: x = -121
 * Output: false
 * 
 * Example 3:
 * Input: x = 10
 * Output: false
 **************************************************************************/


bool isPalindrome(int x){
    if (x < 0) return false;
    long long ret = 0;
    for (int num = x; num; num/=10)
        ret = ret * 10 + num % 10;
    if (ret > INT_MAX) return false;
    return ret == x;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用\[1\]和引用\[2\]提供了两种不同的解决方案来实现整数反转。这两种解决方案都是通过取余和除以10来逐位反转整数。具体来说,我们可以使用一个变量来保存反转后的数,然后循环将原始数除以10,每次取余得到个位数,然后将个位数加到反转数的末尾。最后,根据题目要求,我们需要注意一些边界情况,比如整数溢出的问题。引用\[1\]中的解决方案使用了long long类型来保存反转后的数,并在返回结果之前进行了溢出检查。而引用\[2\]中的解决方案使用了int类型,并在返回结果之前进行了溢出检查。引用\[3\]提供了另一种解决方案,使用了数学转换的方法来实现整数反转。这个解决方案使用了long类型来保存反转后的数,并在返回结果之前进行了溢出检查。根据题目要求,如果反转后的数超过了int类型的范围,我们需要返回0。因此,根据这些引用内容,我们可以得出以下答案: 答案: 整数反转可以通过取余和除以10来逐位反转。我们可以使用一个变量来保存反转后的数,然后循环将原始数除以10,每次取余得到个位数,然后将个位数加到反转数的末尾。最后,根据题目要求,我们需要注意一些边界情况,比如整数溢出的问题。根据引用\[1\]和引用\[2\]提供的解决方案,我们可以选择使用long long类型或int类型来保存反转后的数,并在返回结果之前进行溢出检查。另外,引用\[3\]提供了另一种解决方案,使用了数学转换的方法来实现整数反转。这个解决方案使用了long类型来保存反转后的数,并在返回结果之前进行了溢出检查。根据题目要求,如果反转后的数超过了int类型的范围,我们需要返回0。因此,根据这些引用内容,我们可以选择合适的解决方案来实现整数反转。 #### 引用[.reference_title] - *1* [Reverse-Interger](https://blog.csdn.net/qq_36810403/article/details/77823115)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【LeetCode】7.Reverse Interger(简单难度)](https://blog.csdn.net/zeroheitao/article/details/118115782)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Reverse Integer](https://blog.csdn.net/whitesun123/article/details/79614218)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luuyiran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值