65. 有效数字
65. 有效数字
题目链接:65. 有效数字
代码如下:
//参考链接:https://leetcode.cn/problems/valid-number/solutions/831848/gong-shui-san-xie-zi-fu-chuan-mo-ni-by-a-7cgc
class Solution
{
public:bool isNumber(string s){int idx = -1;for (int i = 0; i < s.size(); i++){if (s[i] == 'e' || s[i] == 'E'){if (idx == -1) { idx = i; }else{return false;}}}bool res = true;if (idx != -1){res &= check(s, 0, idx - 1, false);res &= check(s, idx + 1, s.size() - 1, true);}else{res &= check(s, 0, s.size() - 1, false);}return res;}private:bool check(const string& s, int start, int end, bool mustInteger){if (start > end) { return false; }if (s[start] == '+' || s[start] == '-') { start++; }bool hasDot = false, hasNum = false;for (int i = start; i <= end; i++){if (s[i] == '.'){if (mustInteger || hasDot) { return false; }{hasDot=true;}}else if (s[i] >= '0' && s[i] <= '9') { hasNum = true; }else { return false; }}return hasNum;}
};