Leetcode-8. String to Integer (atoi)
来源:互联网 发布:前端整么添加数据 编辑:程序博客网 时间:2024/06/05 11:45
8. String to Integer (atoi)
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.
class Solution {public: int myAtoi(string 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)
- LeetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) Leetcode Python
- 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 OJ> 8. String to Integer (atoi)
- leetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) LeetCode
- leetcode 8. String to Integer (atoi)
- leetcode 8. String to Integer (atoi)
- LeetCode OJ 8.String to Integer (atoi)
- Leetcode ☞ 8. String to Integer (atoi)
- 8. String to Integer (atoi) ---Leetcode
- leetcode 8. String to Integer (atoi)
- Leetcode: 8. String to Integer (atoi)(JAVA)
- 用一个数组实现三个栈结构
- loj#526. 「LibreOJ β Round #4」子集
- addEventListener与handleEvent
- 中间件 消息队列 ActiveMQ
- 关于iOS中委托(Delegate)的几点看法
- Leetcode-8. String to Integer (atoi)
- 【Spring in action】SpringMVC的准备工作:Servlet基础
- 数据库三范式
- 西瓜书《机器学习》阅读笔记3——Chapter2_ROC曲线
- 【HTTP】Fiddler(一)
- 用javascript实现图片上传预览
- Python基础
- iOS 彻底学会使用delegate
- C# 反射(Reflection)