zoukankan      html  css  js  c++  java
  • [leetcode]String to Integer (atoi) @ Python

    原题地址:https://oj.leetcode.com/problems/string-to-integer-atoi/

    题意:

    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.

    Notes: 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.

    spoilers alert... click to show requirements for atoi.

    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.

    解题思路:atoi的实现,有很多细节需要注意。两个判断条件很关键。INT_MAX/10 >= sum和INT_MAX - digit >= sum这两个判断条件确保了不会溢出。

    代码:

    class Solution:
        # @return an integer
        def atoi(self, str):
            INT_MAX = 2147483647; INT_MIN = -2147483648
            sum = 0
            sign = 1
            i = 0
            if str == '':
                return 0
            while i < len(str) and str[i].isspace():
                i += 1
            if i < len(str) and str[i] == '-':
                sign = -1
            if i < len(str) and (str[i] == '-' or str[i] == '+'):
                i += 1
            while i < len(str) and str[i].isdigit():
                digit = int(str[i])
                if INT_MAX/10 >= sum:
                    sum *= 10
                else:
                    if sign == 1:
                        return INT_MAX
                    else:
                        return INT_MIN
                if INT_MAX - digit >= sum:
                    sum += digit
                else:
                    if sign == 1:
                        return INT_MAX
                    else:
                        return INT_MIN
                i += 1
            return sign*sum
  • 相关阅读:
    面向对象高级
    Intellij IDEA 激活码 | Intellij IDEA 注册码
    如何保证核心链路稳定性的流控和熔断机制?
    消息模型:主题和队列有什么区别?
    MySQL中悲观锁和乐观锁到底是什么?
    SQL是如何在数据库中执行的?
    ZooKeeper 面试题(30道ATM精选问题)
    线上服务的FGC问题排查,看这篇就够了!
    一次线上JVM调优实践,FullGC40次/天到10天一次的优化过程
    由多线程内存溢出产生的实战分析
  • 原文地址:https://www.cnblogs.com/zuoyuan/p/3777806.html
Copyright © 2011-2022 走看看