String to Integer (atoi) - 复杂的测试
这个题。。是要把字符串转为整数。注意是整数,我看到整数的时候松了一口气,没有小数点的判断应该更好做。而且基本的转化函数我想每个程序员都无法忘记:
res=res*+(str[i]-'');
其实就是这么一句话的事情,然而这个题的通过率只有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代码:
class Solution {
public:
int myAtoi(string str) {
long res=;
int flag=;
int i=;
int count=;
while(str[i]==' ')
i++;
if(str[i]=='-')
{
flag=-;i++;
count++;
}
else
if(str[i]=='+'){
flag=;i++;
count++;
}
else
{
if(str[i]>''||str[i]<'')
return ;
}
if(count>)return ;
int numcount=;
for(;i<str.length();i++)
{
if(str[i]<=''&&str[i]>=''){
numcount++;
res=res*+(str[i]-'');
if(numcount>)
{
res=;
break;
}
continue;
}
else
break;
}
if(res>&&flag==)return ;
if(res>&&flag==-)return -;
int final=res;
return final*flag;
}
};
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
String to Integer (atoi) - 复杂的测试的更多相关文章
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
随机推荐
- android-服务Service
服务是在后台运行,负责更新内容提供器.发出意图.触发通知,它们是执行持续或定时处理的方式. 多线程一般捆绑服务执行任务,因为在activity中开辟多线程执行任务的话,子线程的生命周期得不到保障,可能 ...
- JAVA泛型-擦除
package com.xt.thinks15_7; import java.util.Arrays; class EraseObject1<A> { } class EraseObjec ...
- 【基础】常用的机器学习&数据挖掘知识点
Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Least Square Methods 最小二乘法),ML ...
- 害人的VS2008,manifest导致“应用程序配置不正确,应用程序未能启动”
在VC++2008的项目中,如何显示地指定要使用的C++库的版本? 开发环境:VS2008 SP1 + Win2003 SP2 因为我的VS2008安装了SP1补丁,所以有了9.0.3 ...
- Candy----HDU4465----数学题
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4465 题目意思: 有两个箱子,每个箱子装有N个糖果 打开第一个箱子的概率是P,另外一个就是1-P 当小 ...
- block 解析 - 内存
block结构体相应的也有一个成员引用,这样会增加对局部变量的 _para1引用,在Block销毁的时候引用就释放掉了 我们了解到了用__block修饰的变量,可以在block内部修改,__block ...
- python 类属性、对象属性
类的普通属性: dir(Myclass), 返回一个key列表: Myclass.__dir__,返回一个字典: 1.类的数据属性: 2.类的方法: 类的特殊属性: 1.Myclass.__name_ ...
- 捉Bug:易车注册页布局小臭虫 附demo
--------<!--文章开始-->-------- 开场show:这几天天气异常燥热,早上起来从棉毛衣换到了小短袖,配上一杯冷泡乌龙茶,真是没谁了,这哥们懂得享受哈 开始鼓捣博客,把博 ...
- Objective-c 集合对象
集合(NSSet)是一组单值对象的组合,集合对象的操作包括:搜索,添加,删除集合中的成员(可变集合的功能),比较两个集合,计算两个集合的交集,并集等. 下面来看下(NSSet)的方法: 1)集合的构建 ...
- BZOJ 2424: [HAOI2010]订货(最小费用最大流)
最小费用最大流..乱搞即可 ------------------------------------------------------------------------------ #includ ...