https://leetcode.com/problems/reverse-integer/description/
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123Output: 321
Example 2:
Input: -123Output: -321
Example 3:
Input: 120Output: 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.- 简单题,类似字符串处理。主要是考虑integer overflow情况。比较tricky的方法是用long来存储result结果,然后判断是否超过INT_MAX/INT_MIN。
- 我用的还是判断overflow。由于输入不可能是INT_MAX/INT_MIN的reverse,所以不需要考虑abs(INT_MIN) > abs(INT_MAX)的情况。
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include10 #include 11 #include 12 using namespace std;13 14 class Solution {15 public:16 int reverse(int x) {17 int result = 0;18 int sign = 1;19 20 if (x < 0) {21 sign = -1;22 x = abs(x);23 }24 25 while (x != 0) {26 if (result > (INT_MAX - x % 10) / 10) // it's ok coz input would not be reverse of INT_MIN27 return 0;28 29 result = result * 10 + x % 10;30 x /= 10;31 }32 33 return result * sign;34 }35 };36 37 int main(int argc, char* argv[])38 {39 Solution testSolution;40 string result;41 42 vector iVec = { 123, -123, 120, 2147483647, -2147483648, 0};43 44 /*45 32146 -32147 2148 049 050 051 */52 for (auto i : iVec) {53 cout << testSolution.reverse(i) << endl;54 }55 56 return 0;57 }
- 一种解法同上,字符串处理。注意Python3中整除用//,而不是/。
- 第二种解法利用Python sliding s[::-1]做字符串反转。
- Python3 字符串 | 菜鸟教程
- http://www.runoob.com/python3/python3-string.html
- python - Maximum and Minimum values for ints - Stack Overflow
- https://stackoverflow.com/questions/7604966/maximum-and-minimum-values-for-ints
- Python 3
- In Python 3, this question doesn't apply. The plain
int
type is unbounded. - However, you might actually be looking for the machine's . That's still available in Python 3 as
sys.maxsize
.
- In Python 3, this question doesn't apply. The plain
- Python 2
- In Python 2, the maximum value for plain
int
values is available assys.maxint
: >>> sys.maxint 9223372036854775807
- In Python 2, the maximum value for plain
- You can calculate the minimum value with
-sys.maxint - 1
as shown . - Python seamlessly switches from plain to long integers once you exceed this value. So most of the time, you won't need to know it.
1 class Solution: 2 def reverse(self, x: int) -> int: 3 result = 0 4 sign = 1 5 6 if x < 0: 7 sign = -1 8 x = abs(x) 9 10 while x != 0:11 if result > ( 2**31 - ( x % 10 ) ) // 10: # be careful of floor division (integer division)12 return 013 14 result = result * 10 + x % 1015 x //= 10 # be careful of floor division (integer division)16 17 return result * sign18 19 def reverse2(self, x: int) -> int: 20 # [begin:end:step] - leaving begin and end off and specifying a step of -1, it reverses a string.21 s = str( abs( x ) )22 x_reverse = int( s[ ::-1 ] ) if x > 0 else (- int( s[ ::-1 ] ) )23 24 if ( ( x_reverse >= - 2**31 ) and ( x_reverse <= 2**31 - 1 ) ):25 return x_reverse26 else:27 return 0