Skip to content

Commit

Permalink
Create: 7-Reverse-Integer.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
Ykhan799 authored Aug 31, 2022
1 parent c633dda commit 52fc651
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions swift/7-Reverse-Integer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
func reverse(_ x: Int) -> Int {
let MIN = Int32.min
let MAX = Int32.max

var res = 0
var value = x
while value != 0 {
let digit = value % 10
value /= 10

if res > MAX / 10 || (res == MAX / 10 && digit >= MAX % 10) {
return 0
}
if res < MIN / 10 || (res == MIN / 10 && digit <= MIN % 10) {
return 0
}
res = (res * 10) + digit
}
return res
}
}

0 comments on commit 52fc651

Please sign in to comment.