b**********y 发帖数: 24 | 1 一直不明白这个solution的transition matrix 是怎么来的
bool isNumber(const char *s) {
int mat[11][7] = {0 ,0 ,0 ,0 ,0 ,0 ,0, // false
0 ,2 ,3 ,0 ,1 ,4 ,0, // 1
0 ,2 ,5 ,6 ,9 ,0 ,10,// 2
0 ,5 ,0 ,0 ,0 ,0 ,0, // 3
0 ,2 ,3 ,0 ,0 ,0 ,0, // 4
0 ,5 ,0 ,6 ,9 ,0 ,10,// 5
0 ,7 ,0 ,0 ,0 ,8 ,0, // 6
0 ,7 ,0 ,0 ,9 ,0 ,10,// 7
0 ,7 ,0 ,0 ,0 ,0 ,0, // 8
0 ,0 ,0 ,0 ,9 ,0 ,10,// 9
10,10,10,10,10,10,10 // 10
};
int i = 0;
int stat = 1;
while(s[i] != 0) {
int type = 0;
if(s[i] >= '0' && s[i] <= '9')
type = 1;
else if(s[i] == '.')
type = 2;
else if(s[i] == 'e')
type = 3;
else if(s[i] == ' ')
type = 4;
else if(s[i] == '+' || s[i] == '-')
type = 5;
if(stat == 0)
return false;
stat = mat[stat][type];
i++;
}
stat = mat[stat][6];
if(stat == 10)
return true;
else
return false;
} | p*****e 发帖数: 537 | | s**x 发帖数: 7506 | | s**x 发帖数: 7506 | 4 我的想法, 极其简单, I could not find anything wrong, I found most of the
solutions are difficult to write and easy to make mistakes.
you can finish the following in less than 5 minutes and most importantly
hard to make mistakes.
bool isValide(char *string)
{
char *p = string;
if (p == NULL) return false;
p += skipWhiteSpaces(p);
p += skipSigns(p);
int n1 = skipDigits(p);
p+= n1;
if (*p == '.') p++;
int n2 = skipDigits(p);
if (n1 == 0 && n2 == 0) return false;
if (*p == 'e' || *p == 'E')
{
p++;
p += skipSigns(p);
int n3 = skipDigits(p);
if (n3 == 0) return false;
}
p += skipWhiteSpaces(p);
return *p == '\0';
}
those helper functions are easy to understand and write. | w*******e 发帖数: 395 | 5 稍微更正你的代码几个简单错误之后,你的代码是我见过此题最简洁的,而且通过了
judge
多谢了
【在 s**x 的大作中提到】 : 我的想法, 极其简单, I could not find anything wrong, I found most of the : solutions are difficult to write and easy to make mistakes. : you can finish the following in less than 5 minutes and most importantly : hard to make mistakes. : bool isValide(char *string) : { : char *p = string; : if (p == NULL) return false; : p += skipWhiteSpaces(p); : p += skipSigns(p);
| s**x 发帖数: 7506 | 6 多谢验证,俺自己还真没用过online judge.
【在 w*******e 的大作中提到】 : 稍微更正你的代码几个简单错误之后,你的代码是我见过此题最简洁的,而且通过了 : judge : 多谢了
| l*******b 发帖数: 2586 | 7 悲剧呀,学state machine写的,又被拿来当反面教材了。。。
【在 b**********y 的大作中提到】 : 一直不明白这个solution的transition matrix 是怎么来的 : bool isNumber(const char *s) { : int mat[11][7] = {0 ,0 ,0 ,0 ,0 ,0 ,0, // false : 0 ,2 ,3 ,0 ,1 ,4 ,0, // 1 : 0 ,2 ,5 ,6 ,9 ,0 ,10,// 2 : 0 ,5 ,0 ,0 ,0 ,0 ,0, // 3 : 0 ,2 ,3 ,0 ,0 ,0 ,0, // 4 : 0 ,5 ,0 ,6 ,9 ,0 ,10,// 5 : 0 ,7 ,0 ,0 ,0 ,8 ,0, // 6 : 0 ,7 ,0 ,0 ,9 ,0 ,10,// 7
| f********x 发帖数: 2086 | 8
赞
【在 s**x 的大作中提到】 : 我的想法, 极其简单, I could not find anything wrong, I found most of the : solutions are difficult to write and easy to make mistakes. : you can finish the following in less than 5 minutes and most importantly : hard to make mistakes. : bool isValide(char *string) : { : char *p = string; : if (p == NULL) return false; : p += skipWhiteSpaces(p); : p += skipSigns(p);
|
|