前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode String to Integer (atoi)

Leetcode String to Integer (atoi)

作者头像
发布2018-09-04 11:36:43
4410
发布2018-09-04 11:36:43
举报
文章被收录于专栏:WD学习记录

题目: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:

代码语言:javascript
复制
Input: "42"
Output: 42

Example 2:

代码语言:javascript
复制
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:

代码语言:javascript
复制
Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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.

解答:

参考String to Integer (atoi)

代码语言:javascript
复制
class Solution:
    def myAtoi(self, str):
        """
        :type str: str
        :rtype: int
        """
        
        str = str.strip()
        if len(str) == 0:
            return 0
        index = 0
        sign = False
        if str[0] == '+' or str[0] == '-':
            index += 1
            sign = True
        for i in range(index, len(str)):
            if str[i] < '0' or str[i] > '9':
                break
            else:
                index += 1
        if index == 0:
            return 0
        if index == 1 and sign:
            return 0
        if int(str[:index]) > 2**31 - 1:
            return 2**31 - 1
        if int(str[:index]) < - 2**31:
            return -2 ** 31
        return int(str[:index])
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年08月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目:String to Integer (atoi)
  • 解答:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档