LeetCode String to Integer (atoi)
来源:互联网 发布:linux挂载windows盘 编辑:程序博客网 时间:2024/06/06 01:53
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.
spoilers alert... click to show requirements for atoi.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
题意:将字符串转换成数字
思路:注意溢出就是了
class Solution {public: int atoi(const char *str) { if (str == NULL) return 0; while (str[0] == ' ') str++; int flag = 1; if (str[0] == '-') { flag = -1; str++; } else if (str[0] == '+') str++; long long ans = 0; for (int i = 0; str[i] >= '0' && str[i] <= '9'; i++) { ans = ans * 10 + str[i] - '0'; if (ans > INT_MAX) return (flag > 0) ? INT_MAX : INT_MIN; } return ans * flag; }};
- 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)
- hdu 1568 Fibonacci (数论)
- request表示路径
- gdb调试的若干项
- atan(正切函数)
- 有两个指针pa,pb分别指向有两个数,a,b,请写一个函数交换两个指针的指向,也就是让pa指向b,让pb指向a
- LeetCode String to Integer (atoi)
- 37-给UIImage,NSString添加扩展方法(根据内容拉伸图片,根据内容计算文本大小)
- 九度OJ 1056 最大公约数
- 开辟一个二维数组,有10*8个元素,用随机数填充,按照下面的方法用函数实现查找一个数是否存在
- HTML5 本地存储
- ubuntu 挂载 window7 共享文件夹
- 【Servlet】微信接入算法
- java学习--IO包中的其他类
- 微信基本功能的封装