由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Leetcode Valid Number
相关主题
valid number这道题看到有人用有限状态机做 太牛不敢看leetcode 一道题 valid palindrome
Search a 2D Matrix的两种写法,哪种更好?leetcode valid number 一问
LeetCode上word search问题的几个例子不对leetcode的valid number的考点在哪里呢?
请教一道leetcode的新题leetcode valid number
leetcode-- scramble stringfacebook电面题目
请教下leetcode Permutations IIhow to check a bin tree is balanced?
求助各位大牛:LeetCode的Decode WaysL二电面据,附面经
leetcode我这2个palindrome的为什么过不了大oj[合集] G家onsite面经
相关话题的讨论汇总
话题: stat话题: 10话题: type话题: else话题: int
进入JobHunting版参与讨论
1 (共1页)
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
2
state machine
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);

1 (共1页)
进入JobHunting版参与讨论
相关主题
[合集] G家onsite面经leetcode-- scramble string
写一个function判断一个数是不是2的整数次方请教下leetcode Permutations II
facebook的面试题求助各位大牛:LeetCode的Decode Ways
interleave string 的题目leetcode我这2个palindrome的为什么过不了大oj
valid number这道题看到有人用有限状态机做 太牛不敢看leetcode 一道题 valid palindrome
Search a 2D Matrix的两种写法,哪种更好?leetcode valid number 一问
LeetCode上word search问题的几个例子不对leetcode的valid number的考点在哪里呢?
请教一道leetcode的新题leetcode valid number
相关话题的讨论汇总
话题: stat话题: 10话题: type话题: else话题: int