为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode8. 字符串转整数 (atoi) | String to Integer (atoi)

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9881572.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 && 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         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 {
 2         func myAtoi(_ str: String) -> Int {
 3         
 4         let arr: [String] = ["0","1","2","3","4","5","6","7","8","9"]
 5         var result: Int = 0
 6         
 7         var numbers: [String] = []
 8         for char in str {
 9             let temp: String = String.init(char)
10             numbers.append(temp)
11         }
12         
13         var flag: Int = 1
14         var pos: Int = -1
15         var haha: Bool = false //表示是否出现了第一个有效的符号
16         
17         // 1.首先第一个非空字符字符为 数字或正、负号。
18         for i in 0..<numbers.count {
19             
20             if (numbers[i] == " ") && pos == -1 {
21                 continue
22             }else {
23                 pos = i
24             }
25             if (numbers[i] == "-" || numbers[i] == "+") && !haha{
26                 flag = (numbers[i] == "+") ? 1 : -1
27                 haha = true
28                 continue
29             }else if (arr .contains(numbers[i])) {
30                 haha = true
31                 result = result*10 + Int(numbers[i])!
32                 if result > Int32.max && flag == 1{
33                     result = Int(Int32.max)
34                     return result
35                 }else if result*flag <= Int32.min && flag == -1{
36                     result = Int(Int32.min)
37                     return result
38                 }
39             }else {
40                 break
41             }
42         }
43         return result * flag
44     }
45 }

 60ms

 1 class Solution {
 2     func myAtoi(_ str: String) -> Int {
 3         var resultInt:Int = 0
 4         var charArray = [Character]()
 5         var isBegin:Bool = true
 6         var isNegative:Int32 = 1
 7         for charAtoi in str.unicodeScalars {
 8             let charInt = Int32(charAtoi.value)
 9             if (charInt == 32 && isBegin){
10                 continue
11             } else {
12                 if (((charInt == 43 || charInt == 45) && isBegin) || (charInt >= 48 && charInt <= 57)){
13                     isBegin = false
14                     if charInt == 45 {
15                         isNegative = -1
16                     }
17                     if (charInt >= 48 && charInt <= 57) {
18                         if charArray.count > 8 {
19                             let currentInt = Int32(String(charArray))! * isNegative
20                             print(currentInt)
21                             if (currentInt > Int32.max / 10 || (currentInt == Int32.max / 10 && charInt >= 55)) {
22                                 return Int(Int32.max)
23                             }
24                             if (currentInt < Int32.min / 10 || (currentInt == Int32.min / 10 && charInt >= 56)) {
25                                 return Int(Int32.min)
26                             }
27                         }
28                         charArray.append(Character(UnicodeScalar(charAtoi)))
29                     }
30                 } else {
31                     break
32                 }
33             }
34         }
35         if charArray.count > 0 {
36             resultInt = Int(String(charArray))! * Int(isNegative)
37         }
38         return resultInt
39     }
40 }

 60ms

 1 class Solution {
 2     func myAtoi(_ str: String) -> Int {
 3         var chars = Array(str.trimmingCharacters(in: .whitespaces))
 4         guard chars.count > 0 else { return 0 }
 5         let validCharacters = ["-", "+", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].map({Character($0)})
 6         guard validCharacters.contains(chars.first!) else { return 0 }
 7         chars.filter({validCharacters.contains($0)})
 8         var sign = chars.first == "-" ? -1 : 1
 9         if chars.first == "-" || chars.first == "+" {
10             chars.removeFirst()
11         }
12         var intStr = ""
13         for c in chars {
14             if let _ = Int(String(c)) {
15                 intStr.append(c)
16             } else { 
17                 print(c)
18                 break 
19             }
20         }
21         let sol = (Int(intStr) ?? (intStr.isEmpty ? 0 : sign == -1 ? Int(Int32.max) + 1 : Int(Int32.max))) * sign
22         
23         return sign == -1 ? Int(Int32.min) < sol ? sol : Int(Int32.min) : Int(Int32.max) > sol ? sol : Int(Int32.max)
24     }
25 }

 

posted @ 2018-10-31 10:30  为敢技术  阅读(558)  评论(0编辑  收藏  举报