您的位置:首页 > 其它

65. Valid Number

2016-07-19 11:54 411 查看
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.
https://discuss.leetcode.com/topic/1095/the-worst-problem-i-have-ever-met-in-this-oj https://discuss.leetcode.com/topic/9490/clear-java-solution-with-ifs
一些摘取的测试数据

test(1, "123", true);
test(2, " 123 ", true);
test(3, "0", true);
test(4, "0123", true);  //Cannot agree
test(5, "00", true);  //Cannot agree
test(6, "-10", true);
test(7, "-0", true);
test(8, "123.5", true);
test(9, "123.000000", true);
test(10, "-500.777", true);
test(11, "0.0000001", true);
test(12, "0.00000", true);
test(13, "0.", true);  //Cannot be more disagree!!!
test(14, "00.5", true);  //Strongly cannot agree
test(15, "123e1", true);
test(16, "1.23e10", true);
test(17, "0.5e-10", true);
test(18, "1.0e4.5", false);
test(19, "0.5e04", true);
test(20, "12 3", false);
test(21, "1a3", false);
test(22, "", false);
test(23, "     ", false);
test(24, null, false);
test(25, ".1", true); //Ok, if you say so
test(26, ".", false);
test(27, "2e0", true);  //Really?!
test(28, "+.8", true);
test(29, " 005047e+6", true);  //Damn = =|||


All we need is to have a couple of flags so we can process the string in linear time:
public boolean isNumber(String s) {
s = s.trim();

boolean pointSeen = false;
boolean eSeen = false;
boolean numberSeen = false;
boolean numberAfterE = true;
for(int i=0; i<s.length(); i++) {
if('0' <= s.charAt(i) && s.charAt(i) <= '9') {
numberSeen = true;
numberAfterE = true;
} else if(s.charAt(i) == '.') {
if(eSeen || pointSeen) {
return false;
}
pointSeen = true;
} else if(s.charAt(i) == 'e') {
if(eSeen || !numberSeen) {
return false;
}
numberAfterE = false;
eSeen = true;
} else if(s.charAt(i) == '-' || s.charAt(i) == '+') {
if(i != 0 && s.charAt(i-1) != 'e') {
return false;
}
} else {
return false;
}
}

return numberSeen && numberAfterE;
}


We start with trimming.
If we see 
[0-9]
 we reset the number flags.
We can only see 
.
 if we didn't see 
e
 or 
.
.
We can only see 
e
 if we didn't see 
e
 but
we did see a number. We reset numberAfterE flag.
We can only see 
+
 and 
-
 in
the beginning and after an 
e

any other character break the validation.

At the and it is only valid if there was at least 1 number and if we did see an 
e
 then
a number after it as well.

So basically the number should match this regular expression:

[-+]?[0-9]*(.[0-9]+)?(e[-+]?[0-9]+)?


-----------------------------------------------------------------------------------------------------------------------

正则表达式版本

public static boolean isNumber(String s)
{
return s.matches("(\\s*)[+-]?((\\.[0-9]+)|([0-9]+(\\.[0-9]*)?))(e[+-]?[0-9]+)?(\\s*)");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: