7. Reverse Integer
Problem description:
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:1
2Input: 123
Output: 321
Example 2:1
2Input: -123
Output: -321
Example 3:1
2Input: 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.
Solution:
need to check if the reversed number is overflow.
use a long long
variable to store the reversed number.
1 | class Solution { |
time complexity: $O(n)$
space complexity: $O(1)$
reference:
https://goo.gl/zKDGQk