#include<iostream>
using namespace std;
class Solution {
public:
bool checkInt(char **s) {
if (**s == '-' || **s == '+') (*s)++;
char *p = *s;
while (*s!=NULL&&**s>='0'&& **s <= '9'){
(*s)++;
}
return *s > p;
}
bool checkUnsignedInt(char **s) {
char *p = *s;
while (*s != NULL&&**s >= '0'&& **s <= '9') {
(*s)++;
}
return *s > p;
}
bool isNumber(char *s) {
if (s == NULL) return false;
bool temp = false;
temp = checkInt(&s);
if (*s == '.') {
s++;
temp = temp || checkUnsignedInt(&s);
}
if (*s == 'E' || *s == 'e') {
s++;
temp = checkInt(&s)&&temp;
}
return temp&&*s=='\0';
}
};
int main()
{
Solution s;
cout << (s.isNumber("-1e-16") == true) << endl;
cout << (s.isNumber("-100") == true) << endl;
cout << (s.isNumber(".122e") == true) << endl;
cout << (s.isNumber("1.2.3") == true) << endl;
return 0;
}
剑指offer20
最新推荐文章于 2023-05-01 01:48:20 发布