[LeetCode] 7. Reverse Integer [Easy] [LeetCode]
Input: 123
Output: 321Input: -123
Output: -321Input: 120
Output: 21class Solution {
func reverse(_ x: Int) -> Int {
var input = x;
var negative = 1;
var output = 0;
if(input < 0) {
input = input * -1;
negative = -1;
}
while(input > 0) {
var value = input % 10;
input = input / 10;
output = output * 10 + value;
}
output = output * negative;
if(output > Int32.max || output < Int32.min) {
output = 0;
}
return output;
}
}Previous[LeetCode] 5. Longest Palindromic Substring [Medium]Next[LeetCode] 8. String to Integer (atoi)
Last updated