Question
Given a 32-bit signed integer, reverse digits of an integer.
Example 1
iput : 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: [$(-2)^{31}$, $2^{31}$-1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Answer
计算法
这是一个比较投机的方案,因为使用了数字类型long后,就不会再有溢出的问题,最后再和INT_MAX比较一下就可以了。时间复杂度为O(nlogn)
1 | class Solution { |