zoukankan      html  css  js  c++  java
  • LeetCode Problem8

    题目描述:

    Implement atoi which converts a string to an integer.

    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.

    Note:

    • Only the space character ' ' is considered as whitespace character.
    • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

    Example 1:

    Input: "42"
    Output: 42
    

    Example 2:

    Input: "   -42"
    Output: -42
    Explanation: The first non-whitespace character is '-', which is the minus sign.
                 Then take as many numerical digits as possible, which gets 42.
    

    Example 3:

    Input: "4193 with words"
    Output: 4193
    Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
    

    Example 4:

    Input: "words and 987"
    Output: 0
    Explanation: The first non-whitespace character is 'w', which is not a numerical 
                 digit or a +/- sign. Therefore no valid conversion could be performed.

    Example 5:

    Input: "-91283472332"
    Output: -2147483648
    Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
                 Thefore INT_MIN (−231) is returned.

    代码:
     1 class Solution:
     2     def myAtoi(self, str: str) -> int:
     3         str = str.strip()
     4         strNum = 0
     5         if len(str) == 0:
     6             return strNum
     7 
     8         positive = True
     9         if str[0] == '+' or str[0] == '-':
    10             if str[0] == '-':
    11                 positive = False
    12             str = str[1:]
    13 
    14         count = 0
    15         for char in str:
    16             if char >= '0' and char <= '9':
    17                 # strNum = strNum * 10 + ord(char) - ord('0')
    18                 count += 1
    19             if char < '0' or char > '9':
    20                 break
    21         
    22         # 区间是右开的,所以 :count只能取到count-1
    23         # 如果字符串不能正常进行转换,要返回0,所以要首先对count的值进行判断,以确定转换是否有效,
    24         # 否则无法排除掉转换无效的情况
    25         if count != 0:
    26             strNum = int(str[:count])
    27 
    28         if strNum > 2147483647:
    29             if positive == False:
    30                 return -2147483648
    31             else:
    32                 return 2147483647
    33         if not positive:
    34             strNum = 0 - strNum
    35         return strNum
    36 
    37 if __name__ == '__main__':
    38     sol = Solution()
    39     y = sol.myAtoi(" with words 4193")
    40     print(y)

    参考资料:

    [1] Python中strip()函数的用法:https://www.cnblogs.com/yyxayz/p/4034299.html

    [2] python中chr()函数和ord()函数的用法:https://www.cnblogs.com/chaojiyingxiong/p/9169458.html

  • 相关阅读:
    2008俱乐部高校行之中南民族大学
    [更新]MSDN中Webcast "WPF中的图形系统系列" 课程预告及反馈
    7月20日 武汉.NET俱乐部在线沙龙!
    2007武汉.NET俱乐部沙龙VS2008、WPF、Silverlight
    MSDN新年第一次WebCast总结
    [评]Microsoft Visual Web Developer 2008 Step by Step, Express Edition
    [老爸创作的歌词]我从瓦砾中站起
    [Expert MS IL Assembler]武汉.NET俱乐部在线沙龙与线下聚会
    2008开年大礼:《Application = Code + Markup》中文版面世
    2009武汉.NET俱乐部活动之黄冈站
  • 原文地址:https://www.cnblogs.com/tbgatgb/p/10860773.html
Copyright © 2011-2022 走看看