Leetcode#8. String to Integer (atoi)(字符串转数字)

来源:互联网 发布:mac 打开共享文件夹 编辑:程序博客网 时间:2024/05/19 16:34

声明:题目解法使用c++和Python两种,重点侧重在于解题思路和如何将c++代码转换为python代码。

题目

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.

题意

实现atoi将字符串转换为整数。

提示:仔细考虑所有可能的输入案例。如果你想要一个挑战,请不要在下面看到,问自己什么是可能的输入案例。

注意: 这个问题的目的是模糊地指定(即没有给定的输入规范)。您有责任收集所有的输入要求。

分析

题目说你需要自己想象可能的输入数据,下面是我分析可能的输入数据以及提交输出得到的测试数组范围:

  • 前面可能有空格不影响结果。
input:"   012"output:12
  • 第一个数字前面有下面三种情况:
    • ”+”/”-“: 只有其中一个且个数为1为合法。
    • “空格/无”:合法。
    • “小写字符”:不合法之前返回0。
  • 数字必须连着的,中间出现不合法字符,输出前面的数字。
input:"   012b34"output:12
  • 字符组成的数字超过int最大值,输出最大值,超出最小值输出最小值。

思路

我的思路是先找出第一个出现“+”/“-”/数字的下标记为start(如果在出现+/-/数字之前有字符则直接返回0),此时从start开始遍历数字有三种情况:

  • i ==’+’(i必须为start)
  • i == ‘-’ (i必须为start)
  • i== 数字(记录数字出现的位数有多少,int的位数为11位,尽管我们将结果定义为long long int型,但是范围是有限的,数据可能超出long long int的范围)
  • 其它

C++代码

class Solution {public:    int myAtoi(string str) {       int start;    for(int i=0;i<str.size();i++)    {           if(str[i]>='a'&&str[i]<='z')            return 0;        if(str[i]=='+'||str[i]=='-'||str[i]>='0'&&str[i]<='9')        {            start = i;            break;        }    }    long long int res=0,flag = 1;    int num = 0;    for(int i=start;i<str.size();i++)    {        if(i==start&&str[start]=='-')            flag =-1;        else if(i==start&&str[start]=='+')            flag =1;        else if(str[i]>='0'&&str[i]<='9')        {            num++;            res= res*10 + str[i]-'0';         }        else            break;    }    if((res>=2147483648||num>=11)&&flag==1)        return (2147483647);    if((res>2147483648||num>=11)&&flag==-1)        return (-2147483648);    return res*flag;    }};

Python代码

class Solution(object):    def myAtoi(self, str):        """        :type str: str        :rtype: int        python字符转数字:int('2')=2        这里没有用到num,是因为python不用定义类型        """        start = 0        n = len(str)        for i in range(0,n):            if str[i]>='a' and str[i]<='z':                return 0            if str[i]=='+' or str[i]=='-' or (str[i]>='0' and str[i]<='9'):                start = i                break        res = 0        flag = 1        for i in range(start,n):            if i==start and str[start]=='-':                flag = -1            elif i==start and str[start]=='+':                flag = 1            elif str[i]>='0' and str[i]<='9':                res = res * 10 + int(str[i])             else:                break        if res>=2147483648 and flag ==1:            return (2147483647)        if res>2147483648 and flag==-1:            return (-2147483648)        return res*flag

GitHub本题题解:https://github.com/xuna123/LeetCode/blob/master/Leetcode%238.%20String%20to%20Integer%20(atoi).md

原创粉丝点击