【EASY】Reverse Integer

发布于:2019-03-22 17:48,阅读数:430,点赞数:0


# 问题 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 store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. # 分析过程 - 输入:一个整数 - 输出:反转这个整数 - 思路:题目本身没有难度,主要需要考虑 *10 时的溢出问题。 # 解决方法 ```cpp class Solution { public: int reverse(int x) { int result = 0; while (x != 0) { if (result > 214748364 || result < -214748364) { return 0; } result *= 10; result += x % 10; x /= 10; } return result; } }; ```


评论:0条


返回列表

返回主页