diff --git a/solutions/65. Valid Number.md b/solutions/65. Valid Number.md index f6bcfb7..74d2d17 100644 --- a/solutions/65. Valid Number.md +++ b/solutions/65. Valid Number.md @@ -62,4 +62,49 @@ public: return Scan_Signed(s) && s.empty(); } }; -``` \ No newline at end of file +``` +### 写法二 +思路一样,只是分别检查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(); + } +}; +```