Leetcode: String to Integer (atoi)
来源:互联网 发布:mac diva 编辑:程序博客网 时间:2024/05/16 15:45
题目:
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.
参考代码:
#define INT_MAX 2147483647#define INT_MIN (-INT_MAX-1)class Solution {public: int atoi(const char *str) { if (str == nullptr) { return 0; } int position = 0; while (str[position] == ' ') { position++; } if (str[position] == '\0') { return 0; } int signal = 1; if (str[position] == '+' || str[position] == '-') { if (str[position] == '-') { signal = -1; } position++; } long long result = 0;// 注意这里是long long while (str[position] >= '0' && str[position] <= '9') { result = result * 10 + str[position] - '0'; if (result * signal > INT_MAX) { return INT_MAX; } if (result * signal < INT_MIN) { return INT_MIN; } position++; } return result * signal; }};
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)
- Dome of Circus - UVa 1473 几何 凸包
- 常见的连续概率密度函数
- C++ 多重继承,虚析构确定
- 一个android列表的适配器数据异步加载的问题
- 网站开发流程
- Leetcode: String to Integer (atoi)
- html+css第二课
- 用户密码的存储与 Python 示例
- Android中AsyncTask详解.
- 机顶盒的层次结构
- 密码传输问题
- 黑马JAVA知识点总结
- std::bind
- 源码管理十六:标签管理