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.

这一类题目需要考虑所有输入情况,题目给出的条件非常模糊不清,需要我们猜测可能的特殊情况。所以,反复调试是不可避免的。

我首先考虑到了+、-号的情况,然后想到了非数字情况,比如空格和字母、特殊字符等等,空格可以忽略,字母和特殊字符需要终止,判为非法输入。

想出这些情况后,我没有急于编程,搜索了其他人的答案后,发现自己还漏掉了一种最容易忽略的情况——数字溢出

如果输入的值大于int的最大值2147483647该怎么处理?按照C语言的转换规则,高位被截去,只能保留低位数字,这里就不需要这么复杂了吧,只需要设置成最大值就行了。

如何是小于最小值呢?-2147483646,同样的道理,设置成最小值。

这里我还想到了一种情况,那就是有小数点,这里虽然题目没有要求,我还是加入了,实现参考的是C语言的强制转换规则,不是四舍五入。

实现好后,提交,发现居然还有"-+10"这种输入情况,如果有这种情况,那么"--+"、"-+-+++"这些复杂的输入都可以吧。事实上leetcode只提供了"-+"、"+="这两种情况,这样的设置真是非常坑爹,可以说是没有意义的。

int myAtoi(string str) {
int i = ;
while (str[i] == ' ')
i++;
int sign = ;
if (str[i] == '-' || str[i] == '+') {
sign = - * (str[i++] == '-');
}
int sum = ;
while (i < str.length() && isdigit(str[i]))
{
if (sum > INT_MAX / || (sum == INT_MAX / && str[i] - '' > )) {
if (sign == ) return INT_MAX;
else return INT_MIN;
}
sum = * sum + (str[i++] - '');
}
if (i + < str.length())
if (str[i] == '.' && isdigit(str[i + ]))
sum += sign;
return sum * sign;
}

后记:

做这样的题,就好像项目没有明确的需求方案,全靠程序员自己脑补需求一样。猿们现实工作的蛋疼之处可见一斑。

经验丰富的程序员,就是那种真正懂得客户需求的人吧,代码可以编的不多不少,恰到好处,真的需要经历太多历练才得达到这样的水平。

String to Integer (atoi) leetcode的更多相关文章

  1. String to Integer (atoi) ---- LeetCode 008

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. String to Integer (atoi) leetcode java

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  3. 8. String to Integer (atoi) ---Leetcode

    Implement atoi to convert a string to an integer. 题目分析: 题目本身很简单就是将一个字符串转化成一个整数,但是由于字符串的千差万别,导致在实现的时候 ...

  4. 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 ...

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

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

  6. LeetCode: String to Integer (atoi) 解题报告

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

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

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

  8. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  9. 【leetcode】String to Integer (atoi)

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

随机推荐

  1. SVGEditor

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. JavaScript 轻松创建级联函数

    级联函数是什么? 在一行代码上,调用一个接一个的方法.这种技术在 JQuery 或者其他 JavaScript 库中是非常常见的. 代码如下: $('#myDiv').fadeOut().html(' ...

  3. 部署AlwaysOn第一步:搭建Windows服务器故障转移集群

    在Windows Server 2012 R2 DataCenter 环境中搭建集群之前,首先要对Windows服务器故障转移集群(Windows Server Failover Cluster,简称 ...

  4. 初识Dapper

    16年年底开发一个项目,拍卖的项目,对于我这个不入流的程序员来说,雪微是个挑战.程序猿这个行业就是学到老用到老吧.个人比较喜欢sql原生的写法,对EF 还是不怎么感冒,EF 虽然强大,但是用起来还不怎 ...

  5. SQLServer2008开放windows防火墙配置

    为了可以通过TCP/IP协议远程访问SQLServer数据库,需要做以下几点: 在SQLServer所运行的服务器上,我们必须找到SQLServer所侦听的端口然后添加到WIndows防火墙的[允许入 ...

  6. UTF编码检测

    最近工作上正好需要进行UTF编码检测,自己写了一个,分享给大家,希望可以帮得上有需要用的朋友 public bool isUtf8(byte[] rawText) { bool result = tr ...

  7. mac下导出kindle单词本的单词

    平常都是用kindle来看电子书,偶尔也会看上一些英文书籍,不可避免的会遇到不少陌生的单词,而kindle专门针对这种需求,做了不少优化,可以直接在kindle上面查阅单词,甚至可以背单词.但是毕竟不 ...

  8. canvas粒子时钟

    前面的话 本文将使用canvas实现粒子时钟效果 效果展示 点阵数字 digit.js是一个三维数组,包含的是0到9以及冒号(digit[10])的二维点阵.每个数字的点阵表示是7*10大小的二维数组 ...

  9. 使用python制作ArcGIS插件(4)界面交互

    使用python制作ArcGIS插件(4)界面交互 by 李远祥 插件界面部分,除了一开始在设计器中设计的这些界面元素之外,还可以与操作系统进行一些输入输出的交互,这部分的实现全部在pythonadd ...

  10. VMware workstation安装报Microsoft Runtime DLL和Intel VT-x错误

    在安装VMware workstation时,弹出提示框,提示"安装程序无法继续.Microsoft Runtime DLL安装程序未能完成安装." 网上找到两种说法,但我的win ...