From 7e5e7e10823cd61d3787979a9d062266fa3c4859 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Thu, 30 May 2024 08:06:28 +0100 Subject: [PATCH] Swift implementation for LCOF 67 --- .../README.md" | 49 +++++++++++++++++++ .../Solution.swift" | 44 +++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/Solution.swift" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" index 4688aa27dc263..ebe212c8fb7b0 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/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/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/Solution.swift" new file mode 100644 index 0000000000000..493a173a309ef --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/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