leetcode - String to Integer (atoi)
2015-08-16 20:42
211 查看
leetcode - 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
spoilers alert... click to show requirements for atoi.
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.
注意几个功能:正负号,溢出处理,以及开头的空格。
尤其是溢出处理。
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.
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 myAtoi(string str) { long long int res = 0; int sig = 1; bool flag = false, flag_num = false; //sign int max = 0x7fffffff; int min = 0x80000000; string::iterator it = str.begin(); while(it!=str.end()){ if(!flag_num && !flag && *it == ' '){ it++; } else if(*it >= '0' && *it <= '9'){ res = res*10 + sig * (*it - '0'); it++; flag_num = true; if (res > max || res < min) //溢出处理 return res > 0 ? max : min; } else if(!flag && *it == '+'){ sig = 1; flag = true; it++; } else if(!flag && *it == '-'){ sig = -1; flag = true; it++; } else break; } return res; } };
注意几个功能:正负号,溢出处理,以及开头的空格。
尤其是溢出处理。
相关文章推荐
- 动态规划——B 最大高度问题
- 贪心+模拟 ZOJ 3829 Known Notation
- 剑指offer 算法(栈和队列 查找和排序)
- oracle10g安装时报"无法确定主机的IP地址时产生异常错误"
- 【LeetCode]1.Two Sum
- iOS UI 视图移动及缩放
- SQL中常用的数据类型及简介
- HW--自守数
- linux下进程的最大线程数、进程最大数、进程打开的文件数
- 股市风暴下的雪球架构改造经验分享
- 二分搜索
- Mac下解决Android studio真机调试时无法识别魅蓝手机问题
- HDU 2256 & HDU 4565 (矩阵快速幂 + 公式推演)
- 【您的凭证不工作】win7远程桌面提示,请输入新凭据。登录没有成功
- 实模式和保护模式
- NOIP2010 关押罪犯(图论+二分)
- 《C算法》读书笔记 (4):Hello,Joseph!
- JavaScript编程--任意输入一个数判断是不是闰年,数列求和练习
- CodeForces-448C Painting Fence
- 黑马程序员——多线程(上)——第11天