[LeetCode] Valid Number
public boolean isNumber(String s) {
if(s.trim().isEmpty()){
return false;
}
/*
re* Matches 0 or more occurrences of preceding expression.
re+ Matches 1 or more of the previous thing
re? Matches 0 or 1 occurrence of preceding expression.
a| b Matches either a or b.
[...] Matches any single character in brackets.
\d Matches digits. Equivalent to [0-9].
*/
String regex = "[-+]?(\\d+\\.?|\\.\\d+)\\d*(e[-+]?\\d+)?";
if(s.trim().matches(regex)){
return true;
}else{
return false;
}
}
九章算法给了一个non regex解答
public class Solution {
public boolean isNumber(String s) {
int len = s.length();
int i = 0, e = len - 1;
while (i <= e && Character.isWhitespace(s.charAt(i))) i++;
if (i > len - 1) return false;
while (e >= i && Character.isWhitespace(s.charAt(e))) e--;
// skip leading +/-
if (s.charAt(i) == '+' || s.charAt(i) == '-') i++;
boolean num = false; // is a digit
boolean dot = false; // is a '.'
boolean exp = false; // is a 'e'
while (i <= e) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
num = true;
}
else if (c == '.') {
if(exp || dot) return false;
dot = true;
}
else if (c == 'e') {
if(exp || num == false) return false;
exp = true;
num = false;
}
else if (c == '+' || c == '-') {
if (s.charAt(i - 1) != 'e') return false;
}
else {
return false;
}
i++;
}
return num;
}
}