LeetCode-String to Integer (atoi)
2016-12-29 16:46
267 查看
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.
这一道题是面试常见的一道题型,并没有用到什么数据结构与算法,主要是考察对逻辑性,异常情况以及处理的。想要一次性debug free也不是那么容易的。(一个优秀的coder并不是马上写代码,而是要先进行思考,思考清晰以后再动手写代码)。
最开始分析需要注意的地方:需要处理符号位,溢出的情况,出现除符号位外的非数字字符的情况
经过debug之后发现遗漏的情况有:字符串在数字开始之前存在空格,例: - 21212;首几位数字为0,例:000222
对于不同的异常情况,题目没有给出具体的异常返回值,对atoi 函数不是太了解,所以弄的比较蛋疼
代码如下:
class Solution {
public:
int myAtoi(string str) {
int len = str.length();
long result = 0;
int start = 0;
while(str[start] == ' ')
start++;
if((str[start] >= '0' && str[start] <= '9') || str[start] == '+' )
{
if(str[start] == '+' )
start++;
while(str[start] == '0')
start++;
for(int i = start;i<len;i++)
{
if(str[i] < '0' || str[i] >'9')
break;
result *= 10;
result += str[i]-'0';
if(result > INT_MAX)
return INT_MAX;
}
}
else if(str[start] == '-')
{
while(str[start] == '0')
start++;
for(int i = start+1;i<len;i++)
{
if(str[i] < '0' || str[i] >'9')
break;
result *= 10;
result += str[i]-'0';
if(-result < INT_MIN)
return INT_MIN;
}
result = 0-result;
}
else
{
return 0;
}
return result;
}
};
看了自己的代码,发现写的跟一坨屎一样,美观和高效一个都不占 - -! 接下来对代码进行优化,并参考大神们的代码
修改后的代码:
class Solution {
public:
int myAtoi(string str) {
int len = str.length();
long result = 0;
int start = 0;
while(str[start] == ' ')
start++;
int indicator = 1;
if(str[start]=='-' || str[start]=='+')
indicator = (str[start++]=='-')?-1:1;
while(str[start] >= '0' && str[start] <= '9' && start < len)
{
result = result*10 + (str[start++] - '0');
if(result * indicator >= INT_MAX) return INT_MAX;
if(result * indicator <= INT_MIN) return INT_MIN;
}
return result*indicator;
}
};
效率并没有得到提升,很好奇效率排最前面的那些人是怎么做到的。留待以后研究。
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.
这一道题是面试常见的一道题型,并没有用到什么数据结构与算法,主要是考察对逻辑性,异常情况以及处理的。想要一次性debug free也不是那么容易的。(一个优秀的coder并不是马上写代码,而是要先进行思考,思考清晰以后再动手写代码)。
最开始分析需要注意的地方:需要处理符号位,溢出的情况,出现除符号位外的非数字字符的情况
经过debug之后发现遗漏的情况有:字符串在数字开始之前存在空格,例: - 21212;首几位数字为0,例:000222
对于不同的异常情况,题目没有给出具体的异常返回值,对atoi 函数不是太了解,所以弄的比较蛋疼
代码如下:
class Solution {
public:
int myAtoi(string str) {
int len = str.length();
long result = 0;
int start = 0;
while(str[start] == ' ')
start++;
if((str[start] >= '0' && str[start] <= '9') || str[start] == '+' )
{
if(str[start] == '+' )
start++;
while(str[start] == '0')
start++;
for(int i = start;i<len;i++)
{
if(str[i] < '0' || str[i] >'9')
break;
result *= 10;
result += str[i]-'0';
if(result > INT_MAX)
return INT_MAX;
}
}
else if(str[start] == '-')
{
while(str[start] == '0')
start++;
for(int i = start+1;i<len;i++)
{
if(str[i] < '0' || str[i] >'9')
break;
result *= 10;
result += str[i]-'0';
if(-result < INT_MIN)
return INT_MIN;
}
result = 0-result;
}
else
{
return 0;
}
return result;
}
};
看了自己的代码,发现写的跟一坨屎一样,美观和高效一个都不占 - -! 接下来对代码进行优化,并参考大神们的代码
修改后的代码:
class Solution {
public:
int myAtoi(string str) {
int len = str.length();
long result = 0;
int start = 0;
while(str[start] == ' ')
start++;
int indicator = 1;
if(str[start]=='-' || str[start]=='+')
indicator = (str[start++]=='-')?-1:1;
while(str[start] >= '0' && str[start] <= '9' && start < len)
{
result = result*10 + (str[start++] - '0');
if(result * indicator >= INT_MAX) return INT_MAX;
if(result * indicator <= INT_MIN) return INT_MIN;
}
return result*indicator;
}
};
效率并没有得到提升,很好奇效率排最前面的那些人是怎么做到的。留待以后研究。
相关文章推荐
- leetcode 54: String to Integer (atoi)
- Leetcode : String to Integer (atoi)
- LeetCode-String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- leetcode -- String to Integer (atoi)
- leetcode 54: String to Integer (atoi)
- [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)
- LeetCode - String to Integer (atoi)
- leetcode String to Integer (atoi)Dec 27 '117626 / 35090
- [LeetCode] String to Integer (atoi) 解题报告
- LeetCode 8 - String to Integer (atoi)
- 【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)