[Swift]LeetCode8. 字串轉整數 (atoi) | String to Integer (atoi)
Implement atoi
which converts a string to an integer.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned.
Note:
- Only the space character
' '
is considered as whitespace character. - Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
Example 1:
Input: "42" Output: 42
Example 2:
Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42.
Example 3:
Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
Example 4:
Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:
Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned.
實現 atoi
,將字串轉為整數。
該函式首先根據需要丟棄任意多的空格字元,直到找到第一個非空格字元為止。如果第一個非空字元是正號或負號,選取該符號,並將其與後面儘可能多的連續的數字組合起來,這部分字元即為整數的值。如果第一個非空字元是數字,則直接將其與之後連續的數字字元組合起來,形成整數。
字串可以在形成整數的字元後面包括多餘的字元,這些字元可以被忽略,它們對於函式沒有影響。
當字串中的第一個非空字元序列不是個有效的整數;或字串為空;或字串僅包含空白字元時,則不進行轉換。
若函式不能執行有效的轉換,返回 0。
說明:
假設我們的環境只能儲存 32 位有符號整數,其數值範圍是 [−231, 231 − 1]。如果數值超過可表示的範圍,則返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。
示例 1:
輸入: "42" 輸出: 42
示例 2:
輸入: " -42" 輸出: -42 解釋: 第一個非空白字元為 '-', 它是一個負號。 我們儘可能將負號與後面所有連續出現的數字組合起來,最後得到 -42 。
示例 3:
輸入: "4193 with words" 輸出: 4193 解釋: 轉換截止於數字 '3' ,因為它的下一個字元不為數字。
示例 4:
輸入: "words and 987" 輸出: 0 解釋: 第一個非空字元是 'w', 但它不是數字或正、負號。 因此無法執行有效的轉換。
示例 5:
輸入: "-91283472332" 輸出: -2147483648 解釋: 數字 "-91283472332" 超過 32 位有符號整數範圍。 因此返回 INT_MIN (−231) 。
44ms
1 class Solution { 2 func myAtoi(_ str: String) -> Int { 3 //除去前後空格 4 var str = str.trimmingCharacters(in: .whitespaces) 5 if str.isEmpty {return 0} 6 var sign:Int = 1,base:Int = 0,i:Int = 0,n:Int = str.count 7 while(i < n && getChar(str,i) == " ") 8 { 9 i += 1 10 } 11 if getChar(str,i) == "+" || getChar(str,i) == "-" 12 { 13 sign = (getChar(str,i++) == "+") ? 1 : -1 14 } 15 //ASCII: '0'為48 '9'為57 16 while(i < n && getInt(str,i) >= 48 && getInt(str,i) <= 57) 17 { 18 if base > Int32.max / 10 || (base == Int32.max / 10 && getInt(str,i) - 48 > 7) 19 { 20 return (sign == 1) ? Int(Int32.max) : Int(Int32.min) 21 } 22 base = 10 * base + (getInt(str,i++) - 48) 23 } 24 return base * sign 25 } 26 27 func getChar(_ str:String,_ num:Int) -> String 28 { 29 let index = str.index(str.startIndex,offsetBy:num) 30 return String(str[index]) 31 } 32 33 func getInt(_ str:String,_ num:Int) -> Int 34 { 35 return Character(getChar(str,num)).toInt() 36 } 37 } 38 39 /*擴充套件Int類,實現自增++、自減--運算子*/ 40 extension Int{ 41 //++字首:先自增再執行表達示 42 static prefix func ++(num:inout Int) -> Int { 43 //輸入輸出引數num 44 num += 1 45 //返回加1後的數值 46 return num 47 } 48 //字尾++:先執行表示式後再自增 49 static postfix func ++(num:inout Int) -> Int { 50 //輸入輸出引數num 51 let temp = num 52 //num加1 53 num += 1 54 //返回加1前的數值 55 return temp 56 } 57 //--字首:先自減再執行表達示 58 static prefix func --(num:inout Int) -> Int { 59 //輸入輸出引數num 60 num -= 1 61 //返回減1後的數值 62 return num 63 } 64 //字尾--:先執行表示式後再自減 65 static postfix func --(num:inout Int) -> Int { 66 //輸入輸出引數num 67 let temp = num 68 //num減1 69 num -= 1 70 //返回減1前的數值 71 return temp 72 } 73 } 74 75 //Character擴充套件方法 76 extension Character 77 { 78 func toInt() -> Int 79 { 80 var num:Int = Int() 81 for scalar in String(self).unicodeScalars 82 { 83 num = Int(scalar.value) 84 } 85 return num 86 } 87 }
16ms
1 class Solution { 2 func myAtoi(_ str: String) -> Int { 3 let stringLength = str.count 4 var index = 0 5 6 var result = 0 7 let base = 10 8 var sign: Int? = nil // 1 if positive, -1 if negative, nil if not determined 9 var integralPartStarted = false 10 11 while index < stringLength { 12 let character = str[str.index(str.startIndex, offsetBy: index)] 13 let isWhitespaceCharacter = self.isWhitespace(character) 14 let isNumericCharacter = self.isNumeric(character) 15 let numericValue = self.numericValue(character) 16 17 index += 1 18 19 if integralPartStarted == false { 20 if isWhitespaceCharacter { 21 continue 22 } else if let _sign = self.sign(character) { 23 sign = _sign 24 integralPartStarted = true 25 continue 26 } else if isNumericCharacter { 27 sign = 1 28 integralPartStarted = true 29 } else { 30 return 0 31 } 32 } 33 34 if integralPartStarted { 35 if let numericValue = numericValue { 36 let nextResult = result * base + numericValue 37 if nextResult > Int32.max { 38 return (sign ?? 1 > 0) ? Int(Int32.max) : Int(Int32.min) 39 } else { 40 result = nextResult 41 } 42 } else { 43 return result * (sign ?? 1) 44 } 45 } 46 } 47 48 return result * (sign ?? 1) 49 } 50 51 func isNumeric(_ character: Character) -> Bool { 52 return character >= "0" && character <= "9" 53 } 54 55 let unicodeScalar0 = UnicodeScalar("0").value 56 57 // returns the numeric value of the character, returns nil if the character is not a valid numeric character 58 func numericValue(_ character: Character) -> Int? { 59 guard isNumeric(character) else { 60 return nil 61 } 62 63 return Int(character.unicodeScalars.first!.value - unicodeScalar0) 64 } 65 66 func isWhitespace(_ character: Character) -> Bool { 67 return character == " " 68 } 69 70 // returns -1 if negative, +1 if positive, nil if none 71 func sign(_ character: Character) -> Int? { 72 if character == "+" { 73 return 1 74 } else if character == "-" { 75 return -1 76 } else { 77 return nil 78 } 79 } 80 }
20ms
1 class Solution { 2 func myAtoi(_ str: String) -> Int { 3 var result: Int64 = 0 4 var sign: Int64 = 1 5 var strArr = Array(str) 6 7 strArr = stripLeading(chars: strArr, toRemove: " ") 8 9 var n = strArr.count 10 if (n == 0) { 11 return 0 12 } 13 14 if (strArr[0] == Character("-")) { 15 sign = -1 16 strArr.remove(at: 0) 17 } 18 else if (strArr[0] == Character("+")) { 19 sign = 1 20 strArr.remove(at: 0) 21 } 22 23 strArr = stripLeading(chars: strArr, toRemove: "0") 24 25 n = strArr.count 26 if (n == 0) { 27 return 0 28 } 29 30 for i in 0..<n { 31 32 var lastDigit = convertToInt(strArr[i]) 33 if (lastDigit == -1) { 34 return Int(sign*result) 35 } 36 37 result = result*10 + Int64(lastDigit) 38 39 if !isWithinInt32Range(sign*result) { 40 return sign > 0 ? Int(Int32.max) : Int(Int32.min) 41 } 42 } 43 44 return Int(sign*result) 45 } 46 47 private func isWithinInt32Range(_ value: Int64) -> Bool { 48 return value > 0 ? value < Int32.max : value > Int32.min 49 } 50 51 private func convertToInt(_ c: Character) -> Int { 52 switch c { 53 case "0": return 0 54 case "1": return 1 55 case "2": return 2 56 case "3": return 3 57 case "4": return 4 58 case "5": return 5 59 case "6": return 6 60 case "7": return 7 61 case "8": return 8 62 case "9": return 9 63 default: return -1 64 } 65 } 66 67 private func stripWhiteSpace(_ chars: [Character]) -> [Character] { 68 var n: Int = chars.count 69 var i: Int = 0 70 for i in 0..<n { 71 if chars[i] != " " { 72 return Array(chars[i...n-1]) 73 } 74 } 75 76 return [] 77 } 78 79 private func stripLeading(chars: [Character], toRemove c: Character) -> [Character] { 80 var n: Int = chars.count 81 var i: Int = 0 82 for i in 0..<n { 83 if chars[i] != c { 84 return Array(chars[i...n-1]) 85 } 86 } 87 88 return [] 89 } 90 }
20ms
1 class Solution { 2 func myAtoi(_ str: String) -> Int { 3 if str.isEmpty { 4 return 0 5 } 6 7 let +: Int8 = 43 8 let -: Int8 = 45 9 let ascii0: Int8 = 48 10 let ascii9: Int8 = 57 11 let space: Int8 = 32 12 13 14 var sign: Int = 1 15 16 var result: Int = 0 17 18 let chars = str.utf8CString 19 20 var i: Int = 0 21 22 while chars[i] == space { 23 i += 1 24 } 25 26 if chars[i] == + || chars[i] == - { 27 sign = chars[i] == - ? -1 : 1 28 i += 1 29 } 30 31 while i < chars.count - 1, ascii0...ascii9 ~= chars[i] { 32 33 if result > Int32.max / 10 || (result == Int32.max / 10 && Int(chars[i] - ascii0) > 7) { 34 return sign == 1 ? Int(Int32.max) : Int(Int32.min) 35 } 36 37 result = result * 10 + Int(chars[i] - ascii0) 38 i += 1 39 } 40 41 return result * sign 42 } 43 }
24ms
1 class Solution { 2 func myAtoi(_ str: String) -> Int { 3 var result = 0 4 let arr = Array(str) 5 var sign = 1 6 7 var i = 0 8 while i < arr.count { 9 if arr[i] != " " { 10 break 11 } 12 i += 1 13 } 14 15 if i >= arr.count { 16 return 0 17 } 18 19 if arr[i] == "-" { 20 sign = -1 21 i += 1 22 } else if arr[i] == "+" { 23 i += 1 24 } 25 26 while i < arr.count { 27 let c = arr[i] 28 switch c { 29 case "0"..."9": 30 result = result * 10 + Int(String(c))! 31 if sign == 1 && result > Int32.max { 32 return Int(Int32.max) 33 } 34 if sign == -1 && (sign * result) < Int32.min { 35 return Int(Int32.min) 36 } 37 default: 38 return sign * result 39 } 40 i += 1 41 } 42 43 44 return sign * result 45 } 46 }
28ms
1 class Solution { 2 func myAtoi(_ str: String) -> Int { 3 let chars = str.map {String($0)} 4 var first = true 5 var minus = false 6 var result = 0 7 for char in chars { 8 if first { 9 if char == " " { 10 continue 11 } else if char == "-" { 12 minus = true 13 first = false 14 } else if char == "+" { 15 first = false 16 } else if let number = getNumber(char: char) { 17 result = number 18 first = false 19 } else { 20 return 0 21 } 22 } else { 23 if let number = getNumber(char: char) { 24 result = result * 10 + number 25 if result > Int32.max { 26 return minus ? Int(Int32.min) : Int(Int32.max) 27 } 28 } else { 29 break 30 } 31 } 32 } 33 if minus { 34 result = -result 35 } 36 37 return result 38 } 39 40 func getNumber(char: String) -> Int? { 41 guard let number = Int(char) else { 42 return nil 43 } 44 return number 45 } 46 }
28ms
1 class Solution { 2 let intTable: [Character: Int] = ["0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9] 3 func myAtoi(_ str: String) -> Int { 4 var minus = false 5 var digits: [Int] = [] 6 var start = false 7 for s in str { 8 if !start { 9 if s == " " { 10 continue 11 } else if s == "-" { 12 minus = true 13 start = true 14 } else if s == "+" { 15 start = true 16 } else if let i = intTable[s] { 17 start = true 18 digits.append(i) 19 } else { 20 return 0 21 } 22 } else { 23 if let i = intTable[s] { 24 digits.append(i) 25 } else { 26 break 27 } 28 } 29 } 30 if digits.count == 0 { 31 return 0 32 } 33 34 var headZero = true 35 digits = digits.filter({ v in 36 if v != 0 { 37 headZero = false 38 } 39 return !headZero 40 }) 41 let intMax = Int(pow(Double(2), Double(31))) - 1 42 let intMin = -Int(pow(Double(2), Double(31))) 43 var overflow = false 44 var res = digits.reversed().enumerated().reduce(0) { (r, arg1) -> Int in 45 let (i, v) = arg1 46 if pow(10, Double(i)) > pow(2, 32) { 47 overflow = true 48 return 0 49 } 50 return r + Int(pow(10, Double(i))) * v 51 } 52 if overflow { return minus ? intMin : intMax } 53 if minus { res = -res } 54 55 if res > intMax { 56 return intMax 57 } else if res < intMin { 58 return intMin 59 } 60 61 return res 62 } 63 }
32ms
1 class Solution { 2 func myAtoi(_ str:String) -> Int { 3 4 let negative = 45, posiz = 43, empty = 32 5 6 let nums = str.unicodeScalars.map { Int($0.value) } 7 8 let zero2nine = 48...57 9 10 var result = -1 11 12 var fuhao = -1 13 14 for i in 0..<nums.count { 15 16 let value = nums[i] 17 18 if zero2nine.contains(value) { 19 20 let num = value - 48 21 22 if fuhao == 0 && result > (2147483648 - num)/10 { 23 24 return -2147483648 25 } 26 27 if fuhao != 0 && result > (2147483647 - num)/10 { 28 29 return 2147483647 30 } 31 32 result = (result == -1 ? 0 : result) * 10 + value - 48 33 34 continue 35 } 36 37 if value == empty || value == negative || value == posiz { 38 39 if result == -1 && fuhao == -1 { 40 41 if value == negative { 42 43 fuhao = 0 44 } 45 46 if value == posiz { 47 48 fuhao = 1 49 } 50 51 continue 52 } 53 } 54 55 break 56 } 57 58 if result == -1 { 59 60 return 0 61 } 62 63 if fuhao == -1 || fuhao == 1 { 64 65 return min(2147483647, result) 66 } 67 else { 68 69 return max(-2147483648, -1 * result) 70 } 71 } 72 }
32ms
1 class Solution { 2 func isDigit(_ c: Character) -> Bool { 3 return c >= "0" && c <= "9" 4 } 5 6 func myAtoi(_ str: String) -> Int { 7 var positive: Bool = true 8 var started: Bool = false 9 var number: Int = 0 10 11 for index in str.characters.indices { 12 let char = str[index] 13 if started { 14 if isDigit(char) { 15 let multiplied = number.multipliedReportingOverflow(by: 10) 16 if multiplied.1 { 17 return positive ? Int(Int32.max) : Int(Int32.min) 18 } 19 20 let added = multiplied.0.addingReportingOverflow(Int(String(char))!) 21 if added.1 { 22 return positive ? Int(Int32.max) : Int(Int32.min) 23 } 24 25 number = added.0 26 continue 27 } 28 29 break 30 31 } else { 32 if char == " " { 33 continue 34 } 35 36 if char == "-" { 37 positive = false 38 started = true 39 continue 40 } 41 42 if char == "+" { 43 positive = true 44 started = true 45 continue 46 } 47 48 if isDigit(char) { 49 started = true 50 number = Int(String(char))! 51 continue 52 } 53 54 break 55 } 56 } 57 58 let result = positive ? number : number * -1 59 if result > Int32.max { 60 return Int(Int32.max) 61 } 62 63 if result < Int32.min { 64 return Int(Int32.min) 65 } 66 67 return result 68 69 } 70 }
36ms
1 class Solution { 2 func myAtoi(_ str: String) -> Int { 3 4 var strs = [Character]() 5 var count = 0 6 for scalars in str.unicodeScalars { 7 if 48 <= scalars.value && scalars.value < 58 { 8 let foo = UnicodeScalar(scalars.value) 9 strs.append(Character(foo!)) 10 } else if scalars.value == 45 || scalars.value == 43 { 11 if strs.count == 0 { 12 let foo = UnicodeScalar(scalars.value) 13 strs.append(Character(foo!)) 14 } else { 15 break; 16 } 17 } else if scalars.value == 32 { 18 if strs.count > 0 { 19 break; 20 } 21 } else { 22 if count == 0 { 23 return 0 24 } 25 break; 26 } 27 count += 1 28 } 29 let temp = String(strs) 30 let res = Double(temp) ?? 0 31 32 if res < Double(Int32.min) { 33 return Int(Int32.min) 34 } else if res > Double(Int32.max) { 35 return Int(Int32.max) 36 } else { 37 return Int(temp) ?? 0 38 } 39 } 40 }
36ms
1 class Solution { 2 func myAtoi(_ str: String) -> Int { 3 var foundNumber = false 4 var result: Int64 = 0 5 var sign: Int64 = 1 6 for char in str { 7 if char == Character(" ") { 8 if !foundNumber { continue } else { break } 9 } 10 if "-+".contains(String(char)) { 11 if foundNumber { break } 12 if char == Character("-") { 13 sign = -1 14 } 15 foundNumber = true 16 continue 17 } 18 guard let digit = Int64(String(char)) else { break } 19 foundNumber = true 20 result = result * 10 + digit 21 if result * sign > Int32.max { return Int(Int32.max) } 22 if result * sign < Int32.min { return Int(Int32.min) } 23 } 24 return Int(result * sign) 25 } 26 }
40ms
1 class Solution{ 2 func myAtoi(_ str: String) -> Int { 3 var numberStr = "" 4 var number = 0 5 for character in str{ 6 if character == " " ,numberStr.isEmpty{ 7 continue 8 } 9 else if Int(String(character)) != nil{ 10 numberStr.append(character) 11 } 12 else if (character == "+" || character == "-"),numberStr.isEmpty{ 13 numberStr.append(character) 14 } 15 else{ 16 break 17 } 18 19 } 20 if let num = Int(numberStr){ 21 if num > Int32.max{ 22 number = Int(Int32.max) 23 } 24 else if num < Int32.min{ 25 number = Int(Int32.min) 26 } 27 else{ 28 number = num 29 } 30 } 31 else if numberStr.count > 1{ 32 if numberStr.first == "-"{ 33 number = Int(Int32.min) 34 } 35 else{ 36 number = Int(Int32.max) 37 } 38 39 } 40 41 return number 42 } 43 }
40ms
1 class Solution { 2 func myAtoi(_ str: String) -> Int { 3 4 // var str = str.replacingOccurrences(of:" ", with: "") 5 6 var minus = 1 7 var isNum = false 8 var num = 0 9 var isSysmbol = false 10 var isFirstcharacter = false 11 12 for ch in str { 13 14 15 if isFirstcharacter == true && Int(String(ch)) == nil { 16 17 return num 18 } 19 20 21 if ch == "-" { 22 23 minus = -1 24 isFirstcharacter = true 25 continue 26 } 27 28 if ch == "+" { 29 30 isFirstcharacter = true 31 continue 32 33 } 34 35 if ch == " " { 36 37 continue 38 } 39 40 if let value = Int(String(ch)) { 41 42 isFirstcharacter = true 43 num = num * 10 + (value * minus) 44 45 } else { 46 47 return num 48 } 49 50 if num > Int32.max { 51 52 return Int(Int32.max) 53 } 54 55 if num < Int32.min { 56 57 return Int(Int32.min) 58 } 59 60 } 61 62 return num 63 } 64 }
44ms
1 class Solution { 2 func myAtoi(_ str: String) -> Int { 3 let chars = [Character](str) 4 5 var result = 0 6 var sign = 0 7 var isPrefix = true 8 var index = 0 9 for char in chars { 10 // 1. 先判斷字串前面的空格 11 if char == " " && isPrefix { 12 continue 13 } 14 15 isPrefix = false 16 17 // 2. 判斷正負號 18 if char == "+" || char == "-" { 19 if sign != 0 || index != 0 { 20 break 21 } 22 sign = char == "+" ? 1 : -1 23 continue 24 } 25 26 // 3. 判斷合理的值 27 if char >= "0" && char <= "9" { 28 let charValue = Int(String(char))! 29 30 // 3.1 大於最大值 31 if sign >= 0 && result > ((Int(Int32.max) - charValue) / 10) { 32 return Int(Int32.max) 33 } 34 35 // 3.2 小於最小值 36 if sign == -1 && -result < (Int(Int32.min) + charValue) / 10 { 37 return Int(Int32.min) 38 } 39 40 // 3.3 正常值 41 result = result * 10 + charValue 42 } else { 43 break 44 } 45 index += 1 46 } 47 48 sign = sign == 0 ? 1 : sign 49 50 return result * sign 51 52 53 } 54 }
44ms
1 class Solution { 2 func myAtoi(_ str: String) -> Int { 3 let int_max = 2147483647 4 let int_min = -2147483648 5 6 var asd = Array(str.trimmingCharacters(in: .whitespaces)) 7 if asd.isEmpty { 8 return 0 9 } 10 var flag = 1 11 var result = 0 12 let firstChar = String(asd[0]) 13 if firstChar == "" { 14 return 0 15 } else if firstChar == "-" { 16 flag = -1 17 } else if firstChar == "+" { 18 flag = 1 19 } else if let number = Int(firstChar) { 20 result += number 21 } else { 22 return 0 23 } 24 25 var m = 1 26 while m < asd.count { 27 if let number = Int(String(asd[m])) { 28 print(result) 29 result = result * 10 + number 30 m += 1 31 if result >= int_max { 32 if flag == 1 { 33 return int_max 34 } else if result > int_max && flag == -1 { 35 return int_min 36 } 37 } 38 } else { 39 break 40 } 41 } 42 return flag * result 43 } 44 45 }
48ms
1 class Solution { 2 func myAtoi(_ str: String) -> Int { 3 4 if str.isEmpty { 5 return 0 6 } 7 8 func isNumber(_ character: UInt32) -> Bool{ 9 //判定數字 10 if character >= 48 && character <= 57 { 11 return true 12 } 13 return false 14 } 15 16 //去掉空格 17 let whitespace = NSCharacterSet.whitespaces 18 let noWhitespaceStr = str.trimmingCharacters(in: whitespace) 19 20 var result = 0 21 var sign = 1 22 for (index,scalar) in noWhitespaceStr.unicodeScalars.enumerated() { 23 if isNumber(scalar.value) || (scalar.value == 45 && index == 0) { 24 //如果第一位是字元 - 則標識為負數 25 if scalar.value == 45 && index == 0 { 26 sign = -1 27 continue 28 } 29 30 //當前結果大於最大值除以10 31 if result > Int32.max / 10 || (result == Int32.max / 10 && Int(String(scalar))! > 7) { 32 return sign == 1 ? Int(Int32.max) : Int(Int32.min) 33 } 34 35 result = result * 10 + Int(String(scalar))! 36 }else if scalar.value == 43 && index == 0{ 37 continue 38 }else{ 39 break 40 } 41 } 42 43 return result * sign 44 } 45 }
56ms
1 class Solution {