[LeetCode]String to Integer (atoi)

来源:互联网 发布:华道数据有限公司 编辑:程序博客网 时间:2024/06/02 06:45

</pre>Implement <span style="font-family:monospace">atoi</span> to convert a string to an integer.<p></p><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><span style="font-weight:700">Hint:</span> 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.</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><span style="font-weight:700">Notes:</span> 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.</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><span style="font-weight:700"><span style="color:red">Update (2015-02-10):</span></span><br style="" />The signature of the <code style="font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12.6px; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">C++</code> function had been updated. If you still see your function signature accepts a <code style="font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12.6px; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">const char *</code> argument, please click the reload button <span class="glyphicon glyphicon-refresh" style=""></span> to reset your code definition.</p><p class="showspoilers" style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><a target=_blank target="_blank" href="https://leetcode.com/problems/string-to-integer-atoi/#" style="color:rgb(0,136,204); text-decoration:none; background:0px 0px">spoilers alert... click to show requirements for atoi.</a></p><div class="spoilers" style="color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><span style="font-weight:700">Requirements for atoi:</span><p style="margin-top:0px; margin-bottom:10px">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.</p><p style="margin-top:0px; margin-bottom:10px">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.</p><p style="margin-top:0px; margin-bottom:10px">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.</p><p style="margin-top:0px; margin-bottom:10px">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.</p><p style="margin-top:0px; margin-bottom:10px">测试用例:</p></div><span style="color:rgb(51,51,51); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2px">1.数字前面有空格 如s=“    123456”</span><br style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2px" /><span style="color:rgb(51,51,51); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2px">2.数字前出现了不必要或多于的字符导致数字认证错误,输出0   如s=“   b1234”  ,s=“  ++1233” , s=“ +-1121”</span><br style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2px" /><span style="color:rgb(51,51,51); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2px">3.数字中出现了不必要的字符,返回字符前的数字 如s=“   12a12” , s=“ 123  123”</span><br style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2px" /><p><span style="color:rgb(51,51,51); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2px">4.数字越界 超过了范围(-2147483648--2147483647) 若超过了负数的 输出-2147483648  超过了正数的输出2147483647</span></p><p><span style="color:rgb(51,51,51); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2px"></span></p><pre name="code" class="cpp">int myAtoi(char* str) {    int sign = 1;           //设置符号位,初始设置1为正数 如果前面没有符号就代表正数long long sum = 0;             //sum范围要大于int    if(str == NULL)        return 0;while(*str == ' ') str++;         //去掉前面空格if(*str == '-'){                   sign = -1;str++;}                                     //取符号else if(*str == '+'){sign = 1;str++;}    while(*str != '\0'){if(*str < '0' || *str > '9')              //不符合规则的取消,如“  +-123”break;        else if(*str >= '0' && *str <= '9'){            sum = sum * 10 + *str -48;if(sum > 2147483648)                       //sum范围超出break;}str++;    }     if(sign == 1 && sum > 2147483647)        return 2147483647;    if(sign == -1 && sum > 2147483648)        return -2147483648;    sum = (int)sum * sign;                                //最后把sum转化成int类型return sum;}




0 0
原创粉丝点击