## LeetCode Reverse Integer

007 -- Reverse Integer （整数反转）

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

1、大于最大

2、小于最小

 func reverse(_ x: Int) -> Int {
var originInt = x
guard originInt != 0 else {
print("originInt = 0")
return 0
}
var result = 0
var tempInt = 0
while (originInt != 0) {
tempInt = originInt%10
originInt = originInt/10
if (result > Int32.max/10 || (result == Int32.max / 10 && tempInt > 7)) {
print("最大溢出")
return 0
}
if (result < Int32.min/10 || (result == Int32.min / 10 && tempInt < -8) ){
print("最小溢出")
return 0
}
result = result*10 + tempInt
}
return result
}


1032 / 1032 test cases passed.
Status: Accepted
Runtime: 4 ms
Memory Usage: 20.4 MB

有缘看到的亲们：文中若有不对之处，还请劳驾之处，谢谢！

posted on 2019-06-09 11:17 海菲菲菲 阅读(...) 评论(...) 编辑 收藏