测试用例
input  | output  | 
2e0  | T  | 
3.  | T  | 
.3  | T  | 
01  | T  | 
-1.  | T  | 
+.8  | T  | 
0e  | F  | 
尝试正则
#include <iostream>
#include <vector>
#include <algorithm>
#include <regex>
using namespace std;
class Solution {
public:
bool isNumber(string s) {
string fronte = "\\s*\\-?\\+?(([0-9]+)|(([0-9]+[.][0-9]*)|([0-9]*[.][0-9]+)))";
string ab0 = "[1-9][0-9]*";
string epart = "(e[0-9]+)?\\s*";
string ret = fronte + epart;
regex rule(ret);
cout << regex_match(s, rule) << endl; //true
return regex_match(s, rule);
}
};
int main()
{
Solution Solution1;
string s;
while(cin>>s)
Solution1.isNumber(s);
return 0;
}

枯了^.^ 居然这么多测试用例
硬讨论
class Solution {
public:
    bool isNumber(string s) {
        int len = s.size();
        int left = 0, right = len - 1;
        bool eExisted = false;
        bool dotExisted = false;
        bool digitExisited = false;
        // Delete spaces in the front and end of string
        while (s[left] == ' ') ++left;
        while (s[right] == ' ') --right;
        // If only have one char and not digit, return false
        if (left >= right && (s[left] < '0' || s[left] > '9')) return false;
        //Process the first char
        if (s[left] == '.') dotExisted = true;
        else if (s[left] >= '0' && s[left] <= '9') digitExisited = true;
        else if (s[left] != '+' && s[left] != '-') return false;
        // Process the middle chars
        for (int i = left + 1; i <= right - 1; ++i) {
            if (s[i] >= '0' && s[i] <= '9') digitExisited = true;
            else if (s[i] == 'e' || s[i] == 'E') { // e/E cannot follow +/-, must follow a digit
                if (!eExisted && s[i - 1] != '+' && s[i - 1] != '-' && digitExisited) eExisted = true;
                else return false;
            } else if (s[i] == '+' || s[i] == '-') { // +/- can only follow e/E
                if (s[i - 1] != 'e' && s[i - 1] != 'E') return false;                
            } else if (s[i] == '.') { // dot can only occur once and cannot occur after e/E
                if (!dotExisted && !eExisted) dotExisted = true;
                else return false;
            } else return false;
        }
        // Process the last char, it can only be digit or dot, when is dot, there should be no dot and e/E before and must follow a digit
        if (s[right] >= '0' && s[right] <= '9') return true;
        else if (s[right] == '.' && !dotExisted && !eExisted && digitExisited) return true;
        else return false;
    }
};
java正则 直接放在c++里面是错的
class Solution {
    public boolean isNumber(String s) {
        if(s.trim().isEmpty()){
            return false;
        }
        String regex = "[-+]?(\\d+\\.?|\\.\\d+)\\d*(e[-+]?\\d+)?";
        if(s.trim().matches(regex)){
            return true;
        }else{
            return false;
        }
    }
}
有限状态自动机
public static boolean isNumber(String s) {
    int i = 0;
    while(s.charAt(i) == ' '){  // 移除前导whitespace
      i++;
      if(i >= s.length()){
        return false;
      }
    }
    if(s.charAt(i)=='+' || s.charAt(i)=='-'){ // 忽略符号位
      i++;
    }
    int j = s.length()-1;
    while(s.charAt(j) == ' '){  // 移除后缀whitespace
      j--;
    }
    if(i <= j){
      s = s.substring(i, j+1);
    }else{
      return false;
    }
    
    int dot = -1; // 记录点的位置
    int ee = -1;  // 记录e的位置
    for(i=0; i<s.length(); i++){
      if(dot==-1 && s.charAt(i)=='.'){
        dot = i;
      }else if(ee==-1 && s.charAt(i)=='e'){
        ee = i;
        if(i+1<s.length() && (s.charAt(i+1)=='-' || s.charAt(i+1)=='+')){
          i++;
        }
      }else{
        if(Character.isDigit(s.charAt(i))){
          continue;
        }else{
          return false;
        }
      }
    }
    
    //xxx.xxexx
    String startStr, midStr, lastStr;
    if(dot==-1 && ee==-1){  //xxx  
      startStr = s; // xxx
      if(startStr.length()<1){
        return false;
      }
    }else if(dot!=-1 && ee==-1){  //xxx.yyy  
      startStr = s.substring(0, dot); // xxx
      midStr = s.substring(dot+1);    // yyy
      if(startStr.length()<1 && midStr.length()<1){
        return false;
      }
    }else if(dot==-1 && ee!=-1){  // xxxeyyy
      startStr = s.substring(0, ee);  // xxx
      if(startStr.length()<1){
        return false;
      }
      if(ee+1<s.length() && (s.charAt(ee+1)=='-' || s.charAt(ee+1)=='+')){ // xxxe-zz
        lastStr = s.substring(ee+2);  // zz
      }else{
        lastStr = s.substring(ee+1);
      }
      if(lastStr.length() < 1){
        return false;
      }
    }else{    //xxx.yyezz
      if(dot>ee){    // 位置不对
        return false;
      }
      startStr = s.substring(0, dot); // xxx
      midStr = s.substring(dot+1, ee);  // yy
      if(startStr.length()<1 && midStr.length()<1){
        return false;
      }
      if(ee+1<s.length() && (s.charAt(ee+1)=='-' || s.charAt(ee+1)=='+')){
        lastStr = s.substring(ee+2);  // zz
      }else{
        lastStr = s.substring(ee+1);
      }
      if(lastStr.length() < 1){
        return false;
      }
    }
    return true;
  }js正则匹配
https://leetcode-cn.com/problems/valid-number/comments/
var isNumber = function(s) {
    return /^\s*[+-]?((\d*\.\d+)|\d+\.?)(e[+-]?\d+)?\s*$/.test(s);
};                









