t******d 发帖数: 1383 | 1 做了个望上的测试,结果昨天晚上做了,今天被据了。请刷了poj的看看。 这哪里还有
bug?
跑出来的结果是
the number 007 is a number: false
the number 0.5 is a number: true
the number .01 is a number: true
the number 9. is a number: true
the number 1.000 is a number: true
the number 00.5 is a number: false
the number -.005000 is a number: true
我看着没问题
public class IsNumber {
/**
* The IsNumber function takes a String and returns true if that string
is a number, and false otherwise.
* This implementation, however, has several bugs in it. Your task is to
find and fix those bugs.
* Please note that you should be fixing bugs in this implementation,
not implementing your own version.
* Parameters:
* - Numbers should be base-10 only. They may be negative, and may have
decimal portions
* - Numbers should not have any size restrictions (as might be imposed
by the sizes of doubles or longs)
* - Numbers should not have any extra characters, such as whitespace
or letters
* - Numbers should not have leading zeros (007 is a secret agent, not
a number)
* - 0.5, .01, 9., and 1.000 are all numbers, however. 00.5 is not.
* Keep in mind that an engineer will be reviewing your code, and write
it in a way that would pass a peer code review
*/
public static void main(String[] args) {
IsNumber n = new IsNumber();
System.out.println("the number 007 is a number: " + n.isNumber("007"
));
System.out.println("the number 0.5 is a number: " + n.isNumber("0.5"
));
System.out.println("the number .01 is a number: " + n.isNumber(".01"
));
System.out.println("the number 9. is a number: " + n.isNumber("9."));
System.out.println("the number 1.000 is a number: " + n.isNumber("1.
000"));
System.out.println("the number 00.5 is a number: " + n.isNumber("00.
5"));
System.out.println("the number -.005000 is a number: " + n.isNumber(
"-.005000"));
}
public boolean isNumber(String toTest) {
int l = 0;
int r = toTest.length() -1;
while (l <= r && toTest.charAt(l) == ' ') {
l++;
}
if (l > toTest.length() -1) {
return false;
}
while ( r >= l && toTest.charAt(r) == ' ') {
r--;
}
if (toTest.charAt(l) == '+' || toTest.charAt(l) == '-') {
l++;
}
boolean onePoint = false;
boolean hasNonZeroChar = false;
boolean care = true;//if 0 is ahead of . , then we care if
second 0 shows up, if 0 is behind . , then we do not care. 007 is not a
number, 1.007 is.
for (int i = l; i <= r; i++) {
char c = toTest.charAt(i);
if (c == '.') {
if (onePoint)
return false;
onePoint = true;
hasNonZeroChar = true;
care = false;
}
else if (c >= '1' && c <= '9') {
hasNonZeroChar = true;
}
else if (c == '0') {
if (!hasNonZeroChar && care) {
if (i < toTest.length() - 1) {
if (toTest.charAt(i+1) == '.') {
care = false;
continue;
}
else return false;
} else break;
}
}
else if (c == '-' || c == '+') {
return false;
} else {
return false;
}
}
return true;
}
} | l*******u 发帖数: 198 | 2 没刷过POJ, 记得leetcode上也有这道题 | H**********5 发帖数: 2012 | 3 压根就是没打算招你。move on。亲测近期这种凑人数的面试很多。 | t******d 发帖数: 1383 | 4 请帮我看看吧。
【在 l*******u 的大作中提到】 : 没刷过POJ, 记得leetcode上也有这道题
| t******d 发帖数: 1383 | 5 没有,hr这一次还是很想我面的。是不是我拖太久了,他以为我不在乎她们公司。
请帮我看看code吧
【在 H**********5 的大作中提到】 : 压根就是没打算招你。move on。亲测近期这种凑人数的面试很多。
| t******d 发帖数: 1383 | | t**********n 发帖数: 1718 | | t******d 发帖数: 1383 | 8 亲爱的,你看我写了main,我自己测了的。
只是被据了,我也没feedback',所以来问一下刷了poj 2000的大牛们。
【在 t**********n 的大作中提到】 : 不会装个ide debug一下?
| l*****t 发帖数: 1080 | | t******d 发帖数: 1383 | 10 他说要改,不能自己实现。
我就只有这么改。
【在 l*****t 的大作中提到】 : 这代码质量,我去,太不精简了。
| r*********r 发帖数: 53 | 11 无聊过来回复下吧, code大概扫了几秒钟。
1. code style 太烂
a. hard to follow, readability 太差
b. too verbose
2. 没有考虑到科学计数的case, eg: 7.823E5, 1.2e−4
PS: 面试的时候不要觉得题目写出来了feedback就一定好,code 是不是clean, 是不是
organized well也是很重要的一个factor. | P******r 发帖数: 1342 | 12 如果全是空格结果对吗?
如果只有一个+或者-结果对吗? | P******r 发帖数: 1342 | 13 如果全是空格结果对吗?
如果只有一个+或者-结果对吗? | t******d 发帖数: 1383 | 14 我是按照他的骨架modify的。不是我实现,题目说的, 只能modify。
科学计数,没说要。
【在 r*********r 的大作中提到】 : 无聊过来回复下吧, code大概扫了几秒钟。 : 1. code style 太烂 : a. hard to follow, readability 太差 : b. too verbose : 2. 没有考虑到科学计数的case, eg: 7.823E5, 1.2e−4 : PS: 面试的时候不要觉得题目写出来了feedback就一定好,code 是不是clean, 是不是 : organized well也是很重要的一个factor.
|
|