Leetcode做到Valid Number这道题, 看到 Leetcode题目分类 中说这道题难度为二级(总共五级), 还以为这道题真的很简单。。。真是坑了大爹了。
仔细总结了下, 这道题大概有三种解法:
Validate if a given string is numeric.
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
Note: It is intended for the problem statement to be ambiguous.
You should gather all requirements up front before implementing one.
class Solution2
{
public:
bool isNumber(const char *s)
{
regex re("( *)[+-]?(\\d+|\\d+\\.\\d*|\\d*\\.\\d+)([eE][+-]?\\d+)?");
if (regex_match(s, re))
return true;
else
return false;
}
};
代码如下:
class Solution2_1
{
public:
public:
bool isNumber(const char *s)
{
int state = 0;
while (1)
{
switch (state)
{
case 0:
if (*s == ' ')
state = 0;
else if (*s == '+' || *s == '-')
state = 1;
else if (isdigit(*s))
state = 2;
else if (*s == '.')
state = 9;
else
state = 8;
break;
case 1:
if (isdigit(*s))
state = 2;
else if (*s == '.')
state = 9;
else
state = 8;
break;
case 2:
if (isdigit(*s))
state = 2;
else if (*s == '.')
state = 3;
else if (*s == '\0')
state = 7;
else if (*s == ' ')
state = 10;
else if (*s == 'e' || *s == 'E')
state = 5;
else
state = 8;
break;
case 3:
if (isdigit(*s))
state = 4;
else if (*s == '\0')
state = 7;
else if (*s == ' ')
state = 10;
else if (*s == 'e' || *s == 'E')
state = 5;
else
state = 8;
break;
case 4:
if (isdigit(*s))
state = 4;
else if (*s == 'e' || *s == 'E')
state = 5;
else if (*s == '\0')
state = 7;
else if (*s == ' ')
state = 10;
else
state = 8;
break;
case 5:
if (isdigit(*s))
state = 6;
else if (*s == '+' || *s == '-')
state = 11;
else
state = 8;
break;
case 6:
if (isdigit(*s))
state = 6;
else if (*s == ' ')
state = 6;
else if (*s == '\0')
state = 7;
else if (*s == ' ')
state = 10;
else
state = 8;
break;
case 7:
return true;
case 8:
return false;
case 9:
if (isdigit(*s))
state = 4;
else
state = 8;
break;
case 10:
if (*s == '\0')
state = 7;
else if (*s == ' ')
state = 10;
else
state = 8;
break;
case 11:
if (isdigit(*s))
state = 6;
else
state = 8;
break;
}
s++;
}
}
};
这段代码可能开起来有点长, 不过只要弄明白了那张状态转换图很容易明白的。
上面这段代码可能有点长, 所以用二维数组对其进行改进:
代码如下:
class Solution2_2
{
public:
bool isNumber(const char *s)
{
int state = 0;
int translate[][7] =
{
0, 1, 2, 8, 9, 8, 8, //0
8, 8, 2, 8, 9, 8, 8, //1
10, 8, 2, 5, 3, 7, 8, //2
10, 8, 4, 5, 8, 7, 8, //3
10, 8, 4, 5, 8, 7, 8, //4
8, 11, 6, 8, 8, 8, 8, //5
10, 8, 6, 8, 8, 7, 8, //6
7, 7, 7, 7, 7, 7, 7, //7
8, 8, 8, 8, 8, 8, 8, //8
8, 8, 4, 8, 8, 8, 8, //9
10, 8, 8, 8, 8, 7, 8, //10
8, 8, 6, 8, 8, 8, 8 //11
};
int type;
while (1)
{
if (*s == ' ')
type = 0;
else if (*s == '+' || *s == '-')
type = 1;
else if (isdigit(*s))
type = 2;
else if (*s == 'e' || *s == 'E')
type = 3;
else if (*s == '.')
type = 4;
else if (*s == '\0')
type = 5;
else
type = 6;
state = translate[state][type];
if (state == 7)
return true;
else if (state == 8)
return false;
s++;
}
}
};
class Solution3 {
public:
bool isNumber(const char *s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (s == NULL)
return false;
while(isspace(*s))
s++;
if (*s == '+' || *s == '-')
s++;
bool eAppear = false;
bool dotAppear = false;
bool firstPart = false;
bool secondPart = false;
bool spaceAppear = false;
while(*s != '\0')
{
if (*s == '.')
{
if (dotAppear || eAppear || spaceAppear)
return false;
else
dotAppear = true;
}
else if (*s == 'e' || *s == 'E')
{
if (eAppear || !firstPart || spaceAppear)
return false;
else
eAppear = true;
}
else if (isdigit(*s))
{
if (spaceAppear)
return false;
if (!eAppear)
firstPart = true;
else
secondPart = true;
}
else if (*s == '+' || *s == '-') // behind of e/E
{
if (spaceAppear)
return false;
if (!eAppear || !(*(s-1) == 'e' || *(s-1) == 'E'))
return false;
}
else if (isspace(*s))
spaceAppear = true;
else
return false;
s++;
}
if (!firstPart) {
return false;
} else if (eAppear && !secondPart) {
return false;
}
return true;
}
};