leetcode解题报告8. String to Integer (atoi)
来源:互联网 发布:securecrt mac 注册码 编辑:程序博客网 时间:2024/06/01 10:42
leetcode解题报告8. String to Integer (atoi)
题目地址
难度是medium
题目描述
把一个字符串,变成数字
比如”123”, 返回数字123
我的思路
粗看,是很简单的模拟类题目。但是提了几次才过,难点在于考虑各种边界情况(特殊情况)
比如,字符串为空,前面是空字符,前面有符号标示(正负号),字符串包含其他符号,还有溢出等问题。
我的代码
class Solution {public: int myAtoi(string str) { if (str.size() == 0) { return 0; } int index = 0; int flag = 1; while (str[index] == ' ') { index++; } if (str[index] == '+' || str[index] == '-') { if (str[index] == '-') { flag = -1; } index++; } long ans = 0; while (index < str.size()) { int t = str[index] - 48; if (t < 0 || t > 9) { return flag * ans; } ans = ans * 10 + t; if (ans > 2147483647) { if (flag == -1) { return -2147483648; } return 2147483647; } index++; } return flag * ans; }};
阅读官方题解
没有官方题解,从网上信息来看,题目重点考察的是对特殊情况,边界情况的考虑,也没什么可说的。
思想核心总结
考察对特殊情况,边界情况的考虑
0 0
- 【LeetCode】String to Integer (atoi) 解题报告
- LeetCode解题报告--String to Integer (atoi)
- LeetCode-String to Integer (atoi) -解题报告
- [LeetCode] String to Integer (atoi) 解题报告
- [LeetCode] String to Integer (atoi) 解题报告
- String to Integer (atoi) LeetCode 解题报告
- [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 解题报告]008.String to Integer (atoi)
- LeetCode(8) String to Integer (atoi)解题报告
- String to Integer (atoi)解题报告
- [Leetcode]解题文档-String to Integer (atoi)
- [LeetCode]8. String to Integer (atoi)
- LeetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) Leetcode Python
- LeetCode --- 8. String to Integer (atoi)
- Java LinkedHashMap工作原理及实现(转)
- B. Masha and geometric depression-模拟
- 十四、Mysql 去重复
- ASP.NET C#学习一(环境基础知识)
- UVA 232
- leetcode解题报告8. String to Integer (atoi)
- 如何安装WordPress博客
- WMI Provider Host 占用CPU 过高的处理
- opencv与emgucv的参数传递
- 题目1434:今年暑假不AC 九度OJ
- 一点点想法
- Android TextView重写onMeasure和onDraw显示无拉伸图片(含实现代码链接)
- JS特效--跟随鼠标移动的日历时钟
- String,StringBuffer与StringBuilder的区别??