String to Integer(ATOI) leetcode
来源:互联网 发布:vb picturebox 缩放 编辑:程序博客网 时间:2024/06/10 07:46
对题目的理解:
1.前面如果有其他的字符,只能是空格,若有其他字符也是非法
2.前面只能有一个+ - 或者没有,如果出现 +-2,或者 ++ 2,也是非法
3.越界非法
吐槽一句,恶心死我了。。。
class Solution {public: int atoi(const char *str) { int max =2147483647 ; int min =-2147483648; int len = strlen(str); if(len < 1) return 0; int i=0; double sum=0; bool flag = false , tag = false; while(i < len && str[i] == ' '){ ++i; } if(str[i] == '+') i++; else if(str[i] == '-'){ flag = true; ++i; } if(str[i] < '0' || str[i] > '9' || i >len) return 0; while(str[i] >= '0' && str[i] <= '9' && i<len ){ sum = sum*10 + (str[i]-'0'); ++i; } if(flag) sum = 0-sum ; if(sum > max) return max; if(sum < min) return min; return sum; }};
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)
- PPT:C&C++,巧用单元测试新技术,实现开发质量效率飞跃
- KNN K~近邻算法笔记
- C++入门题目小结
- php的register_long_arrays可以关闭以提高性能
- unity3D Net
- String to Integer(ATOI) leetcode
- easyUI的iframe子页面操作父页面元素
- IOS8系列之应用扩展
- asp.net mvc(模式)和三层架构(BLL、DAL、Model)的联系与区别
- Ubuntu install of ROS Indigo
- Java 用Arrays.sort()对对象数组排序之comparable接口和comparator比较器
- 树状数组 小结
- Java程序员必知的8大排序
- 关于 java.sql.Date 于 java.util.Date 之间的互相转换