leetcode 8. String to Integer (atoi)
来源:互联网 发布:手柄键位设置软件mac 编辑:程序博客网 时间:2024/06/16 18:29
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){if (str.size() == 0) return 0;while (str[0] == ' ')str = str.substr(1, str.size() - 1); int k = 0;long long sum = 0;if (int(str[0]) >= 48 && str[0] <= 57)sum = int(str[0]) - 48;else if (int(str[0]) == 43)k = 0;else if (int(str[0]) == 45)k = 1;elsereturn 0;for (int i = 1; i < str.size(); i++){ if (int(str[i]) >= 48 && str[i] <= 57 && sum < 2147483649)sum = sum * 10 + (int(str[i]) - 48);elsebreak;}if (k == 1){sum = -sum; if (sum < INT_MIN) return INT_MIN;}else { if (sum > INT_MAX) return INT_MAX; }return sum;}};
阅读全文
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)
- YOLOv1v2 window7+cpu版本
- c# 返回类型比委托的可访问性低
- 使用git将项目上传到github的方法
- c++学习补充
- 【第五届蓝桥杯】地宫取宝
- leetcode 8. String to Integer (atoi)
- JVM及java基本知识整理
- Android介绍(未整理完...)
- c++ 自带string类 的对象 字符串结尾带不带‘0’?
- sscanf用法
- JVM 常用参数总结
- 常见bug
- mips架构中的原子操作指令(llsc)
- Spring Cloud Config客户端配置细节