leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0065.cpp (1428B)


0 class Solution {
1 using it_t = string::const_iterator;
3 static bool validSign(it_t &pos, const it_t &end) {
4 if (*pos == '-' || *pos == '+') {
5 pos++;
6 if (find(pos, end, '-') != end || find(pos, end, '+') != end) {
7 return false;
8 }
9 }
11 return pos != end;
12 }
14 static bool validDigit(const it_t &pos, const it_t &end) {
15 return all_of(pos, end, [](char c) { return isdigit(c); });
16 }
18 static bool isDecimal(const string &s) {
19 auto pos = begin(s);
21 if (!validSign(pos, end(s))) return false;
22 if (string(pos, end(s)) == ".") return false;
24 auto dot = find(pos, end(s), '.');
25 return validDigit(pos, dot) && validDigit(dot + 1, end(s));
26 }
28 static bool isInteger(const string &s) {
29 auto pos = begin(s);
30 if (!validSign(pos, end(s))) return false;
31 return validDigit(pos, end(s));
32 }
34 public:
35 bool isNumber(const string &s) const {
36 auto e = s.find('e'), E = s.find('E');
38 if (E != string::npos) swap(e, E);
39 if (e != string::npos) {
40 const string first = s.substr(0, e);
41 const string second = s.substr(e + 1);
42 return !first.empty() && !second.empty() && isInteger(second) &&
43 (isDecimal(first) || isInteger(first));
44 }
46 return isDecimal(s) || isInteger(s);
47 }
48 };