参考剑指offer
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str string字符串 * @return bool布尔型 */ bool scanUnsignedInteger(string str, int &index){ int before = index; while(str[index] != '\0' && str[index] >= '0' && str[index] <= '9') index++; return index > before; } bool scanInteger(string str, int &index) { if(str[index] == '+' || str[index] == '-'){ index++; } return scanUnsignedInteger(str, index); } bool isNumeric(string str) { // write code here int str_len = str.length(); if(str_len <= 0) return false; int index = 0; bool numeric = scanInteger(str, index); if(str[index] == '.'){ ++index; numeric = scanUnsignedInteger(str, index) || numeric; } if(str[index] == 'e' || str[index] == 'E'){ ++index; numeric = numeric && scanInteger(str, index); } return numeric&&str[index] == '\0'; } };