leetcode---String to Integer (atoi)
来源:互联网 发布:nginx和zookeeper 编辑:程序博客网 时间:2024/06/11 16:30
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: bool isNum(char a) { if(a < '0' || a > '9') return false; return true; } int myAtoi(string str) { int result = 0; int len = str.length(); int flag; int i = 0; while(str[i] == ' ') //去掉前面的空格 i++; if((str[i] == '+' || str[i] == '-') && (str[i+1] == '+' || str[i+1] == '-')) { return 0; } else if(str[i] == '-') { flag = -1; i++; } else if(str[i] == '+') { flag = 1; i++; } else { flag = 1; } while(str[i] == '0') //去掉前面的0 i++; int j = i; for(; i<len; i++) { if(!isNum(str[i])) break; result = result * 10 + str[i] - '0'; if(i-j >= 10 || result < 0) //溢出 if(flag > 0) return pow(2,31) - 1; else return pow(2, 31) * (-1); } return result * flag; }};
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)
- 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)
- 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)
- [LeetCode] String to Integer (atoi)
- leetcode String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- Sublime Text3安装 + 破解码(最新) + 插件
- Android学习之Design Support Library中TextInputLayout的使用
- IOS学习之——通讯录的应用
- Log4j的使用
- hdoj 幸福列车 1434 (模拟&队列)
- leetcode---String to Integer (atoi)
- 如何将一个 android工程作为依赖工程
- matlab多变量牛顿方法求解非线性方程组
- Number spiral diagonals
- nyoj拦截导弹
- 视觉是如何演化的1
- HDFS DataNode 设计实现解析
- 2016-AspNet-MVC教学-8-异步Controller的应用
- ROS系统安装