这个代码会报错
class Solution {
public:
enum status {kvalid = ,kinvalid};
int now_status = kvalid;
int StrToInt(string str) {
now_status = kinvalid;
long long num = ;
int index = ;
bool minus = false;
if(str.size() != && str[index] != '\0'){
if(str[index] == '+'){
index++;
}
else if(str[index] == '-'){
minus = true;
index++;
}
if(str[index] != '\0'){
num = StrToIntCore(str,minus,index);
}
}
return int(num);
}
long long StrToIntCore(string str,bool minus,int index){
int length = str.size();
long long num = ;
int flag = minus ? - : ;
for(;index < length;index++){
if(str[index] >= '' && str[index] <= ''){
num = num* + flag * (str[index]-'');if(( num < 0x80000000) || ( num > 0x7FFFFFFF)){
num = ;
break;
}
}
else{
num = ;
break;
}
}
if(str[index] == '\0'){
now_status = kvalid;
}
return num;
}
};
修改为:
class Solution {
public:
enum status {kvalid = ,kinvalid};
int now_status = kvalid;
int StrToInt(string str) {
now_status = kinvalid;
long long num = ;
int index = ;
bool minus = false;
if(str.size() != && str[index] != '\0'){
if(str[index] == '+'){
index++;
}
else if(str[index] == '-'){
minus = true;
index++;
}
if(str[index] != '\0'){
num = StrToIntCore(str,minus,index);
}
}
return int(num);
}
long long StrToIntCore(string str,bool minus,int index){
int length = str.size();
long long num = ;
int flag = minus ? - : ;
for(;index < length;index++){
if(str[index] >= '' && str[index] <= ''){
num = num* + flag * (str[index]-'');
int xxxx= 0x80000000;
if(( num < xxxx) || ( num > 0x7FFFFFFF)){
num = ;
break;
}
}
else{
num = ;
break;
}
}
if(str[index] == '\0'){
now_status = kvalid;
}
return num;
}
};
或者修改为
class Solution {
public:
enum status {kvalid = ,kinvalid};
int now_status = kvalid;
int StrToInt(string str) {
now_status = kinvalid;
long long num = ;
int index = ;
bool minus = false;
if(str.size() != && str[index] != '\0'){
if(str[index] == '+'){
index++;
}
else if(str[index] == '-'){
minus = true;
index++;
}
if(str[index] != '\0'){
num = StrToIntCore(str,minus,index);
}
}
return int(num);
}
long long StrToIntCore(string str,bool minus,int index){
int length = str.size();
long long num = ;
int flag = minus ? - : ;
for(;index < length;index++){
if(str[index] >= '' && str[index] <= ''){
num = num* + flag * (str[index]-'');
if(( num < (signed int)0x80000000) || ( num > 0x7FFFFFFF)){
num = ;
break;
}
}
else{
num = ;
break;
}
}
if(str[index] == '\0'){
now_status = kvalid;
}
return num;
}
};