C++ 写leetcode遇到的一些问题总结string to integer
来源:互联网 发布:淘宝必买清单怎么写 编辑:程序博客网 时间:2024/04/30 00:19
一、
String to Integer (atoi)
这一题主要难点在于考虑条件。
符合规定条件:1、+123,-123,123;
2、1323s
3、_ _ _ _134
4、0000123,+000123,-000123
不符合条件的情况:1、-+123,s123,-s123
2、数据溢出
代码:
class Solution {public:int myAtoi(string str) {if (str == "") return 0;int result = 0;int flag = 0;int strEnd = str.length() - 1;int sign = 1;int flagBegin = 0;int INT_MAX_MIN_PRE = INT_MAX / 10;int INT_MAX_LAST = INT_MAX % 10;int INT_MIN_LAST = INT_MAX_LAST + 1;while (flag <= strEnd){if (str[flag] != ' ') break;flag++;}if (str[flag] == '-'){sign = -1;flag++;}else if (str[flag] == '+') flag++;while (flag <= strEnd){if (str[flag] != '0') break;flag++;}while (flag <= strEnd){if (str[flag] > '9' || str[flag] < '0') break;if (sign == 1 && (result > INT_MAX_MIN_PRE || (result == INT_MAX_MIN_PRE && str[flag] - '0' > INT_MAX_LAST))){return INT_MAX;}if (sign == -1 && (result > INT_MAX_MIN_PRE || (result == INT_MAX_MIN_PRE && str[flag] - '0' > INT_MIN_LAST))){return INT_MIN;}result = result * 10 + str[flag] - '0';flag++;}return result * sign;}};
0 0
- C++ 写leetcode遇到的一些问题总结string to integer
- C++ 写leetcode遇到的一些问题总结Reverse Words in a String
- C++ 写leetcode遇到的一些问题总结Implement strStr()
- [C++]LeetCode: 24 String to Integer (atoi)
- 【leetcode c++】08 String to Integer (atoi)
- Leetcode c语言-String to Integer (atoi)
- C++ 写leetcode遇到的一些问题总结Restore IP Addresses
- [LeetCode]String to Integer
- 【Leetcode】String to Integer
- [leetcode] string to integer
- leetcode-string to integer
- Leetcode: String to Integer
- [LeetCode]String to Integer
- LeetCode:String to Integer
- LeetCode--String to Integer
- leetcode string to integer
- [leetcode] String to Integer
- LeetCode-String To Integer
- MacBook Pro 开机密码忘记的解决办法
- setjmp 及 alarm实现带超时限制调用read
- 如何使自己的QML应用不进入到屏保状态以保持屏幕是亮的
- Pro Android学习笔记(一六五):联系人API(8):控制整合
- LeetCode的medium题集合(C++实现)十三
- C++ 写leetcode遇到的一些问题总结string to integer
- Android Hook学习之ptrace函数的使用
- python爬虫第一课,制作搜索引擎
- Hiho 国庆出游
- Android应用Context详解及源码解析
- Uncaught TypeError: undefined is not a function
- 各种距离算法汇总
- 用c++实现 c++顺序表的实现(采用模板)
- linux下清空文件内容方法