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

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) - 复杂的测试的更多相关文章

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

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  2. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  3. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  4. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  5. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  6. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  7. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  8. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  9. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

随机推荐

  1. for循环产生的Cortex-M3汇编代码的一个奇怪现象

    最近比较一下KEIL和IAR两个编译器产生的代码,基于Cortex-M3处理器的,然后发现了一几个奇怪的地方. 很简单的一个C的for循环 void fun_for_add_65535(void) { ...

  2. JAVA和.NET互调用

    通过接口实现JAVA和.NET互调用-JNInterface 使用C#编程多年,也十分感激微软在语言架构.语法糖.编辑器等方面给自己带来的便利.但因为最近工作中有接触到JAVA,渐渐地发现的确像大家说 ...

  3. 网页在Safari快速滚动和回弹的原理: -webkit-overflow-scrolling : touch;的实现

    现在很多for Mobile的H5网页内都有快速滚动和回弹的效果,看上去和原生app的效率都有得一拼. 要实现这个效果很简单,只需要加一行css代码即可: -webkit-overflow-scrol ...

  4. China&#39;s WeChat Gaining Global Attention

    近期在论坛上看见一个美丽的妹子, 本能反应,点击她的头像进去她的资料.发现她是google+认证而来.我就进一去一探到底. 增加她的圈子.发现他看过的一个视频. China's WeChat Gain ...

  5. Leetcode OJ 刷题

    Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...

  6. 自己动手写处理器之第一阶段(3)——MIPS32指令集架构简单介绍

    将陆续上传本人写的新书<自己动手写处理器>(尚未出版).今天是第四篇.我尽量每周四篇 1.4 MIPS32指令集架构简单介绍 本书设计的处理器遵循MIPS32 Release 1架构,所以 ...

  7. c++栈管理库TCMalloc、jeMalloc

    示例:http://blog.csdn.net/chosen0ne/article/details/9338591

  8. C++知识点整理——持续更新

    virtual是C++的一个关键字,virtual修饰的函数可以被子类重写.   用法:在返回值类型的前面添加关键字即可. override是C++的保留字(注意不是关键字),表示当前函数重写了基类的 ...

  9. 怎样用Excel自动排成绩

    经常需要做表格,排成绩名次总是笨笨的一个一个填,费时又费力,终于找到了解决的办法%>_<%(不要嘲笑我了(✿◡‿◡)),原来就是一个名叫RANK的函数,还在苦逼地自己输的小伙伴们,快来看吧 ...

  10. Java Socket编程 标准范例(多线程)

    链接地址:http://blog.csdn.net/benweizhu/article/details/6615542 服务器端(Server)非多线程 package com.zeph.server ...