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 ...
随机推荐
- [Scoi2010]游戏
游戏 Time Limit:5000MS Memory Limit:165888KB 64bit IO Format:%lld & %llu Submit Status Pra ...
- java虚拟机启动参数整理
java启动参数 共分为三类其一是标准参数(-),所有的JVM实现都必须实现这些参数的功能,而且向后兼容:其二是非标准参数(-X),默认jvm实现这些参数的功能,但是并不保证所有jvm实现都满足,且不 ...
- Javascript日期格式化指定格式的字符串实现
代码部分 TypeScript /** * format a Date object * 将 Date 转化为指定格式的String * @param {Date} date 源日期对象 * @par ...
- C++ 头文件系列 (algorithm)
简介 algorithm头文件是C++的标准算法库,它主要应用在容器上. 因为所有的算法都是通过迭代器进行操作的,所以算法的运算实际上是和具体的数据结构相分离的 ,也就是说,具有低耦合性. 因此,任何 ...
- java_JDBC字段对应
地址: http://otndnld.oracle.co.jp/document/products/oracle10g/102/doc_cd/java.102/B19275-03/datacc.htm ...
- PHP面向对象(OOP)----分页类
> 同验证码类,分页也是在个人博客,论坛等网站中不可缺少的方式,通过分页可以在一个界面展示固定条数的数据,而不至于将所有数据全部罗列到一起,实现分页的原理其实就是对数据库查询输出加了一个limi ...
- Vuex核心知识(2.0)
Vuex 是一个专门为 Vue.js 应该程序开发的状态管理模式,它类似于 Redux 应用于 React 项目中,他们都是一种 Flux 架构.相比 Redux,Vuex 更简洁,学习成本更低.希望 ...
- linux 进程间通信 之fifo
上一篇博客已经介绍了一种进程间通信的方式,但是那只是针对于有血缘关系的进程,即父子进程间的通信,那对于没有血缘关系的进程,那要怎么通信呢? 这就要创建一个有名管道,来解决无血缘关系的进程通信, fi ...
- MySQL日志系统
body { font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 1.6; padding-top: 10 ...
- SpringMvc拦截器小测试
前言 俗话说做项目是让人成长最快的方案,最近小编写项目的时候遇到了一个小问题.小编在项目中所负责的后台系统,但是后台系统是通过系统的页面是通过ifame联动的,那么这时候问题就来了,后台所做的所有操作 ...