8.字符串转换整数 (atoi)

版权声明:本文为博主原创文章,转载请联系作者获得授权并注明出处。 https://blog.csdn.net/qq_34172340/article/details/84645101

文章已同步更新到本人个人博客跳转链接
8.字符串转换整数 (atoi)

请你来实现一个 atoi 函数,使其能将字符串转换成整数。

首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。

当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。

该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。

注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。

在任何情况下,若函数不能进行有效的转换时,请返回 0。

说明:

假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−2**31,  2**31 − 1]。如果数值超过这个范围,qing返回  INT_MAX (2**31 − 1) 或 INT_MIN (−2**31) 。

示例 1:

输入: "42"
输出: 42
示例 2:

输入: "   -42"
输出: -42
解释: 第一个非空白字符为 '-', 它是一个负号。
     我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。
示例 3:

输入: "4193 with words"
输出: 4193
解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。
示例 4:

输入: "words and 987"
输出: 0
解释: 第一个非空字符是 'w', 但它不是数字或正、负号。
     因此无法执行有效的转换。
示例 5:

输入: "-91283472332"
输出: -2147483648
解释: 数字 "-91283472332" 超过 32 位有符号整数范围。 
     因此返回 INT_MIN (−2**31) 。
class Solution:
    def myAtoi(self, str):
        """
        :type str: str
        :rtype: int
        """
        import re
        str = str.strip()
        res = re.match(r'(\+?|\-?)\d+',str)
        if res:
            num = int(res.group())
            if num >= 2**31:
                return 2**31 -1
            elif num < -2**31:
                return -2**31
            else:
                return num
        else:
            return 0

写个符合条件的正则表达式,匹配以0个或1个+亦或者以0个或1个-开头后面有1个或者多个数字的字符,直接进行匹配,然后用int转成数字,但是这个效率怎么会这么低呢,需要优化,但没优化思路,
在这里插入图片描述

对代码进行性能测试,结果入下,但是不知道如何优化了,如有大神路过,请指点一下

 320 function calls (300 primitive calls) in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       54    0.000    0.000    0.000    0.000 :0(append)
        1    0.000    0.000    0.000    0.000 :0(compile)
        1    0.000    0.000    0.000    0.000 :0(exec)
        1    0.000    0.000    0.000    0.000 :0(find)
        5    0.000    0.000    0.000    0.000 :0(get)
        1    0.000    0.000    0.000    0.000 :0(group)
       35    0.000    0.000    0.000    0.000 :0(isinstance)
        1    0.000    0.000    0.000    0.000 :0(items)
    55/50    0.000    0.000    0.000    0.000 :0(len)
        1    0.000    0.000    0.000    0.000 :0(match)
        2    0.000    0.000    0.000    0.000 :0(max)
       17    0.000    0.000    0.000    0.000 :0(min)
        2    0.000    0.000    0.000    0.000 :0(ord)
        1    0.000    0.000    0.000    0.000 :0(setprofile)
        1    0.000    0.000    0.000    0.000 :0(strip)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 lengthOfLongestSubstring.py:99(con)
        1    0.000    0.000    0.000    0.000 profile:0(con())
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    0.000    0.000 re.py:160(match)
        1    0.000    0.000    0.000    0.000 re.py:278(_compile)
        1    0.000    0.000    0.000    0.000 sre_compile.py:221(_compile_charset)
        1    0.000    0.000    0.000    0.000 sre_compile.py:248(_optimize_charset)
        3    0.000    0.000    0.000    0.000 sre_compile.py:386(_simple)
        1    0.000    0.000    0.000    0.000 sre_compile.py:412(_compile_info)
        2    0.000    0.000    0.000    0.000 sre_compile.py:513(isstring)
        1    0.000    0.000    0.000    0.000 sre_compile.py:516(_code)
        1    0.000    0.000    0.000    0.000 sre_compile.py:531(compile)
      7/1    0.000    0.000    0.000    0.000 sre_compile.py:64(_compile)
        7    0.000    0.000    0.000    0.000 sre_parse.py:105(__init__)
       15    0.000    0.000    0.000    0.000 sre_parse.py:153(__len__)
       30    0.000    0.000    0.000    0.000 sre_parse.py:157(__getitem__)
        3    0.000    0.000    0.000    0.000 sre_parse.py:161(__setitem__)
        5    0.000    0.000    0.000    0.000 sre_parse.py:165(append)
     11/5    0.000    0.000    0.000    0.000 sre_parse.py:167(getwidth)
        1    0.000    0.000    0.000    0.000 sre_parse.py:217(__init__)
       10    0.000    0.000    0.000    0.000 sre_parse.py:226(__next)
        8    0.000    0.000    0.000    0.000 sre_parse.py:242(match)
        7    0.000    0.000    0.000    0.000 sre_parse.py:247(get)
        6    0.000    0.000    0.000    0.000 sre_parse.py:276(tell)
        3    0.000    0.000    0.000    0.000 sre_parse.py:362(_escape)
      2/1    0.000    0.000    0.000    0.000 sre_parse.py:429(_parse_sub)
      3/1    0.000    0.000    0.000    0.000 sre_parse.py:491(_parse)
        1    0.000    0.000    0.000    0.000 sre_parse.py:70(__init__)
        4    0.000    0.000    0.000    0.000 sre_parse.py:75(groups)
        1    0.000    0.000    0.000    0.000 sre_parse.py:78(opengroup)
        1    0.000    0.000    0.000    0.000 sre_parse.py:797(fix_flags)
        1    0.000    0.000    0.000    0.000 sre_parse.py:819(parse)
        1    0.000    0.000    0.000    0.000 sre_parse.py:90(closegroup)

=========================== 更新===================================
对正则表达式优化了一下,r’(+?|-?)\d+‘改为r’[-+]?\d+’,函数调用次数减少了1/3,还能不能继续优化呢?

 199 function calls (193 primitive calls) in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       38    0.000    0.000    0.000    0.000 :0(append)
        1    0.000    0.000    0.000    0.000 :0(compile)
        1    0.000    0.000    0.000    0.000 :0(exec)
        6    0.000    0.000    0.000    0.000 :0(find)
        3    0.000    0.000    0.000    0.000 :0(get)
        1    0.000    0.000    0.000    0.000 :0(group)
       17    0.000    0.000    0.000    0.000 :0(isinstance)
        1    0.000    0.000    0.000    0.000 :0(items)
    35/33    0.000    0.000    0.000    0.000 :0(len)
        1    0.000    0.000    0.000    0.000 :0(match)
        7    0.000    0.000    0.000    0.000 :0(min)
        2    0.000    0.000    0.000    0.000 :0(ord)
        1    0.000    0.000    0.000    0.000 :0(setprofile)
        1    0.000    0.000    0.000    0.000 :0(strip)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 lengthOfLongestSubstring.py:117(myAtoi)
        1    0.000    0.000    0.000    0.000 profile:0(myAtoi())
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    0.000    0.000 re.py:160(match)
        1    0.000    0.000    0.000    0.000 re.py:278(_compile)
        2    0.000    0.000    0.000    0.000 sre_compile.py:221(_compile_charset)
        2    0.000    0.000    0.000    0.000 sre_compile.py:248(_optimize_charset)
        2    0.000    0.000    0.000    0.000 sre_compile.py:386(_simple)
        1    0.000    0.000    0.000    0.000 sre_compile.py:412(_compile_info)
        2    0.000    0.000    0.000    0.000 sre_compile.py:513(isstring)
        1    0.000    0.000    0.000    0.000 sre_compile.py:516(_code)
        1    0.000    0.000    0.000    0.000 sre_compile.py:531(compile)
      3/1    0.000    0.000    0.000    0.000 sre_compile.py:64(_compile)
        3    0.000    0.000    0.000    0.000 sre_parse.py:105(__init__)
        6    0.000    0.000    0.000    0.000 sre_parse.py:153(__len__)
       12    0.000    0.000    0.000    0.000 sre_parse.py:157(__getitem__)
        2    0.000    0.000    0.000    0.000 sre_parse.py:161(__setitem__)
        2    0.000    0.000    0.000    0.000 sre_parse.py:165(append)
      5/3    0.000    0.000    0.000    0.000 sre_parse.py:167(getwidth)
        1    0.000    0.000    0.000    0.000 sre_parse.py:217(__init__)
        8    0.000    0.000    0.000    0.000 sre_parse.py:226(__next)
        6    0.000    0.000    0.000    0.000 sre_parse.py:242(match)
        7    0.000    0.000    0.000    0.000 sre_parse.py:247(get)
        4    0.000    0.000    0.000    0.000 sre_parse.py:276(tell)
        1    0.000    0.000    0.000    0.000 sre_parse.py:312(_class_escape)
        1    0.000    0.000    0.000    0.000 sre_parse.py:362(_escape)
        1    0.000    0.000    0.000    0.000 sre_parse.py:429(_parse_sub)
        1    0.000    0.000    0.000    0.000 sre_parse.py:491(_parse)
        1    0.000    0.000    0.000    0.000 sre_parse.py:70(__init__)
        2    0.000    0.000    0.000    0.000 sre_parse.py:75(groups)
        1    0.000    0.000    0.000    0.000 sre_parse.py:797(fix_flags)
        1    0.000    0.000    0.000    0.000 sre_parse.py:819(parse)

猜你喜欢

转载自blog.csdn.net/qq_34172340/article/details/84645101