add code2

This commit is contained in:
ShusenTang 2020-06-22 17:03:25 +08:00 committed by GitHub
parent e9285f17ee
commit b420ed4bae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -63,3 +63,48 @@ public:
}
};
```
### 写法二
思路一样只是分别检查ABC是否为空AB不能同时为空。
``` C++
class Solution {
private:
bool check_unsign_empty(string &s){
int i = 0;
while(i < s.size() && s[i] >= '0' && s[i] <= '9') i++;
if(i > 0) s = s.substr(i);
return i == 0;
}
bool check_sign_empty(string &s){
if(!s.empty() && s[0] == '-' || s[0] == '+') s = s.substr(1);
return check_unsign_empty(s);
}
public:
bool isNumber(string s) { // A.BeC
// 去掉首尾的空格
int l = 0, r = s.size() - 1;
while(s[l] == ' ') l++;
while(l < r && s[r] == ' ') r--;
s = s.substr(l, r - l + 1);
bool A_is_empty = true, B_is_empty = true, C_is_empty = true;
A_is_empty = check_sign_empty(s);
if(!s.empty() && s[0] == '.'){
s = s.substr(1);
B_is_empty = check_unsign_empty(s);
}
if(A_is_empty && B_is_empty) return false; // AB不能同时为空
if(!s.empty() && s[0] == 'e'){
s = s.substr(1);
C_is_empty = check_sign_empty(s);
if(C_is_empty) return false;
}
return s.empty();
}
};
```