[Math]008 String to Integer (atoi)

  • 分类:Math

  • 考察知识点:Math/ Case by Case

  • 最优解时间复杂度:**O(n) **

8. String to Integer (atoi)

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.

代码:

我的方法:

class Solution:
    def myAtoi(self,str):
        
        #判断边界
        str_=str.strip()
        if str_=="":
            return 0
        
        #判断符号
        sign=1
        if str_[0]=="+":
            str_=str_[1:]
        elif str_[0]=="-":
            str_=str_[1:]
            sign=-1
            
        #判断数字与字母
        res=0
        for s in str_:
            if s.isdigit()==False:
                break
            res=res*10+int(s)
            
        if res*sign<-2**31:
            return -2**31
        elif res*sign>2**31-1:
            return 2**31-1
        else:
            return res*sign

讨论:

1.case by case

Perfect哦
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 切换到github上的非master分子 git clone http://github.com/project....
    乐呵老头阅读 2,622评论 0 0
  • 寺曰:定慧 —— 寺名定慧知何代, 桥古碑横不记年。 古树乱鸦啼晚照, 故园新蝶舞春烟。 七层宝塔...
    暮喃阅读 2,629评论 0 0
  • 我们有时候在面对困难和快乐的时候,反应截然不同。我在自己经历中也深切体会到,我们更愿意接受那些让自己开心的...
    蓝色的海sunshine阅读 1,616评论 0 2
  • 我们知道要想成功的实现订单,很重要的一点是感觉要好,因为宇宙回应的是我们的感觉,当你感觉怀疑,匮乏的时候,你是在向...
    筱晨沐雨阅读 3,449评论 0 3

友情链接更多精彩内容