-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.字符串转换整数-atoi.swift
64 lines (58 loc) · 1.78 KB
/
8.字符串转换整数-atoi.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* @lc app=leetcode.cn id=8 lang=swift
*
* [8] 字符串转换整数 (atoi)
*/
// @lc code=start
import Foundation
class Solution {
func myAtoi(_ s: String) -> Int {
var resultStr = ""
var isNegative = false
var resultNum = 0
var isGetNum = false
for (_, char) in s.enumerated(){
let charStr = String(char)
if charStr == " " {
if isGetNum {
break
}else{
continue
}
}
if charStr == "-" && !isGetNum{
isGetNum = true
isNegative = true
resultStr += charStr
}else if (charStr == "+" && !isGetNum){
isGetNum = true
resultStr += charStr
continue
}else if(self.isNumber(charStr)){
isGetNum = true
resultStr += charStr
resultNum = resultNum * 10 + Int(charStr)!
let tempNum = isNegative ? resultNum * -1 : resultNum
if tempNum < Int32.min{
return Int(Int32.min)
}else if tempNum > Int32.max{
return Int(Int32.max)
}
}else{
break
}
}
return isNegative ? resultNum * -1 : resultNum
}
func isNumber(_ s:String) -> Bool{
let regex = "^[0-9]{1}$"
do{
let RE = try NSRegularExpression(pattern: regex, options: .caseInsensitive)
let matchs = RE.matches(in: s, options: .reportProgress, range: NSRange(location: 0, length: s.count))
return matchs.count > 0
}catch{
return false
}
}
}
// @lc code=end