[leetcode] 8. String to Integer (atoi) 解题报告
来源:互联网 发布:sql 选取第二大 编辑:程序博客网 时间:2024/06/10 02:22
题目链接:https://leetcode.com/problems/string-to-integer-atoi/
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.
Update (2015-02-10):
The signature of the C++
function had been updated. If you still see your function signature accepts a const char *
argument, please click the reload button to reset your code definition.
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.
思路:好讨厌这种处理字符串的,WA了好多次又。还剩最后一题尼姆博弈就把easy KO了,现在123了,
代码如下:
class Solution {public: int myAtoi(string str) { long i =0, ans =0, flag = 1; while(str[i] == ' ') i++; if(str[i] == '+' || str[i] == '-') { if(str[i] == '-') flag = -1; i++; } while(isdigit(str[i])) { ans = 10*ans + (str[i++] - '0'); if(ans*flag > INT_MAX) return INT_MAX; else if(ans*flag < INT_MIN) return INT_MIN; } return flag * ans; }};
- 【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) 解题报告
- String to Integer (atoi) LeetCode 解题报告
- [leetcode] 8. String to Integer (atoi) 解题报告
- [Leetcode] 8. String to Integer (atoi) 解题报告
- leetcode解题报告8. String to Integer (atoi)
- 【Leetcode】8.String to Integer (atoi) 解题
- [LeetCode 解题报告]008.String to Integer (atoi)
- LeetCode(8) String to Integer (atoi)解题报告
- String to Integer (atoi)解题报告
- [Leetcode]解题文档-String to Integer (atoi)
- [LeetCode]8. String to Integer (atoi)
- LeetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) Leetcode Python
- LeetCode --- 8. String to Integer (atoi)
- ASP.NET动态绑定HighCharts图表控件
- cell 重用 视图叠加 -- 转自互联网bkjia.com
- jquery选择器 之 获取父级元素、同级元素、子元素
- android:windowSoftInputMode属性详解
- AndroidStudio使用记录
- [leetcode] 8. String to Integer (atoi) 解题报告
- POJ 1811 Prime Test(素数判定Miller-Rabin+素因子分解Pollard-rho)
- top 使用技巧
- sf4j+logback
- 线程操作
- 设计模式——观察者模式 Java源代码
- Educational Codeforces Round 3 B. The Best Gift
- MyEclipse使用DB Browser连接Oracle数据库
- ANDROID自定义视图——onMeasure流程,MeasureSpec详解