- 思路
以e或者E为分界,左边必须是整数或者float但是右边必须是整数 - 代码
class Solution {
public:
bool check(const string &s, int start, int end, bool must_int) {
if (start > end) return false;
if(s[start] == '+' || s[start] == '-') start++;
bool has_point = false, has_num = false;
for(int i = start; i <= end; i++) {
if(s[i] == '.') {
if(must_int || has_point) {
return false;
}
has_point = true;
} else if (s[i] >= '0' && s[i] <= '9') {
has_num = true;
} else {
return false;
}
}
return (has_point && has_num) || (!has_point && has_num);
}
bool isNumber(string s) {
int len = s.length();
int i = 0;
for(; i < len; i++) {
if(s[i] == 'e' || s[i] == 'E') {
break;
}
}
if (i == len) {
return check(s, 0, len - 1, false);
}
return check(s, 0, i - 1, false) && check(s, i + 1, len - 1, true);
}
};