Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func myAtoi(_ str: String) -> Int {
- let arr = Array(str.trimmingCharacters(in: .whitespaces))
- var isNeg = false
- var res = 0
- for (index, char) in arr.enumerated(){
- if index == 0 && char == "-"{
- isNeg = true
- }else if index == 0 && char == "+" {
- }else if char <= "9" && char >= "0"{
- res = res * 10 + Int(String(char))!
- if !isNeg && res > Int32.max {
- return Int(Int32.max)
- } else if isNeg && 0 - res < Int32.min {
- return Int(Int32.min)
- }
- }else{
- break
- }
- }
- return isNeg ? 0 - res : res
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement