zoukankan      html  css  js  c++  java
  • LeetCode #8 String to Integer (atoi)

    LeetCode #8 String to Integer (atoi)

    Question

    Implement atoi to convert a string to an integer.

    Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

    Note: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

    Update (2015-02-10):
    The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

    Requirements for atoi:

    The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

    The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

    If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

    If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

    Solution

    Approach #1

    class Solution {
        func myAtoi(_ str: String) -> Int {
            let arr = Array(str.utf16)
            var i = 0
            var positive = true
            var result = 0
            let whiteSpace = " ".utf16.first!
            while i < arr.count, arr[i] == whiteSpace {
                i += 1
            }
            let plus = "+".utf16.first!
            let minus = "-".utf16.first!
            if i < arr.count, arr[i] == plus || arr[i] == minus {
                if arr[i] == minus {
                    positive = false
                }
                i += 1
            }
            let zero = "0".utf16.first!
            let max = Int(Int32.max)
            while i < arr.count, arr[i] >= zero, arr[i] < zero + 10 {
                if max / 10 < result || (max / 10 == result && Int(arr[i] - zero) > max % 10) {
                    return positive ? max : Int(Int32.min)
                }
                result = result * 10 + Int(arr[i] - zero)
                i += 1
            }
            return positive ? result : -result
        }
    }
    

    Time complexity: O(n).

    Space complexity: O(1).

    转载请注明出处:http://www.cnblogs.com/silence-cnblogs/p/6882779.html

  • 相关阅读:
    python 修改excel
    python 正则表达式规则收集
    anusplina 4.36版本使用提示 说明
    python 汉字编码问题
    python IOError: [Errno 22] invalid mode ('r') or filename:
    将矩阵数据转换为栅格图 filled.contour()
    将数值矩阵映射为栅格图
    一脚踩进java之基础篇23——常用API(Object、String)
    一脚踩进java之基础篇22——面向对象 (不同修饰符使用细节)
    一脚踩进java之基础篇21——面向对象 (访问修饰符、代码块)
  • 原文地址:https://www.cnblogs.com/silence-cnblogs/p/6882779.html
Copyright © 2011-2022 走看看