String to Integer (atoi) - 复杂的测试

这个题。。是要把字符串转为整数。注意是整数,我看到整数的时候松了一口气,没有小数点的判断应该更好做。而且基本的转化函数我想每个程序员都无法忘记:

res=res*10+(str[i]-‘0‘);

其实就是这么一句话的事情,然而这个题的通过率只有13%,在200多个题目中排名第五。本想不看提示自己写了一些判断,然而仍逃不掉wa的结局。

看了下面这堆requirement,还是有很大概率wa。

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.

这个函数在第一个非空白字符出现可以丢弃尽量多的空白字符。然后从这个非空白字符开始,获取一个初始的加号或者减号,然后之后的数字字符会被转成一个数。

这个字符串在形成一个数之后可以包含其他字符,函数应该不受他们影响并忽略它们。

如果第一个非空白字符不是一个合法的数字字符,或者这个序列不存在即为空或者全为空白字符,不进行任何转换。

如果没有进行转换,函数应该返回0值。如果正确的数值越界了,应该返回2147483647或者-2147483648。

提供一些在dicuss收集的错误结果和被wa的样例:

Input Output Expected
"    b11228552307" 2147483647 0
"+-2" 2 0
"  -0012a42" 0 -12
"   +0 123" 123 0
" 10522545459" 1932610867 2147483647

不得不说测试样例太凶险。

以下是AC代码:

 1 class Solution {
 2 public:
 3     int myAtoi(string str) {
 4
 5         long res=0;
 6         int flag=1;
 7         int i=0;
 8         int count=0;
 9         while(str[i]==‘ ‘)
10             i++;
11         if(str[i]==‘-‘)
12         {
13             flag=-1;i++;
14             count++;
15         }
16         else
17             if(str[i]==‘+‘){
18             flag=1;i++;
19             count++;
20             }
21             else
22             {
23                 if(str[i]>‘9‘||str[i]<‘0‘)
24                     return 0;
25             }
26         if(count>1)return 0;
27         int numcount=0;
28         for(;i<str.length();i++)
29         {
30             if(str[i]<=‘9‘&&str[i]>=‘0‘){
31                 numcount++;
32                 res=res*10+(str[i]-‘0‘);
33                 if(numcount>11)
34                 {
35                     res=2147483649;
36                     break;
37                 }
38                 continue;
39             }
40             else
41                 break;
42         }
43         if(res>2147483647&&flag==1)return 2147483647;
44         if(res>2147483648&&flag==-1)return -2147483648;
45         int final=res;
46         return final*flag;
47     }
48 };

flag是用来判断正负的,count是用来判断正负号数目的,如果读到非数字字符就结束,numcount是用来判断越界的。本题我使用了long这个数据类型先进行转化,这样在小于11个数的范围内,long是不会越界的,直接将最后结果赋值给一个int型即可。

在AC之后官方给出了解锁的Solution:

To deal with overflow, inspect the current number before multiplication. If the current number is greater than 214748364, we know it is going to overflow. On the other hand, if the current number is equal to 214748364, we know that it will overflow only when the current digit is greater than or equal to 8.

Average Rating: 3.7 (305 votes)

大意是在做下一次乘之前判断下当前数是否是214748364,如果大于这个数继续乘是一定越界的,如果此时的待加数字是8或者以上也是越界的(正负数还要分7和8讨论)。

这个题虽然简单,但是在写的时候感觉要考虑的情况还是很多,在判断越界的方法上有三种:

第一种是这种官方给的做法,推荐使用;

第二种是使用一个更大的数据类型来乘放这个数据,在位数超过10的时候直接break;

第三种是使用字符串匹配,我在Reverse Integer使用的是这个方法。

PS:在32位的编译器上:

unsigned int取值范围为:0 - 4294967295;

int的取值范围是-2147483648 - 2147483647(2的32次方);

long 的取值在32位编译器上和int是相同的,但是在64位的编译器上是有8个字节的,所以比int表示范围要大很多。本题中使用long可以通过那个数值溢出的样例,说明评测机也是64位的。

long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808

__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808

时间: 2024-08-02 17:58:20

String to Integer (atoi) - 复杂的测试的相关文章

leetcode——String to Integer (atoi) 字符串转换为整型数(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

LeetCode7~9 Reverse Integer/String to Integer (atoi)/Palindrome Number

一:Reverse Integer 题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 链接:https://leetcode.com/problems/reverse-integer/ 分析:这题通过不断取余将余数存放在一个vector中,然后乘以相应的10^i次方相加即可,这里主要考虑是否overflow,因此将result设为long long int

Kotlin实现LeetCode算法题之String to Integer (atoi)

题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 class Solution { 2 fun myAtoi(str: String): Int { 3 val maxInt = "2147483647" 4 val maxIntS = "+2147483647" 5 val minIntS = "-21474836

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,

【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][JavaScript]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

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