leetcode 7. 反转整数(c++版)
2019-01-13 21:42
387 查看
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.
class Solution { public: #define INT_MAX 2147483647 #define INT_MIN (-INT_MAX-1) int reverse(int x) { int flag=x<0?-1:1; int num=0; while(x){ if((flag==-1&&(INT_MIN/10>num))||(flag==1&&INT_MAX/10<num))return 0; num=num*10+x%10; x/=10; } return num; } };
相关文章推荐
- LeetCode 7. Reverse Integer 整数反转
- [C++]LeetCode 12: Integer to Roman(将整数转换为罗马数字)
- LeetCode - 7. 反转整数
- LeetCode : 12 整数转罗马数字 C++
- [leetcode]反转整数
- [置顶] [LeetCode] Reverse Integer 整数反转
- LeetCode里的7.反转整数问题 Java15行
- LeetCode7——Reverse Integer(将一个整数反转,注意溢出的处理)
- LeetCode:第7题: 反转整数(reverse-integer)
- LeetCode 7. Reverse Integer C++--带正负号数字反转
- leetcode 7. Reverse Integer整数反转
- LeetCode 7. Reverse Integer(整数反转)
- LeetCode Reverse Integer 反转整数
- LeetCode 7. Reverse Integer(反转整数)
- LeetCode 第七题 反转整数
- (LeetCode)反转整数
- leetcode-整数反转
- leetcode——Reverse Integer 反转整数数字(AC)
- Leetcode Reverse Integer 反转整数
- 【leetcode】Reverse Integer整数反转----Java代码实现