【String to Integer (atoi) 】cpp

题目

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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

spoilers alert... click to show requirements for atoi.

Requirements for atoi:

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. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

代码

class Solution {
public:
    int myAtoi(string str) {
         const size_t len = str.length();
         // index of str
         size_t i = 0;
         // skip the white space
         while ( str[i]==‘ ‘ && i<len ) i++;
         int sign = 1;
         if ( str[i]==‘+‘ ) {
             sign = 1;
             ++i;
         }
         else if( str[i]==‘-‘ ){
             sign = -1;
             ++i;
         }
         // visit all the other char
         int num = 0;
         while ( i < len )
         {
             // not numerical char
             if ( str[i]<‘0‘ || str[i]>‘9‘ ) break;
             // overflow max bound or min bound
             if ( num > INT_MAX/10 || ( num==INT_MAX/10 && (str[i]-‘0‘)>INT_MAX%10))
             {
                 return sign==1 ? INT_MAX : INT_MIN;
             }
             // accumulate the num
             num = num * 10 + str[i] - ‘0‘;
             ++i;
         }
         // return the result
         return num * sign;
    }
};

Tips

逐步把case处理掉。这里主要有两个点需要关注:

(1)

复习了一下字符串相减,就是其ASCII码相减,这个跟学C语言的时候差不多。

(2)

判断是否超出INT_MAX的时候,一开始写成了“num*10 > INT_MAX”,这种写法是有问题的比如下面的test case就无法通过:

"      -11919730356x"

在代码中加一句每次处理num前看看num是什么的语句cout << "num:" << num << ",num*10:" << num*10 <<endl;

num:0,num*10:0
num:1,num*10:10
num:11,num*10:110
num:119,num*10:1190
num:1191,num*10:11910
num:11919,num*10:119190
num:119197,num*10:1191970
num:1191973,num*10:11919730
num:11919730,num*10:119197300
num:119197303,num*10:1191973030
num:1191973035,num*10:-965171538

在这一步就出错了,原因是

2147483647(INT_MAX)

1191973035*10 超出了int类型的限制,就溢出了。

所以 还是用num > INT_MAX/10这种比较形式安全,以后也要记住。

时间: 2024-10-14 12:33:55

【String to Integer (atoi) 】cpp的相关文章

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *

【leetcode刷题笔记】String to Integer (atoi)

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 spe

【leetcode】String to Integer (atoi)

String to Integer (atoi) 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 f

LeetCode【8】. String to Integer (atoi) --java实现

String to Integer (atoi) 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 f

【Leet Code】String to Integer (atoi) ——常考类型题

String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions 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 yours

【LeetCode-面试算法经典-Java实现】【008-String to Integer (atoi) (字符串转成整数)】

[008-String to Integer (atoi) (字符串转成整数)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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

LeetCode 008 String to Integer (atoi)

[题目] 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 b

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,