LeetCode 8. String to Integer (atoi)
来源:互联网 发布:万捷网络验证要多少钱 编辑:程序博客网 时间:2024/06/10 21: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.
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.
answer:
public:int myAtoi(string str) {int length = str.length();if(length == 0) return 0;double result = 0.0f, i = 0;int flag = 1;while((i < length) && (str[i] == ' '))i ++;if(i >= length) return 0;if(str[i] == '+'){flag = 1;}else if(str[i] == '-'){flag = -1;}else if(str[i] < '0' || str[i] > '9') {return 0;}else {flag = 1;result += str[i] - '0';}i ++;while(i < length && str[i] >= '0' && str[i] <= '9'){result = result * 10 + str[i] - '0';if(result * flag > INT_MAX) { result = INT_MAX; return result;}if(result * flag < INT_MIN){ result = INT_MIN; return result;}i ++;}return result * flag;}};
- [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)
- 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 OJ> 8. String to Integer (atoi)
- leetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) LeetCode
- leetcode 8. String to Integer (atoi)
- leetcode 8. String to Integer (atoi)
- LeetCode OJ 8.String to Integer (atoi)
- Leetcode ☞ 8. String to Integer (atoi)
- 8. String to Integer (atoi) ---Leetcode
- leetcode 8. String to Integer (atoi)
- Leetcode: 8. String to Integer (atoi)(JAVA)
- Hibernate 笔记大全二
- a href=#与 a href=javascript:void(0) 的区别
- logstash grok
- Android 4.0新的广播机制FLAG_EXCLUDE_STOPPED_PACKAGES
- Codeforces 765D Artsem and Saunders 构造
- LeetCode 8. String to Integer (atoi)
- retrofit 学习和使用
- CI3框架初识-钩子
- nginx 日志配置与分割
- AndroidManifest.xml 清单文件
- Linux NTP config
- iOS设计模式-建造者模式
- android控件拖动,移动、解决父布局重绘时控件回到原点
- 任务栏窗口隐藏