diff --git a/lcof/面试题67. 把字符串转换成整数/README.md b/lcof/面试题67. 把字符串转换成整数/README.md index 4688aa27dc..ebe212c8fb 100644 --- a/lcof/面试题67. 把字符串转换成整数/README.md +++ b/lcof/面试题67. 把字符串转换成整数/README.md @@ -282,6 +282,55 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + func strToInt(_ str: String) -> Int { + let n = str.count + if n == 0 { + return 0 + } + + var index = str.startIndex + while index != str.endIndex && str[index] == " " { + index = str.index(after: index) + } + + if index == str.endIndex { + return 0 + } + + var sign = 1 + if str[index] == "-" { + sign = -1 + index = str.index(after: index) + } else if str[index] == "+" { + index = str.index(after: index) + } + + var result = 0 + let flag = Int(Int32.max) / 10 + + while index != str.endIndex { + let char = str[index] + if char < "0" || char > "9" { + break + } + + if result > flag || (result == flag && char > "7") { + return sign == 1 ? Int(Int32.max) : Int(Int32.min) + } + + result = result * 10 + Int(char.asciiValue! - Character("0").asciiValue!) + index = str.index(after: index) + } + + return sign * result + } +} +``` + diff --git a/lcof/面试题67. 把字符串转换成整数/Solution.swift b/lcof/面试题67. 把字符串转换成整数/Solution.swift new file mode 100644 index 0000000000..493a173a30 --- /dev/null +++ b/lcof/面试题67. 把字符串转换成整数/Solution.swift @@ -0,0 +1,44 @@ +class Solution { + func strToInt(_ str: String) -> Int { + let n = str.count + if n == 0 { + return 0 + } + + var index = str.startIndex + while index != str.endIndex && str[index] == " " { + index = str.index(after: index) + } + + if index == str.endIndex { + return 0 + } + + var sign = 1 + if str[index] == "-" { + sign = -1 + index = str.index(after: index) + } else if str[index] == "+" { + index = str.index(after: index) + } + + var result = 0 + let flag = Int(Int32.max) / 10 + + while index != str.endIndex { + let char = str[index] + if char < "0" || char > "9" { + break + } + + if result > flag || (result == flag && char > "7") { + return sign == 1 ? Int(Int32.max) : Int(Int32.min) + } + + result = result * 10 + Int(char.asciiValue! - Character("0").asciiValue!) + index = str.index(after: index) + } + + return sign * result + } +} \ No newline at end of file