forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Atoi.swift
60 lines (51 loc) · 1.48 KB
/
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
/**
* Question Link: https://leetcode.com/problems/string-to-integer-atoi/
* Primary idea: Trim, positive and negative, integer overflow, is character digit
*
* Time Complexity: O(n), Space Complexity: O(1)
*/
class Atoi {
func myAtoi(str: String) -> Int {
var res = 0
var flag = 1
var index = 0
let int_max = 2147483647
let int_min = -2147483648
// trim
let content = [Character](str.characters)
while index < content.count {
guard content[index] == " " else {
break
}
index += 1
}
guard index < content.count else {
return res
}
// handle flag
if content[index] == "-" {
flag = -1
index += 1
} else if content[index] == "+" {
index += 1
}
while index < content.count {
guard _isDigit(content[index]) else {
break
}
res = res * 10 + Int(String(content[index]))!
if res >= int_max {
if flag == 1 {
return int_max
} else if res > int_max && flag == -1 {
return int_min
}
}
index += 1
}
return flag * res
}
private func _isDigit(char: Character) -> Bool {
return char >= "0" && char <= "9"
}
}