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.
解法1:
使用表示更大范围的类型 如long long 然后判断结果是否溢出 ans > INT_MAX || ans < INT_MIN,溢出则返回0,与题意不符,且如果让翻转long long 类型的值,该怎么处理溢出的问题呢?
C++:int 4字节,long 4字节,long long 8字节
java:int 4字节,long 8字节
方法2:
C++:
注意不能写abs(ans) * 10 > INT_MAX
会报错Line 9: Char 23: runtime error: signed integer overflow: 964632435 * 10 cannot be represented in type ‘int’ (solution.c