Valid Number
Source
Validate if a given string is numeric.
Example
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
Java
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--;
if (s.charAt(i) == '+' || s.charAt(i) == '-') i++;
boolean num = false;
boolean dot = false;
boolean exp = false;
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;
}
}