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 ...
随机推荐
- pureMVC介绍及学习
1 简介 Pure MVC是在基于模型.视图和控制器MVC模式建立的一个轻量级的应用框架,这种开源框架是免费的,它最初是执行的ActionScript 3语言使用的Adobe Flex.Flash ...
- Bootstrap入门(二十二)组件16:列表组
Bootstrap入门(二十二)组件16:列表组 列表组是灵活又强大的组件,不仅能用于显示一组简单的元素,还能用于复杂的定制的内容. 1.默认样式列表组 2.加入徽章 3.链接 4.禁用的列表组 5. ...
- 2016年,总结篇 之 VueJS 如何入门(一)
接着 2016 年的总结,我们来看看 2016年 国内最火且没有之一的前端MVVM 框架 VueJs 虽然 到写文章的这个时间点,VueJs已经发布了 2.1.x 了, 但是对于很多 Vuejs 的初 ...
- 移动端H5页面遇到的问题总结
最近刚做完一个移动端的项目,产品之无敌,过程之艰辛,我就不多说了,记录下在这个项目中遇到的问题,以防万一,虽然这些可能都是已经被N多前辈解决掉了的问题,也放在这里,算是为自己漫漫前端路铺了一颗小石子儿 ...
- java Runtime类
public class Test { public static void main(String[] args) throws UnsupportedEncodingException { Run ...
- HDU4403(暴搜)
A very hard Aoshu problem Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- WinForm 对话框、流
一.对话框 ColorDialog:颜色选择控件 private void button1_Click(object sender, EventArgs e) { //显示颜色选择器 colorDia ...
- 蓝桥网试题 java 基础练习 特殊的数字
-------------------------------------------------------- 笑脸 :-) ------------------------------------ ...
- 用label实现自适应宽度的方法
- PrintWriter用法简析
public class PrintWriterextends Writer 向文本输出流打印对象的格式化表示形式.此类实现在 PrintStream 中的所有 print 方法.它不包含用于写入原始 ...