在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Implement 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:
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. 实现 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止。如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为整数的值。如果第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。 字符串可以在形成整数的字符后面包括多余的字符,这些字符可以被忽略,它们对于函数没有影响。 当字符串中的第一个非空字符序列不是个有效的整数;或字符串为空;或字符串仅包含空白字符时,则不进行转换。 若函数不能执行有效的转换,返回 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 && str.charAt(i) == " ") 8 { 9 i += 1 10 } 11 if str.charAt(i) == "+" || str.charAt(i) == "-" 12 { 13 sign = (str.charAt(i++) == "+") ? 1 : -1 14 } 15 //ASCII: '0'为48 '9'为57 16 while(i < n && str.toInt(i) >= 48 && str.toInt(i) <= 57) 17 { 18 if base > Int32.max / 10 || (base == Int32.max / 10 && str.toInt(i) - 48 > 7) 19 { 20 return (sign == 1) ? Int(Int32.max) : Int(Int32.min) 21 } 22 base = 10 * base + (str.toInt(i++) - 48) 23 } 24 return base * sign 25 } 26 } 27 28 extension String { 29 //获取指定索引位置的字符,返回为字符串形式 30 func charAt(_ num:Int) -> String 31 { 32 guard num < self.count else { 33 assertionFailure("Index out of range!") 34 return String() 35 } 36 let index = self.index(self.startIndex,offsetBy: num) 37 return String(self[index]) 38 } 39 40 //获取指定索引位置字符的ASCII整数值 41 func toInt(_ num:Int) -> Int 42 { 43 guard num < self.count else { 44 assertionFailure("Index out of range!") 45 return 0 46 } 47 var number:Int = Int() 48 for scalar in charAt(num).unicodeScalars 49 { 50 number = Int(scalar.value) 51 } 52 return number 53 } 54 } 55 56 /*扩展Int类,实现自增++*/ 57 extension Int{ 58 //后缀++:先执行表达式后再自增 59 static postfix func ++(num:inout Int) -> Int { 60 //输入输出参数num 61 let temp = num 62 //num加1 63 num += 1 64 //返回加1前的数值 65 return temp 66 } 67 } 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 全部评论
专题导读
热门推荐
热门话题
阅读排行榜
|
请发表评论