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 ...
随机推荐
- sitemap.xml 的 几个东西
https://github.com/PureKrome/SimpleSitemap/wiki/Sitemap-Index-example 简单类实现 支持sitemapindex 有说明向导 ht ...
- Spring @Transactional使用的示例
Spring @Transactional使用的示例: 参考: http://blog.csdn.net/seng3018/article/details/6690527 http://blog.si ...
- Flex移动皮肤开发(二)
范例文件 mobile-skinning-part2.zip 在这个讨论创建 Flex 移动 skin 的系列的 第 1 部分 中,我讨论了 Flex 团队在 Mobile 主题中所做的性能优化的原理 ...
- Spring 基于Java的Bean声明
Spring 基于Java的Bean声明 使用@Configuration进行设置: Xml: <?xml version="1.0" encoding="UTF- ...
- C++:C语言实现HTTP的GET和POST请求
HTTP请求和IP/TCP 所谓的HTTP协议是基于IP/TCP协议的, 所以要获取远端的html数据只要创建socket对象就足够了: HTTP是基于IP/TCP加上了网络请求的固定格式, 比如: ...
- Android中的IPC机制
Android IPC简介 IPC是Inter-Process Communication的缩写,含义就是进程间通信或者跨进程通信,是指两个进程之间进行数据交换的过程.那么什么是进程,什么是线程,进程 ...
- 《JAVASCRIPT高级程序设计》第一章
在使用调制解调器的时代,频繁的表单验证对客户端来说是一个很大的负担,javascript,作为一种专门进行表单验证的客户端脚本语言诞生了.到今天,javascript早已超越了当初设定的角色.Java ...
- yii2 邮件发送(有图有真相)
经典的密码找回方案是发送邮件到用户邮箱然后修改密码,下面利用yii2 高级版的mail功能,进行邮件的发送,如下图 1.在comm/config/main-local.php中添加 'mailer' ...
- Windows下搭建HTTP/HTTPS服务器及测试过程
1 安装Apache http://www.apachehaus.com/cgi-bin/download.plx 选择合适的版本下载 本次下载的是 Apache 2.4.x VC14 Apache版 ...
- RejectedExecutionException 分析
当往一个固定队列ArrayBlockingQueue 不停的提交任务时,会发生什么? 请看如下代码 private static final int QUEUE_SIZE = 20; private ...