Skip to content

Commit

Permalink
Time: 148 ms (91.24%), Space: 33.5 MB (85.71%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
grigorevmp committed Apr 29, 2022
1 parent 8d94224 commit e7da484
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions 7-reverse-integer/7-reverse-integer.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
class Solution {
fun reverse(x: Int): Int {
try{
var x_c = Math.abs(x)

var str = if (x < 0){
"-"
} else ""

while (x_c != 0){
val ch = x_c % 10
if (((str == "") and (ch != 0)) or (str != "")){
str += ch
}
x_c /= 10
var xx = x
var sign_ = 1
if (x < 0) {
sign_ = -1
xx = sign_ * xx
}

return str.toInt()

var res = 0
while (xx > 0) {

if (res * sign_ > Integer.MAX_VALUE / 10 || res * sign_ < Integer.MIN_VALUE / 10) {
return 0
}

res = res * 10 + xx % 10
xx = xx / 10
}

return (sign_ * res)


}
catch(e: Exception){
Expand Down

0 comments on commit e7da484

Please sign in to comment.