Leetcode/cpp/65.cpp
2024-06-21 01:36:27 +01:00

85 lines
1.6 KiB
C++

#include <string>
#include <vector>
class Solution {
private:
int i = 0;
std::string s;
std::vector<char> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
bool atEnd() {
return this->i == s.length();
}
bool test(char c) {
if (atEnd()) {
return false;
}
return s.at(i) == c;
}
bool test(std::vector<char> chars) {
for (char c : chars) {
if (test(c)) {
return true;
}
}
return false;
}
bool match(char c) {
if (test(c)) {
i++;
return true;
}
return false;
}
bool match(std::vector<char> chars) {
if (test(chars)) {
i++;
return true;
}
return false;
}
bool many(std::vector<char> chars) {
bool matched = false;
while (match(chars)) {
matched = true;
}
return matched;
}
bool digitString() {
return many(digits);
}
bool integer() {
match({'+', '-'});
return digitString();
}
bool decimal() {
if (!integer()) {
return match('.') && digitString();
} else {
match('.');
digitString();
return true;
}
}
bool exponent() {
if (match({'e', 'E'})) {
return integer();
}
return true;
}
bool number() {
bool success = decimal();
return success && exponent();
}
public:
bool isNumber(std::string s) {
this->s = s;
return number() && this->atEnd();
}
};