Problem:
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.
Requirements for atoi:
The function first discards as your 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 your numerical digits as possible, and interprets them as a numerical value.
The string can contain in 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 saved med.
If no valid conversion cocould be specified med, 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.
Haha, this question has not been referenced by other people's code. I have finished reading the above questions and finally passed the modification seven times.
I write according to requirements for atoi,
Write a subfunction to return the correct numeric string.
First, read the leading space. If the first non-space character is not +-or a number, return an empty string. If it is a + or-number, it must be followed by a number; otherwise, an empty character is returned. If none of the above is returned, it is a number. Read the non-numeric part directly, and then return the string with the plus or minus sign and number.
Then judge the returned string in atoi.
I define the long type for storage, and convert it to the long type by string (using stringstream to convert it by including the sstream header file ), then judge the ratio of long type to int_max or int_min under positive and negative conditions. Note that if the boundary is incorrect, many cases cannot pass, I switched to the long type only when I first found that I could not use the long type.
In addition, at the beginning, I did not know that it was necessary to set the beginning. If the number is in the middle, other characters before and after will not be counted. For example, although "ABC123" has 123 characters, a number cannot be returned because it does not meet the requirements. Therefore, 0 is returned directly.
class Solution {public:int atoi(const char *str){ string s = substringInteger(str); if (s.size() == 0) return 0; int positive = 1; if (s[0] == ‘+‘) s = s.substr(1,s.size() - 1); else if(s[0] == ‘-‘) { s = s.substr(1,s.size() - 1); positive = 0; } std::stringstream ss; ss<<s; long long result; ss>>result; if (positive == 0) { long long maxval = 2147483648; // 这里直接用数字是因为我用 INT_MAX + 1 的时候显示的是 INT_MIN的值, -1的case会出错 if (result < maxval) return -1*result; else return INT_MIN; } else { if (result < INT_MAX) return result; else return INT_MAX; }}private://leetcode7 return the sub string include intergerstring substringInteger(string s){ if (s.size() == 0) return ""; string substring; int pos = -1, len = 1; int index = 0; int positive = 1; while(s[index] == ‘ ‘ && index < s.size()) { index++; } if (index == s.size()) return ""; if(s[index]!=‘+‘ && s[index]!=‘-‘ && !isdigit(s[index])) return ""; if(s[index] == ‘+‘&&index+1<s.size()) if(!isdigit(s[index+1])) return ""; if(s[index] == ‘-‘&&index+1<s.size()) { if(!isdigit(s[index+1])) return ""; else positive = 0; } // detect till digit if exist while(!isdigit(s[index])&&index<s.size()) { index++; } if(index == s.size()) return ""; // find the length of the digit pos = index; while(isdigit(s[index])&&index<s.size()) { index++; } len = index - pos; substring = s.substr(pos,len); // if negative return ‘-‘ in front if (positive == 0) substring = "-" + substring; return substring;}};
In this coding, I reviewed and learned substr. When stringstream in sstream converts string and any type into int_max and int_min special boundary processing, note that there is an ending number of 8, also, pay attention to Chinese comments in the code.
Question 8 of leetcode -- string to INTEGER (atoi)