Redian新闻
>
好虫不要吵架了 win10 10547
avatar
好虫不要吵架了 win10 10547# PDA - 掌中宝
s*x
1
说一下思路吧,懒得写代码
1 skip white spaces
2 skip + or - if there is one.
3 skip all numeric digits, say n1 digits are skipped.
4 skip . if there is one
5 same as step 3, but save count to n2.
6 here is the key part, return false if n1 + n2 == 0.
7. Skip E or e if there is one.
8 if there is e or E, do step 2 and 3, return false if n1 == 0
9 skip white space
10 return false if string is empty now, otherwise return true.
Simple, fast and hard to make mistakes.
avatar
f*w
2
买gc算不算啊
avatar
p*w
3
H2O:
15% off every h2o plus purchase all January
code: 110011
Lancome:
receive a gorgeous bouquet of 5 FREE lipsticks (a $124 product value) with
your next $35 purchase
Offer expires January 26, 2011 11:59AM EST.
Offer valid while supplies last, one-time purchase only,
not valid in combination with other sample offers.
兰蔻的只能用一次,想要的mm给我发站内短信吧。26号过期,前几天忘记贴出来了。
avatar
p*m
4
netflix app不工作了 派人解决一下吧
[发表自未名空间手机版 - m.mitbbs.com]
avatar
s*x
5
有人找到了代码
bool isNumber(const char *s)
{
if (!s) return false;
s = skipSpace(s);
if (*s == '+' || *s == '-')
{
++s;
}
int n1 = getNumDigits(s);
s += n1;
if (*s == '.')
{
++s;
}
int n2 = getNumDigits(s);
if (n1 + n2 == 0) return false;
s += n2;
if (*s == 'E' || *s == 'e')
{
++s;
if (*s == '+' || *s == '-')
{
++s;
}
int n3 = getNumDigits(s);
if (n2 == 0) return false;
s += n3;
}
s = skipSpace(s);
return *s == '\0';
}
avatar
s*2
6
which store?
avatar
s*m
7
必然是微软的问题,好虫是冤枉的

【在 p*******m 的大作中提到】
: netflix app不工作了 派人解决一下吧
: [发表自未名空间手机版 - m.mitbbs.com]

avatar
y*2
8
我以前看过的最好的解法是那个用finite state machine做的。
我看看你的解法
avatar
g*n
9
杂货店都行
walmart,中国店也没问题

【在 s******2 的大作中提到】
: which store?
avatar
g*g
10
没人用的平台,不修也罢。

【在 p*******m 的大作中提到】
: netflix app不工作了 派人解决一下吧
: [发表自未名空间手机版 - m.mitbbs.com]

avatar
y*n
11
真心说,面试想清楚10步不容易。

【在 s**x 的大作中提到】
: 说一下思路吧,懒得写代码
: 1 skip white spaces
: 2 skip + or - if there is one.
: 3 skip all numeric digits, say n1 digits are skipped.
: 4 skip . if there is one
: 5 same as step 3, but save count to n2.
: 6 here is the key part, return false if n1 + n2 == 0.
: 7. Skip E or e if there is one.
: 8 if there is e or E, do step 2 and 3, return false if n1 == 0
: 9 skip white space

avatar
f*w
12
没有说呀
之说instore puchase呀
啤酒rebate

【在 s******2 的大作中提到】
: which store?
avatar
r*g
13
赚一分是一分。你这样子老板会开掉你吧?

【在 g*****g 的大作中提到】
: 没人用的平台,不修也罢。
avatar
s*x
14
sigh, 唯一的 trick 就是第六步。 写个浮点数 -123.E+5 , 一清二楚阿。
这个是最直接明了的土办法了。 我是慢慢琢磨出来的, 老觉得网上给出的算法都很坑
爹。 面试高度紧张, 好几个变量直接歇菜。

【在 y***n 的大作中提到】
: 真心说,面试想清楚10步不容易。
avatar
g*n
15
有的form上写了不包括GC,那就不行
没写的,应该没问题吧
不过,够100应该很容易啊
一个月的收据怎么也够了吧

【在 f*******w 的大作中提到】
: 买gc算不算啊
avatar
p*m
16

我软药三分天下的啊

【在 g*****g 的大作中提到】
: 没人用的平台,不修也罢。
avatar
s*x
17
那个当然是最优的, 可以提一下加分。

【在 y****2 的大作中提到】
: 我以前看过的最好的解法是那个用finite state machine做的。
: 我看看你的解法

avatar
f*w
18
还真难啊,因为很多都用了胖子啊

【在 g**n 的大作中提到】
: 有的form上写了不包括GC,那就不行
: 没写的,应该没问题吧
: 不过,够100应该很容易啊
: 一个月的收据怎么也够了吧

avatar
r*g
19
Pc上一统天下

【在 p*******m 的大作中提到】
:
: 我软药三分天下的啊

avatar
y*n
20
你误会了,你的解法很好,我是说看到一个新的题,要想到10步不容易。

【在 s**x 的大作中提到】
: sigh, 唯一的 trick 就是第六步。 写个浮点数 -123.E+5 , 一清二楚阿。
: 这个是最直接明了的土办法了。 我是慢慢琢磨出来的, 老觉得网上给出的算法都很坑
: 爹。 面试高度紧张, 好几个变量直接歇菜。

avatar
c*y
21
上次的还是最近的?
上次的 有人costco的没qualify 我staples+RA GC的拿到了。。。。

【在 f*******w 的大作中提到】
: 买gc算不算啊
avatar
p*m
22

必须的把猪猪86扼杀在摇篮里

【在 r**********g 的大作中提到】
: Pc上一统天下
avatar
s*r
23
还是有corner case没有handle,比如多个.的情况

【在 s**x 的大作中提到】
: 说一下思路吧,懒得写代码
: 1 skip white spaces
: 2 skip + or - if there is one.
: 3 skip all numeric digits, say n1 digits are skipped.
: 4 skip . if there is one
: 5 same as step 3, but save count to n2.
: 6 here is the key part, return false if n1 + n2 == 0.
: 7. Skip E or e if there is one.
: 8 if there is e or E, do step 2 and 3, return false if n1 == 0
: 9 skip white space

avatar
f*w
24
只限我们州的
coors的
avatar
r*g
25
这个没戏。要能搞死,果早搞死了,还轮到犬?

【在 p*******m 的大作中提到】
:
: 必须的把猪猪86扼杀在摇篮里

avatar
s*r
26
如果真写成这样可以直接踢出去了
如果s里面有非数字的字符,getNumDigits()应该会返回何值,程序里面没有检测这种
返回值。getNumDigits()应该遇到什么情况返回,貌似条件是遇到.或者大小写e,但问
题来了,如果s里面有两个.,比如1.1.1,这个程序也会返回true
这个问题其实有点烦琐,因为需要一些context info

【在 s**x 的大作中提到】
: 有人找到了代码
: bool isNumber(const char *s)
: {
: if (!s) return false;
: s = skipSpace(s);
: if (*s == '+' || *s == '-')
: {
: ++s;
: }
: int n1 = getNumDigits(s);

avatar
i*4
27
难的不是一点点,最后用了鞋店的

【在 g**n 的大作中提到】
: 有的form上写了不包括GC,那就不行
: 没写的,应该没问题吧
: 不过,够100应该很容易啊
: 一个月的收据怎么也够了吧

avatar
p*m
28
修好了
avatar
m*k
29
it works,
1.1.1
会return '.'=='\0'
avatar
y*u
30
我在MD,有这个活动吗?WM买满100是COUPON后还是前?REBATE在哪儿找?求LINK PLZ
avatar
m*s
31
不用谢

【在 p*******m 的大作中提到】
: 修好了
avatar
z*c
32
我觉得这个解法很好,容易理解也容易写出来。为什么会被踢
avatar
a*a
33
原来netflix的高人在这儿啊。有个问题,lumia 635上win8系统语言是英文时netflix
能工作,改成中文就根本启动不了了,能帮着看看吗?

【在 p*******m 的大作中提到】
: netflix app不工作了 派人解决一下吧
: [发表自未名空间手机版 - m.mitbbs.com]

avatar
b*c
34
好思路,就是程序里if (n2 == 0) return false;那里 是不是应该是n3
附上我按照lz思路写的,leetcode OJ 136ms 通过
bool isNumber(const char *s) {
if (!s) return false;
while (*s == ' ') s++;
if (*s == '+' || *s == '-') s++;
int n1 = 0;
while (isdigit(*s)) {
n1++;
s++;
}
if (*s == '.') s++;
int n2 = 0;
while (isdigit(*s)) {
n2++;
s++;
}
if (n1 + n2 == 0) return false;
if (*s == 'e' || *s == 'E') {
s++;
if (*s == '+' || *s == '-') s++;
int n3 = 0;
while (isdigit(*s)) {
n3++;
s++;
}
if(n3 == 0) return false;
}
while (*s == ' ') s++;
return *s == '
avatar
s*x
35
他好像是L家的,L家的面试官经常自己搞错的,版上经常碰到。给他们一个新解法看半
天都看不懂,sigh.

★ 发自iPhone App: ChineseWeb 8.7

【在 z***c 的大作中提到】
: 我觉得这个解法很好,容易理解也容易写出来。为什么会被踢
avatar
q*c
36
L的码工coding水品堪忧啊,还一堆一堆senior SDE.

【在 s**x 的大作中提到】
: 他好像是L家的,L家的面试官经常自己搞错的,版上经常碰到。给他们一个新解法看半
: 天都看不懂,sigh.
:
: ★ 发自iPhone App: ChineseWeb 8.7

avatar
s*r
37
还是认真点吧,你的getNumDigits(char*)定义上面有问题,肯定会有bug,还不能
handle negative case
对于有经验的面试官,一眼就看出问题来

【在 s**x 的大作中提到】
: 他好像是L家的,L家的面试官经常自己搞错的,版上经常碰到。给他们一个新解法看半
: 天都看不懂,sigh.
:
: ★ 发自iPhone App: ChineseWeb 8.7

avatar
s*x
38
sigh, 怎么说你呢? 真正有经验的读了那个说明根本就不需要看 code 了, 这个
code 真是太简单了, 想出错都很难, 真的, 因为就是简单的 sequencial
processing, 记得我最初 post 的 code, 有人发现有一部忘了移动指针. 如果你用
pointer reference for functions, 这个都不会出错。
为了帮助你, getNUmDigits should really be skipNumDigits to be consistent
with my comments in first post, it is someone else's post, i did not change
completely. but every junior level engineer should know what it does.

【在 s*****r 的大作中提到】
: 还是认真点吧,你的getNumDigits(char*)定义上面有问题,肯定会有bug,还不能
: handle negative case
: 对于有经验的面试官,一眼就看出问题来

avatar
h*2
39
我觉得挺好
但是为什么是n1+n2 == 0 return false
.1 和 1. 都是valid吗
avatar
h*2
40
比如说1.e5,看着感觉有点别扭。
avatar
m*k
42
I like it too!

【在 z***c 的大作中提到】
: 我觉得这个解法很好,容易理解也容易写出来。为什么会被踢
avatar
l*4
43
为什么不用正则表达式?

【在 s**x 的大作中提到】
: 说一下思路吧,懒得写代码
: 1 skip white spaces
: 2 skip + or - if there is one.
: 3 skip all numeric digits, say n1 digits are skipped.
: 4 skip . if there is one
: 5 same as step 3, but save count to n2.
: 6 here is the key part, return false if n1 + n2 == 0.
: 7. Skip E or e if there is one.
: 8 if there is e or E, do step 2 and 3, return false if n1 == 0
: 9 skip white space

avatar
y*2
44
确实很赞。我写了一遍。 过了Leetcode.
avatar
l*s
45
我写了一遍。正则的太麻烦了。
还是楼主的思路好!再贴一下:
class Solution {
private:
void skipSpace(const char *& s)
{
while (*s == ' ' || *s == 't')
{
++s;
}
}
int getNumDigits(const char *s)
{
int num = 0;
while (*s >= '0' && *s <= '9')
{
++s;
++num;
}
return num;
}

public:
bool isNumber(const char *s) {
if (!s) return false;
skipSpace(s);
if (*s == '+' || *s == '-')
{
++s;
}
int numDigits1 = getNumDigits(s);
s += numDigits1;
if (*s == '.')
{
++s;
}
int numDigits2 = getNumDigits(s);
if (numDigits1 + numDigits2 == 0) return false; //点前后不能都没有数字
s += numDigits2;
if (*s == 'E' || *s == 'e')
{
++s;
if (*s == '+' || *s == '-')
{
++s;
}
int numDigits3 = getNumDigits(s);
if (numDigits3 == 0) return false;
s += numDigits3;
}
skipSpace(s);
return *s == '\0';
}
};

【在 l***4 的大作中提到】
: 为什么不用正则表达式?
avatar
l*s
46
对。点前后不能都没有数字

【在 h*********2 的大作中提到】
: 我觉得挺好
: 但是为什么是n1+n2 == 0 return false
: .1 和 1. 都是valid吗

avatar
m*e
47
切记,if 后面一定要换一行,不然可能被评coding style不好。

【在 l****s 的大作中提到】
: 我写了一遍。正则的太麻烦了。
: 还是楼主的思路好!再贴一下:
: class Solution {
: private:
: void skipSpace(const char *& s)
: {
: while (*s == ' ' || *s == 't')
: {
: ++s;
: }

avatar
g*s
48
E后面不能是小数吗,指数不一定非是整数的
avatar
p*y
49
if(....){
a=1;
}
这个风格在面试里算好还是太啰嗦?

【在 m**e 的大作中提到】
: 切记,if 后面一定要换一行,不然可能被评coding style不好。
avatar
s*B
50


【在 s**x 的大作中提到】
: 说一下思路吧,懒得写代码
: 1 skip white spaces
: 2 skip + or - if there is one.
: 3 skip all numeric digits, say n1 digits are skipped.
: 4 skip . if there is one
: 5 same as step 3, but save count to n2.
: 6 here is the key part, return false if n1 + n2 == 0.
: 7. Skip E or e if there is one.
: 8 if there is e or E, do step 2 and 3, return false if n1 == 0
: 9 skip white space

avatar
f*u
51
这个用finite state machine做的方法谁能给个代码链接?学习下。

【在 y****2 的大作中提到】
: 我以前看过的最好的解法是那个用finite state machine做的。
: 我看看你的解法

avatar
z*m
52
很好

【在 s**x 的大作中提到】
: 说一下思路吧,懒得写代码
: 1 skip white spaces
: 2 skip + or - if there is one.
: 3 skip all numeric digits, say n1 digits are skipped.
: 4 skip . if there is one
: 5 same as step 3, but save count to n2.
: 6 here is the key part, return false if n1 + n2 == 0.
: 7. Skip E or e if there is one.
: 8 if there is e or E, do step 2 and 3, return false if n1 == 0
: 9 skip white space

avatar
p*y
53
基本思路应该是按parsing的思路走,然后无需真的计算出数字来。

【在 s**x 的大作中提到】
: 说一下思路吧,懒得写代码
: 1 skip white spaces
: 2 skip + or - if there is one.
: 3 skip all numeric digits, say n1 digits are skipped.
: 4 skip . if there is one
: 5 same as step 3, but save count to n2.
: 6 here is the key part, return false if n1 + n2 == 0.
: 7. Skip E or e if there is one.
: 8 if there is e or E, do step 2 and 3, return false if n1 == 0
: 9 skip white space

avatar
l*s
54
我的秒杀办法:
public boolean isNumber(String s) {
try {
Double.valueOf(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
avatar
N*D
55
def toNum(s: String) = (scala.util.control.Exception.allCatch opt s.toDouble
).isDefined

【在 l********s 的大作中提到】
: 我的秒杀办法:
: public boolean isNumber(String s) {
: try {
: Double.valueOf(s);
: return true;
: } catch (NumberFormatException e) {
: return false;
: }
: }

avatar
w*j
56
用自动机写的
求批判
个人觉得自动机的好处是可视化,只要图画对,代码不容易写错
corner case看得也比较清楚
enum State
{
ErrorState
{
@Override
public boolean isValid() { return false; }
},
EndState,
StartState
{
@Override
public State readDigit() { return DigitState; }
@Override
public State readPlus() { return PlusState; }

@Override
public State readMinus() { return MinusState; }

@Override
public State readPoint() { return PointState; }

@Override
public State readSpace() { return this; }
},
PlusState
{
@Override
public State readDigit() { return DigitState; }

@Override
public State readPoint() { return PlusPointState; }
},
MinusState
{
@Override
public State readDigit() { return DigitState; }

@Override
public State readPoint() { return MinusPointState; }
},
PlusPointState
{
@Override
public State readDigit() { return DigitState; }
},
MinusPointState
{
@Override
public State readDigit() { return DigitState; }
},
DigitState
{
@Override
public State readDigit() { return this; }

@Override
public State readPoint() { return DigitPointState; }

@Override
public State readE() { return EState; }

@Override
public State readEnd() { return EndState; }
},
DigitPointState
{
@Override
public State readDigit() { return PointDigitState; }

@Override
public State readEnd() { return EndState; }

@Override
public State readE() { return EState; }
},
PointState
{
@Override
public State readDigit() { return PointDigitState; }
},
PointDigitState
{
@Override
public State readDigit() { return this; }

@Override
public State readEnd() { return EndState; }

@Override
public State readE() { return EState; }
},
EState
{
@Override
public State readDigit() { return EDigitState; }

@Override
public State readPlus() { return EPlusState; }

@Override
public State readMinus() { return EMinusState; }
},
EPlusState
{
@Override
public State readDigit() { return EDigitState; }
},
EMinusState
{
@Override
public State readDigit() { return EDigitState; }
},
EDigitState
{
@Override
public State readDigit() { return this; }

@Override
public State readEnd() { return EndState; }
};

public boolean isValid() { return true; }
public State readSpace() { return ErrorState; }
public State readDigit() { return ErrorState; }
public State readPlus() { return ErrorState; }
public State readMinus() { return ErrorState; }
public State readPoint() { return ErrorState; }
public State readE() { return ErrorState; }
public State readEnd() { return ErrorState; }
}
public class ValidNumber
{
private static boolean invalidChar(char input) {
return !(Character.isDigit(input) ||
input == ' ' ||
input == '+' ||
input == '-' ||
input == '.' ||
input == 'e' ||
input == 'E');
}

public static boolean isNumber(String s)
{
s = s.trim(); // remove leading and trailing spaces
State state = State.StartState;
for(char c: s.toCharArray())
{
// some chars are not acceptable by any state
if(invalidChar(c))
return false;

if(Character.isDigit(c))
state = state.readDigit();
else if(c == ' ')
state = state.readSpace();
else if(c == '+')
state = state.readPlus();
else if(c == '-')
state = state.readMinus();
else if(c == '.')
state = state.readPoint();
else if(c == 'e' || c == 'E')
state = state.readE();

if(!state.isValid())
return false;
}
state = state.readEnd();
return state == State.EndState;
}
}

【在 f*****u 的大作中提到】
: 这个用finite state machine做的方法谁能给个代码链接?学习下。
avatar
b*g
57
跪了。。。自动机的方法太高深了

【在 w*****j 的大作中提到】
: 用自动机写的
: 求批判
: 个人觉得自动机的好处是可视化,只要图画对,代码不容易写错
: corner case看得也比较清楚
: enum State
: {
: ErrorState
: {
: @Override
: public boolean isValid() { return false; }

avatar
w*j
58
补充一句,自动机另一种写法是用二维表格来记录状态转换,更简洁,但是可读性更差
avatar
Q*a
59
可以用regex写吗? 面试官会允许么? 那样就几行
avatar
s*x
60
说一下思路吧,懒得写代码
1 skip white spaces
2 skip + or - if there is one.
3 skip all numeric digits, say n1 digits are skipped.
4 skip . if there is one
5 same as step 3, but save count to n2.
6 here is the key part, return false if n1 + n2 == 0.
7. Skip E or e if there is one.
8 if there is e or E, do step 2 and 3, return false if n1 == 0
9 skip white space
10 return false if string is empty now, otherwise return true.
Simple, fast and hard to make mistakes.
avatar
s*x
61
有人找到了代码
bool isNumber(const char *s)
{
if (!s) return false;
s = skipSpace(s);
if (*s == '+' || *s == '-')
{
++s;
}
int n1 = getNumDigits(s);
s += n1;
if (*s == '.')
{
++s;
}
int n2 = getNumDigits(s);
if (n1 + n2 == 0) return false;
s += n2;
if (*s == 'E' || *s == 'e')
{
++s;
if (*s == '+' || *s == '-')
{
++s;
}
int n3 = getNumDigits(s);
if (n2 == 0) return false;
s += n3;
}
s = skipSpace(s);
return *s == '\0';
}
avatar
y*2
62
我以前看过的最好的解法是那个用finite state machine做的。
我看看你的解法
avatar
y*n
63
真心说,面试想清楚10步不容易。

【在 s**x 的大作中提到】
: 说一下思路吧,懒得写代码
: 1 skip white spaces
: 2 skip + or - if there is one.
: 3 skip all numeric digits, say n1 digits are skipped.
: 4 skip . if there is one
: 5 same as step 3, but save count to n2.
: 6 here is the key part, return false if n1 + n2 == 0.
: 7. Skip E or e if there is one.
: 8 if there is e or E, do step 2 and 3, return false if n1 == 0
: 9 skip white space

avatar
s*x
64
sigh, 唯一的 trick 就是第六步。 写个浮点数 -123.E+5 , 一清二楚阿。
这个是最直接明了的土办法了。 我是慢慢琢磨出来的, 老觉得网上给出的算法都很坑
爹。 面试高度紧张, 好几个变量直接歇菜。

【在 y***n 的大作中提到】
: 真心说,面试想清楚10步不容易。
avatar
s*x
65
那个当然是最优的, 可以提一下加分。

【在 y****2 的大作中提到】
: 我以前看过的最好的解法是那个用finite state machine做的。
: 我看看你的解法

avatar
y*n
66
你误会了,你的解法很好,我是说看到一个新的题,要想到10步不容易。

【在 s**x 的大作中提到】
: sigh, 唯一的 trick 就是第六步。 写个浮点数 -123.E+5 , 一清二楚阿。
: 这个是最直接明了的土办法了。 我是慢慢琢磨出来的, 老觉得网上给出的算法都很坑
: 爹。 面试高度紧张, 好几个变量直接歇菜。

avatar
s*r
67
还是有corner case没有handle,比如多个.的情况

【在 s**x 的大作中提到】
: 说一下思路吧,懒得写代码
: 1 skip white spaces
: 2 skip + or - if there is one.
: 3 skip all numeric digits, say n1 digits are skipped.
: 4 skip . if there is one
: 5 same as step 3, but save count to n2.
: 6 here is the key part, return false if n1 + n2 == 0.
: 7. Skip E or e if there is one.
: 8 if there is e or E, do step 2 and 3, return false if n1 == 0
: 9 skip white space

avatar
s*r
68
如果真写成这样可以直接踢出去了
如果s里面有非数字的字符,getNumDigits()应该会返回何值,程序里面没有检测这种
返回值。getNumDigits()应该遇到什么情况返回,貌似条件是遇到.或者大小写e,但问
题来了,如果s里面有两个.,比如1.1.1,这个程序也会返回true
这个问题其实有点烦琐,因为需要一些context info

【在 s**x 的大作中提到】
: 有人找到了代码
: bool isNumber(const char *s)
: {
: if (!s) return false;
: s = skipSpace(s);
: if (*s == '+' || *s == '-')
: {
: ++s;
: }
: int n1 = getNumDigits(s);

avatar
m*k
69
it works,
1.1.1
会return '.'=='\0'
avatar
z*c
70
我觉得这个解法很好,容易理解也容易写出来。为什么会被踢
avatar
b*c
71
好思路,就是程序里if (n2 == 0) return false;那里 是不是应该是n3
附上我按照lz思路写的,leetcode OJ 136ms 通过
bool isNumber(const char *s) {
if (!s) return false;
while (*s == ' ') s++;
if (*s == '+' || *s == '-') s++;
int n1 = 0;
while (isdigit(*s)) {
n1++;
s++;
}
if (*s == '.') s++;
int n2 = 0;
while (isdigit(*s)) {
n2++;
s++;
}
if (n1 + n2 == 0) return false;
if (*s == 'e' || *s == 'E') {
s++;
if (*s == '+' || *s == '-') s++;
int n3 = 0;
while (isdigit(*s)) {
n3++;
s++;
}
if(n3 == 0) return false;
}
while (*s == ' ') s++;
return *s == '
avatar
s*x
72
他好像是L家的,L家的面试官经常自己搞错的,版上经常碰到。给他们一个新解法看半
天都看不懂,sigh.

★ 发自iPhone App: ChineseWeb 8.7

【在 z***c 的大作中提到】
: 我觉得这个解法很好,容易理解也容易写出来。为什么会被踢
avatar
q*c
73
L的码工coding水品堪忧啊,还一堆一堆senior SDE.

【在 s**x 的大作中提到】
: 他好像是L家的,L家的面试官经常自己搞错的,版上经常碰到。给他们一个新解法看半
: 天都看不懂,sigh.
:
: ★ 发自iPhone App: ChineseWeb 8.7

avatar
s*r
74
还是认真点吧,你的getNumDigits(char*)定义上面有问题,肯定会有bug,还不能
handle negative case
对于有经验的面试官,一眼就看出问题来

【在 s**x 的大作中提到】
: 他好像是L家的,L家的面试官经常自己搞错的,版上经常碰到。给他们一个新解法看半
: 天都看不懂,sigh.
:
: ★ 发自iPhone App: ChineseWeb 8.7

avatar
s*x
75
sigh, 怎么说你呢? 真正有经验的读了那个说明根本就不需要看 code 了, 这个
code 真是太简单了, 想出错都很难, 真的, 因为就是简单的 sequencial
processing, 记得我最初 post 的 code, 有人发现有一部忘了移动指针. 如果你用
pointer reference for functions, 这个都不会出错。
为了帮助你, getNUmDigits should really be skipNumDigits to be consistent
with my comments in first post, it is someone else's post, i did not change
completely. but every junior level engineer should know what it does.

【在 s*****r 的大作中提到】
: 还是认真点吧,你的getNumDigits(char*)定义上面有问题,肯定会有bug,还不能
: handle negative case
: 对于有经验的面试官,一眼就看出问题来

avatar
h*2
76
我觉得挺好
但是为什么是n1+n2 == 0 return false
.1 和 1. 都是valid吗
avatar
h*2
77
比如说1.e5,看着感觉有点别扭。
avatar
m*k
79
I like it too!

【在 z***c 的大作中提到】
: 我觉得这个解法很好,容易理解也容易写出来。为什么会被踢
avatar
l*4
80
为什么不用正则表达式?

【在 s**x 的大作中提到】
: 说一下思路吧,懒得写代码
: 1 skip white spaces
: 2 skip + or - if there is one.
: 3 skip all numeric digits, say n1 digits are skipped.
: 4 skip . if there is one
: 5 same as step 3, but save count to n2.
: 6 here is the key part, return false if n1 + n2 == 0.
: 7. Skip E or e if there is one.
: 8 if there is e or E, do step 2 and 3, return false if n1 == 0
: 9 skip white space

avatar
y*2
81
确实很赞。我写了一遍。 过了Leetcode.
avatar
l*s
82
我写了一遍。正则的太麻烦了。
还是楼主的思路好!再贴一下:
class Solution {
private:
void skipSpace(const char *& s)
{
while (*s == ' ' || *s == 't')
{
++s;
}
}
int getNumDigits(const char *s)
{
int num = 0;
while (*s >= '0' && *s <= '9')
{
++s;
++num;
}
return num;
}

public:
bool isNumber(const char *s) {
if (!s) return false;
skipSpace(s);
if (*s == '+' || *s == '-')
{
++s;
}
int numDigits1 = getNumDigits(s);
s += numDigits1;
if (*s == '.')
{
++s;
}
int numDigits2 = getNumDigits(s);
if (numDigits1 + numDigits2 == 0) return false; //点前后不能都没有数字
s += numDigits2;
if (*s == 'E' || *s == 'e')
{
++s;
if (*s == '+' || *s == '-')
{
++s;
}
int numDigits3 = getNumDigits(s);
if (numDigits3 == 0) return false;
s += numDigits3;
}
skipSpace(s);
return *s == '\0';
}
};

【在 l***4 的大作中提到】
: 为什么不用正则表达式?
avatar
l*s
83
对。点前后不能都没有数字

【在 h*********2 的大作中提到】
: 我觉得挺好
: 但是为什么是n1+n2 == 0 return false
: .1 和 1. 都是valid吗

avatar
m*e
84
切记,if 后面一定要换一行,不然可能被评coding style不好。

【在 l****s 的大作中提到】
: 我写了一遍。正则的太麻烦了。
: 还是楼主的思路好!再贴一下:
: class Solution {
: private:
: void skipSpace(const char *& s)
: {
: while (*s == ' ' || *s == 't')
: {
: ++s;
: }

avatar
g*s
85
E后面不能是小数吗,指数不一定非是整数的
avatar
p*y
86
if(....){
a=1;
}
这个风格在面试里算好还是太啰嗦?

【在 m**e 的大作中提到】
: 切记,if 后面一定要换一行,不然可能被评coding style不好。
avatar
s*B
87


【在 s**x 的大作中提到】
: 说一下思路吧,懒得写代码
: 1 skip white spaces
: 2 skip + or - if there is one.
: 3 skip all numeric digits, say n1 digits are skipped.
: 4 skip . if there is one
: 5 same as step 3, but save count to n2.
: 6 here is the key part, return false if n1 + n2 == 0.
: 7. Skip E or e if there is one.
: 8 if there is e or E, do step 2 and 3, return false if n1 == 0
: 9 skip white space

avatar
f*u
88
这个用finite state machine做的方法谁能给个代码链接?学习下。

【在 y****2 的大作中提到】
: 我以前看过的最好的解法是那个用finite state machine做的。
: 我看看你的解法

avatar
z*m
89
很好

【在 s**x 的大作中提到】
: 说一下思路吧,懒得写代码
: 1 skip white spaces
: 2 skip + or - if there is one.
: 3 skip all numeric digits, say n1 digits are skipped.
: 4 skip . if there is one
: 5 same as step 3, but save count to n2.
: 6 here is the key part, return false if n1 + n2 == 0.
: 7. Skip E or e if there is one.
: 8 if there is e or E, do step 2 and 3, return false if n1 == 0
: 9 skip white space

avatar
p*y
90
基本思路应该是按parsing的思路走,然后无需真的计算出数字来。

【在 s**x 的大作中提到】
: 说一下思路吧,懒得写代码
: 1 skip white spaces
: 2 skip + or - if there is one.
: 3 skip all numeric digits, say n1 digits are skipped.
: 4 skip . if there is one
: 5 same as step 3, but save count to n2.
: 6 here is the key part, return false if n1 + n2 == 0.
: 7. Skip E or e if there is one.
: 8 if there is e or E, do step 2 and 3, return false if n1 == 0
: 9 skip white space

avatar
l*s
91
我的秒杀办法:
public boolean isNumber(String s) {
try {
Double.valueOf(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
avatar
N*D
92
def toNum(s: String) = (scala.util.control.Exception.allCatch opt s.toDouble
).isDefined

【在 l********s 的大作中提到】
: 我的秒杀办法:
: public boolean isNumber(String s) {
: try {
: Double.valueOf(s);
: return true;
: } catch (NumberFormatException e) {
: return false;
: }
: }

avatar
w*j
93
用自动机写的
求批判
个人觉得自动机的好处是可视化,只要图画对,代码不容易写错
corner case看得也比较清楚
enum State
{
ErrorState
{
@Override
public boolean isValid() { return false; }
},
EndState,
StartState
{
@Override
public State readDigit() { return DigitState; }
@Override
public State readPlus() { return PlusState; }

@Override
public State readMinus() { return MinusState; }

@Override
public State readPoint() { return PointState; }

@Override
public State readSpace() { return this; }
},
PlusState
{
@Override
public State readDigit() { return DigitState; }

@Override
public State readPoint() { return PlusPointState; }
},
MinusState
{
@Override
public State readDigit() { return DigitState; }

@Override
public State readPoint() { return MinusPointState; }
},
PlusPointState
{
@Override
public State readDigit() { return DigitState; }
},
MinusPointState
{
@Override
public State readDigit() { return DigitState; }
},
DigitState
{
@Override
public State readDigit() { return this; }

@Override
public State readPoint() { return DigitPointState; }

@Override
public State readE() { return EState; }

@Override
public State readEnd() { return EndState; }
},
DigitPointState
{
@Override
public State readDigit() { return PointDigitState; }

@Override
public State readEnd() { return EndState; }

@Override
public State readE() { return EState; }
},
PointState
{
@Override
public State readDigit() { return PointDigitState; }
},
PointDigitState
{
@Override
public State readDigit() { return this; }

@Override
public State readEnd() { return EndState; }

@Override
public State readE() { return EState; }
},
EState
{
@Override
public State readDigit() { return EDigitState; }

@Override
public State readPlus() { return EPlusState; }

@Override
public State readMinus() { return EMinusState; }
},
EPlusState
{
@Override
public State readDigit() { return EDigitState; }
},
EMinusState
{
@Override
public State readDigit() { return EDigitState; }
},
EDigitState
{
@Override
public State readDigit() { return this; }

@Override
public State readEnd() { return EndState; }
};

public boolean isValid() { return true; }
public State readSpace() { return ErrorState; }
public State readDigit() { return ErrorState; }
public State readPlus() { return ErrorState; }
public State readMinus() { return ErrorState; }
public State readPoint() { return ErrorState; }
public State readE() { return ErrorState; }
public State readEnd() { return ErrorState; }
}
public class ValidNumber
{
private static boolean invalidChar(char input) {
return !(Character.isDigit(input) ||
input == ' ' ||
input == '+' ||
input == '-' ||
input == '.' ||
input == 'e' ||
input == 'E');
}

public static boolean isNumber(String s)
{
s = s.trim(); // remove leading and trailing spaces
State state = State.StartState;
for(char c: s.toCharArray())
{
// some chars are not acceptable by any state
if(invalidChar(c))
return false;

if(Character.isDigit(c))
state = state.readDigit();
else if(c == ' ')
state = state.readSpace();
else if(c == '+')
state = state.readPlus();
else if(c == '-')
state = state.readMinus();
else if(c == '.')
state = state.readPoint();
else if(c == 'e' || c == 'E')
state = state.readE();

if(!state.isValid())
return false;
}
state = state.readEnd();
return state == State.EndState;
}
}

【在 f*****u 的大作中提到】
: 这个用finite state machine做的方法谁能给个代码链接?学习下。
avatar
b*g
94
跪了。。。自动机的方法太高深了

【在 w*****j 的大作中提到】
: 用自动机写的
: 求批判
: 个人觉得自动机的好处是可视化,只要图画对,代码不容易写错
: corner case看得也比较清楚
: enum State
: {
: ErrorState
: {
: @Override
: public boolean isValid() { return false; }

avatar
w*j
95
补充一句,自动机另一种写法是用二维表格来记录状态转换,更简洁,但是可读性更差
avatar
Q*a
96
可以用regex写吗? 面试官会允许么? 那样就几行
avatar
Z*0
97
大家都没有修过编译原理?都是要要求你自己实现一个parser, 然后用再用yacc/bison
实现一次。
avatar
w*t
98
shortest version, according to these guidelines and leetcode's offcial codes:
class Solution {
public:
bool isNumber(const char *s) {
if (!s) return false;
while (*s && isblank(*s)) ++s; // preceding spaces
if (*s && ('+' == *s || '-' == *s)) ++s; // preceding +/-
bool ret = false;
for (; *s && isdigit(*s); ret = true, ++s); // integer part
if (*s && '.' == *s) ++s;
for (; *s && isdigit(*s); ret = true, ++s); // decimal
if (ret && *s && 'e' == tolower(*s)) {
++s;
ret = false;
if (*s && ('+' == *s || '-' == *s)) ++s; // preceding +/-
for (; *s && isdigit(*s); ret = true, ++s); // integer part
}
while (*s && isblank(*s)) ++s; // tail spaces
return ret && !*s;
}
};
avatar
f*0
99
跟我写的基本完全一样啊!
n1+n2==0判断是否valid太赞了!
另外,if,while后面只有一句的到底要不要加花括号啊?
class Solution {
public:
bool isNumber(const char *s) {
while (*s == ' ') s++;
if (*s == '+' || *s == '-') s++;
int n1 = 0;
while (*s <= '9' && *s >= '0') {
n1++;
s++;
}
if (*s == '.') s++;
int n2 = 0;
while (*s <= '9' && *s >= '0') {
n2++;
s++;
}
if (n1+n2 == 0) return false;
if (*s == 'e' || *s == 'E') {
s++;
if (*s == '+' || *s == '-') s++;
int n3 = 0;
while (*s <= '9' && *s >= '0') {
s++;
n3++;
}
if (n3 == 0) return false;
}
while (*s == ' ') s++;
return *s == '
avatar
s*x
100
这好像是俺刷题时唯一自己想出来解法的题。
avatar
x*a
101
你是在开玩笑?
Leetcode很多简单题三五行代码就写出来的你不会做偏偏会做这个?

【在 s**x 的大作中提到】
: 这好像是俺刷题时唯一自己想出来解法的题。
avatar
p*p
102
public class Solution {

int[][] stateTrans = {
{1, 2, 3, -1, -1},
{1, -1, 4, 5, -1},
{1, -1, 3, -1, -1},
{4, -1, -1, -1, -1},
{4, -1, -1, 5, -1},
{7, 6, -1, -1, -1},
{7, -1, -1, -1, -1},
{7, -1, -1, -1, -1}
};

public boolean isNumber(String s) {
int state = 0;
s = s.trim();
for (int i = 0; i < s.length(); i++) {
state = stateTrans[state][getCharGroup(s.charAt(i))];
if (state == -1) {
return false;
}
}
return state == 7 || state == 4 || state == 1;
}

private int getCharGroup(char c) {
if (c >= '0' && c <= '9') {
return 0;
}
else if (c == '-' || c == '+') {
return 1;
}
else if (c == '.') {
return 2;
}
else if (c == 'e' || c == 'E') {
return 3;
}
return 4;
}
}
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。