首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关关的刷题日记80 – Leetcode 7. Reverse Integer

关关的刷题日记80 – Leetcode 7. Reverse Integer

作者头像
WZEARW
发布2018-04-12 14:43:51
6060
发布2018-04-12 14:43:51
举报
文章被收录于专栏:专知专知

关关的刷题日记80 – Leetcode 7. Reverse Integer

题目

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123 Output: 321 Example 2:

Input: -123 Output: -321 Example 3:

Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

题目要求转置一个整数,如果转置后的整数越界了,就返回0。

思路

思路:在关关的刷题日记79 – Leetcode 9. Palindrome Number已经写了如何转置一个整数了,只是还有细节问题注意处理:如果转置后的整数越界了,就返回0。

class Solution {
public:
    int reverse(int x) {
        long a=0;   
        while(x!=0)
        {
            a=a*10+x%10;
            x/=10;
        }
        if(a>INT_MAX || a<INT_MIN)
            return 0;
        return a;
    }
};

以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang)。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-12-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 专知 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 思路
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档