[Swift]LeetCode7. 反转整数 | Reverse Integer
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9697892.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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.
给定一个 32 位有符号整数,将整数中的数字进行反转。
示例 1:
输入: 123 输出: 321
示例 2:
输入: -123 输出: -321
示例 3:
输入: 120 输出: 21
注意:
假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231, 231 − 1]。根据这个假设,如果反转后的整数溢出,则返回 0。
12ms
1 class Solution { 2 func reverse(_ x: Int) -> Int { 3 //x为常量,赋值为变量 4 var num:Int = x 5 var rev:Int = 0 6 while(num != 0) 7 { 8 var pop:Int = num % 10 9 num /= 10 10 if((rev > Int32.max/10)||(rev == Int32.max/10 && pop > 7)){return 0} 11 if((rev < Int32.min/10)||(rev == Int32.min/10 && pop < -8)){return 0} 12 rev = rev * 10 + pop 13 } 14 return rev 15 } 16 }
12ms
1 class Solution { 2 func reverse(_ x: Int) -> Int { 3 4 guard x != 0 && abs(x) < INT32_MAX else{ 5 return 0 6 } 7 var result = 0 8 var num = abs(x) 9 while num > 0{ 10 let cursor = num % 10 11 if cursor == 0 && result == 0{ 12 num /= 10 13 continue 14 }else{ 15 result = result * 10 + cursor 16 num /= 10 17 } 18 } 19 if result >= INT32_MAX{ 20 return 0 21 } 22 return (x > 0) ? result : -result 23 } 24 }
16ms
1 class Solution { 2 func reverse(_ x: Int) -> Int { 3 var str = String(x) 4 var sign = "" 5 if x < 0 { 6 sign = String(str.remove(at: str.startIndex)) 7 } 8 9 let result = Int("\(sign)\(String(str.reversed()))") ?? 0 10 11 let lowerLimit = Int(pow(Double(-2), 31)) 12 let higherLimit = Int(pow(Double(2), 31)-1) 13 14 return result < lowerLimit || result > higherLimit ? 0 : result 15 } 16 }
20ms
1 class Solution { 2 func reverse(_ x: Int) -> Int { 3 let str = Array(String(x)) 4 var rev : [Character] = [] 5 for i in 0..<str.count { 6 let index = str.count-i-1 7 if (str[index] == "-") { continue } 8 rev.append(str[index]) 9 } 10 11 // if we can convert the reverse string to a 32bit Int ... else return 0 12 if let result = Int32(String(rev)) { 13 // signum == 1 or -1 if x is negative 14 return Int(result) * x.signum() 15 } else { 16 return 0 17 } 18 } 19 }
24ms
1 class Solution { 2 func reverse(_ x: Int) -> Int { 3 let tmp = x < 0 ? -1 : 1 4 let returnVal = Int(String(abs(x).description.reversed())) ?? 0 5 if returnVal > Int(Int32.max) || returnVal < Int(Int32.min) { 6 return 0 7 } 8 return returnVal * tmp 9 } 10 }
28ms
1 class Solution { 2 func reverse(_ x: Int) -> Int { 3 var temp: Int = x 4 var answer: UInt = 0 5 var isNegative: Bool = false 6 7 // Flip negatiave to positive 8 if x < 0 { 9 isNegative = true 10 if x == Int.min { 11 temp = (temp + 1) * -1 12 temp += 1 13 } else { 14 temp *= -1 15 } 16 } 17 18 // Reverse the numbers 19 while temp > 0 { 20 // Get last digit 21 answer = (answer * 10) + (UInt(temp) % 10) 22 23 // Remove last digit 24 temp = temp / 10 25 } 26 27 print("answer: \(answer)") 28 print("max: \(UInt(Int.max))") 29 30 if answer > UInt(Int32.max) { 31 return 0 32 } 33 34 if (isNegative) { 35 return Int(answer) * -1 36 } else { 37 return Int(answer) 38 } 39 } 40 }