Leetcode#8 String to Integer (atoi) $corner case
来源:互联网 发布:linux 重启后不能远程 编辑:程序博客网 时间:2024/06/16 22:14
这是Leetcode上一道难度为Easy,但是AC率倒数第二的宝搞题目。难点主要在于要兼顾各种input case。
来自leetcode discuss区域
主要要注意:
- discards all leading whitespaces (忽略符号和数字前的空格)
- sign of the number(处理正负号)
- overflow(处理overflow)
- invalid input(处理非法输入)
其他的三点处理起来都还算容易,overflow处理,我的代码是采用单独设一个long long flag 值和 return值一样,如果下一步flag>INT_MAX,就溢出了。discuss区的这个示例处理的更加好一些。
int atoi(const char *str) { int sign = 1, base = 0, i = 0; while (str[i] == ' ') { i++; } if (str[i] == '-' || str[i] == '+') { sign = 1 - 2 * (str[i++] == '-'); } while (str[i] >= '0' && str[i] <= '9') { if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > 7)) { if (sign == 1) return INT_MAX; else return INT_MIN; } base = 10 * base + (str[i++] - '0'); } return base * sign;}
0 0
- Leetcode#8 String to Integer (atoi) $corner case
- LeetCode 8 - String to Integer (atoi)
- [LeetCode 8] String to Integer (atoi)
- leetcode 8STRING TO INTEGER (ATOI)
- Leetcode【8】:String to Integer(atoi)
- [leetcode 8] String to Integer (atoi)
- LeetCode(8) String To Integer(atoi)
- [leetcode 8] String to Integer (atoi)
- LeetCode 8:《String to Integer (atoi) 》
- leetcode.8---------------String to Integer (atoi)
- leetCode #8 String to Integer (atoi)
- [LeetCode Java] 8 String to Integer (atoi)
- leetcode 8 String to Integer (atoi)
- leetcode-8 String to Integer(atoi)
- LeetCode 8 String to Integer (atoi)
- LeetCode(8)String to Integer (atoi)
- LeetCode 8 - String to Integer (atoi)
- leetcode #8 String to Integer (atoi)
- Android基础入门教程——2.5.2 Notification(状态栏通知)详解
- jQuery之滑动
- Android绘图机制(四)自定义控件
- HTTP返回码总结
- jQuery中click和onclick的区别
- Leetcode#8 String to Integer (atoi) $corner case
- 虚拟存储子系统调整
- js的事件代理和事件冒泡
- 黑马程序员—字节流+字符流缓冲区(装饰设计)+转换流+IO流操作规律+异常日志+系统信息列表输出
- 常用位操作小技巧
- ubuntu安装MySQL
- 跟我一起写编译器(一)——lex&yacc【转载】
- GetDC()函数
- MongoDB数据库设计中6条重要的经验法则(一)