-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path8_StringToInteger.swift
64 lines (60 loc) · 1.62 KB
/
8_StringToInteger.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
64
//
// 8_StringToInteger.swift
// LeetcodeSwift
//
// Created by yansong li on 2016-09-06.
// Copyright © 2016 YANSONG LI. All rights reserved.
//
import Foundation
/**
Title:8 String to Integer(atoi)
URL: https://leetcode.com/problems/string-to-integer-atoi/
Space: O(n)
Time: O(1)
*/
class StringToInteger_Solution {
func myAtoi(_ str: String) -> Int {
var ret = 0
var flag = 1
var index = 0
let zeroScalar = "0".unicodeScalars
let maxIntRemained = String(Int.max % 10)
let maxRemained = maxIntRemained[maxIntRemained.startIndex]
let maxInt = Int(Int32.max)
let minInt = Int(Int32.min)
let characters = Array(str.characters)
guard characters.count > 0 else {
return ret
}
while characters[index] == " " {
index += 1
}
if characters[index] == "-" || characters[index] == "+" {
flag = 1 - 2 * (characters[index] == "-" ? 1 : 0)
index += 1
}
while index <= characters.count - 1 &&
characters[index] >= "0" && characters[index] <= "9" {
if ret > maxInt / 10 ||
(ret == maxInt / 10 && characters[index] > maxRemained) {
if flag == 1 {
return maxInt
} else {
return minInt
}
}
// convert to unicode scalar value.
let currentString = String(characters[index])
let currentScalar = currentString.unicodeScalars
ret = ret * 10 +
Int(currentScalar[currentScalar.startIndex].value) -
Int(zeroScalar[zeroScalar.startIndex].value)
index += 1
}
return ret * flag
}
func test() {
let test1 = myAtoi("2147483648")
print(test1)
}
}