-
Notifications
You must be signed in to change notification settings - Fork 8
/
Solution.kt
31 lines (28 loc) · 896 Bytes
/
Solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
fun reverse(x: Int): Int {
var num = x
var res : Long = 0
while (num != 0) {
res = res * 10 + num % 10
num /= 10
if (res > Int.MAX_VALUE || res < Int.MIN_VALUE) return 0
}
return res.toInt()
}
}
class Solution2 {
fun reverse(x: Int): Int {
val num = Math.abs(x.toLong()).toString().reversed().toLong()
if (num > Int.MAX_VALUE) return 0
return if (x < 0) num.toInt() * -1 else num.toInt()
}
}
fun main(args: Array<String>) {
Solution2().reverse(123).let(::println)
Solution2().reverse(-123).let(::println)
Solution2().reverse(120).let(::println)
Solution2().reverse(2147483647).let(::println)
Solution2().reverse(-2147483647).let(::println)
// Solution2().reverse(2147483648).let(::println)
Solution2().reverse(-2147483648).let(::println)
}