8. String to Integer (atoi)
来源:互联网 发布:便利蜂上班怎么样 知乎 编辑:程序博客网 时间:2024/06/06 04:24
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 returne
这代码写的很恶心 ,因为之前没看到atoi,一边提交,一边改,实在是难看:
class Solution {public:int myAtoi(string str) {int len = str.length();int index = 0;long ans = 0;bool flag = false;bool jia = false;while (str[index] - '0' < 0 || str[index] - '0' > 9){ if (str[index] != '+' && str[index] != '-' && str[index] != ' ') return 0 ;if (str[index] == '-') { if (index + 1 < len && (str[index + 1] - '0' < 0 || str[index + 1] - '0' > 9)) return 0; if (jia) return 0;flag = true;}if (str[index] == '+'){ if (index + 1 < len && (str[index + 1] - '0' < 0 || str[index + 1] - '0' > 9)) return 0;if (flag) return 0;jia = true;}++index;}for (int i = index; i < len; ++i){if (int(str[i] - '0') < 0 || int(str[i] - '0') > 9){if (flag) ans = ans * -1;return ans;}ans = ans * 10 + int(str[i] - '0');if (!flag && ans > 2147483647) return 2147483647;if (flag && ans > 2147483648) return -2147483648;}if (flag) ans *= -1;return ans;}};
- 8.String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8.String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- HDOJ 2014 青年歌手大奖赛_评委会打分
- 数据结构实验之排序七:选课名单
- 2017-12-20作业4 for循环和数组
- 内存缓存
- (安卓)图片上传
- 8. String to Integer (atoi)
- Java 用eclipse调试老是提示Source not found解决方法——关联
- JAVAWEB第七天java基础加强
- 4-1 复数类的运算符重载
- Android——实现网络下载资源
- jquery ajax同步
- 使用自制绘图实现viewpager无限轮播的小圆点
- Apache POI HSSF读写Excel总结
- 排序