LeetCode-007 Reverse Integer
2017-12-06 15:26
501 查看
Description
Given a 32-bit signed integer, reverse digits of an integer.Example
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.
Analyse
一道水题,将一个有符号整数反转,但需要注意的是,反转之后可能越界,这里投机取巧的方式就是让答案设为64位,若越界则返回0,否则强制类型转换回整型即可。Code
class Solution { public: int reverse(int x) { long ans=0; while (x!=0) { ans=ans*10+x%10; x=x/10; if (ans>INT_MAX || ans<INT_MIN) return 0; } return (int) ans; } };
相关文章推荐
- LeetCode 007 Reverse Integer
- [LeetCode]-007-Reverse Integer
- leetcode [007] : Reverse Integer
- leetcode007 Reverse Integer
- LCP007 LeetCode 7 Reverse Integer
- 【JAVA、C++】LeetCode 007 Reverse Integer
- LeetCode 007 Reverse Integer
- leetcode007 Reverse Integer
- LeetCode 007 Reverse Integer
- [LeetCode]题解(python):007-Reverse Integer
- leetcode 007 Reverse Integer
- LeetCode007 Reverse Integer
- Leetcode007 Reverse Integer
- leetcode 007 Reverse Integer
- LeetCode 007 Reverse Integer
- [LeetCode 007] Reverse Integer
- [LeetCode]007-Reverse Integer
- leetcode 007 —— Reverse Integer
- LeetCode 007 Reverse Integer
- LeetCode 007 Reverse Integer