mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
add code2
This commit is contained in:
parent
e9285f17ee
commit
b420ed4bae
@ -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();
|
||||
}
|
||||
};
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user