String to Integer (atoi) leetcode
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的更多相关文章
- String to Integer (atoi) ---- LeetCode 008
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- String to Integer (atoi) leetcode java
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 8. String to Integer (atoi) ---Leetcode
Implement atoi to convert a string to an integer. 题目分析: 题目本身很简单就是将一个字符串转化成一个整数,但是由于字符串的千差万别,导致在实现的时候 ...
- 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 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))
8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
随机推荐
- CentOS7 部署 tomcat
1. 准备tomcat账号 本着最小权限原则,新建账号来安装tomcat. 命令:useradd , passwd 2. 配置防火墙 2.1. tomcat.xml 在/etc/firewalld/s ...
- WPF ResourceDictionary的使用
作用:一个应用程序中,某个窗口需要使用样式,但是样式非常多,写在一个窗口中代码分类不方便.最好Style写在专门的xaml文件中,然后引用到窗口中,就像HTML引用外部css文件一样. 初衷:就在于可 ...
- SVG六基本元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Canvas createImageData
createImageData() 方法创建新的空白 ImageData 对象.新对象的默认像素值 transparent black. 对于 ImageData 对象中的每个像素,都存在着四方面的信 ...
- JavaWeb知识回顾二
动态web资源相关 1.tomcat相关 tomcat的目录结构 bin -- tomcat服务器的批处理文件的存放目录 conf -- tomcat服务器配置文件的存放目录 lib -- tomca ...
- 最近总当机,IT帮网站用了1天时间成功搬家
Posted on 2017年1月22日 最近一两周,IT帮的会员.BA学员和AM学员时不时反馈网站打不开.敏捷个人APP登录不了,有时候又可以但是很慢,还有的时候电信能访问而联通网络访问不了. 这个 ...
- 常用js类型相互转换
数字转换为字符串 var a=200.21;document.write(a.toString(10)); 结果为:200.21以十进制转换 document.write(a.toFixed(3)) ...
- arcpy.mapping常用四大件-MapDocument
arcpy.mapping常用四大件-MapDocument by 李远祥 点开arcpy.mapping的帮助,可以看到其有限的几个类,看起来东西不是很多,但却是非常的使用.由于arcpy定位就是粗 ...
- Python 最大公约数的欧几里得算法及Stein算法
greatest common divisor(最大公约数) 1.欧几里得算法 欧几里德算法又称辗转相除法,用于计算两个正整数a,b的最大公约数. 其计算原理依赖于下面的定理: 两个整数的最大公约数等 ...
- Visual Studio 2017 离线安装方式
Visual Studio, 特别是Visual Studio 2017 通常是一个在线安装程序,如果你在安装过程中失去连接,你可以遇到问题.但是,由于法律原因,微软没有提供完整的可下载的ISO镜像. ...