String to Integer (atoi)
来源:互联网 发布:域名icp备案 编辑:程序博客网 时间:2024/04/27 14:47
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.
需要考虑的情况比较多,但是有几个要记得:
一定要考虑是否溢出;
一定要考虑前面是否有+,-号;
一定要考虑前面是否有无效字符;
中间和最后是否有无效字符。
+ , - 号后面还有无效字符的一律为0.(这点我吃亏了不少。。。)
class Solution {public: int atoi(const char *str) { int len = strlen(str); //cout<<len<<endl; int i=0; if(*str == '\0'){ // empty return 0; }while(i<len && str[i] == ' ') // !((str[i]=='-' && str[i+1]>='0' && str[i]<='9') || (str[i]>='0' && str[i]<='9')){i++;}//cout<<"i: "<<i<<endl; if(i==len){ // all are blank return 0; } else{ bool flag=true; if(str[i] == '-' && (str[i+1]>='0' && str[i+1]<='9')){ //negative flag=false; i++; }else if(str[i] == '+' && (str[i+1]>='0' && str[i+1]<='9')){flag=true; i++; }else if ((str[i]>='0' && str[i]<='9')){}else {return 0; } //positive string s=""; while(i < len){ if(str[i]>='0' && str[i]<='9'){ s += str[i]; //contecate }else{break; }i++; }//cout<<s<<endl; string max="2147483647"; string min="2147483648"; if(flag && (s.length() > 10 || (s.length() == 10 && s>max))){ return INT_MAX; } if(!flag && (s.length() > 10 || (s.length() == 10 && s>min))){ return INT_MIN; } int result=0; for(i = 0; i < s.length(); i++){ result = result*10 + s[i]-'0'; }if(!flag){result = -1*result; } return result; } }};
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- string to integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer(atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- listview 去掉系统自带点击时候的颜色
- 【将金令】1.21早评:欧QE独占鳌头,金银高位不休
- VS 2012开始第一个编程项目
- Eclipse 报错:Error: Program "sh" not found in PATH
- *** non-numeric second argument to `wordlist' function: ''. Stop错误解决办法
- String to Integer (atoi)
- Ural_Dynamic Programming Problems_1225-Flags
- 寻找和为定值的多个数
- 黑马程序员-C语言之全局变量和局部变量
- Letter Combinations of a Phone Number
- Minimum Depth of Binary Tree
- db2 delete 大表不写日志方法
- Maximum Depth of Binary Tree
- 03-Swift 字符串