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.
public class Solution {
public boolean isNumber(String s) {
s = s.trim();
if(s.length()!=0 && (s.charAt(0)=='-' || s.charAt(0)=='+')) s = s.substring(1);
if(s.isEmpty()) return false;
return isNum(s);
}
public boolean isNum(String s){
boolean hasInteger=false, hasDot=false, hasSign=false, hasExp=false, hasExpNum=false;
for(char c : s.toCharArray()) {
if(c>='0' && c<='9') {
hasInteger = true;
if(hasExp) hasExpNum = true;
} else if(c=='.') {
if(!hasDot && !hasExp) hasDot = true;
else return false;
} else if(c=='+' || c=='-') {
if(!hasSign && hasExp && !hasExpNum) hasSign = true;
else return false;
} else if(c=='e') {
if(hasInteger && !hasExp) hasExp = true;
else return false;
} else return false;
}
if(hasInteger && (hasExp==hasExpNum)) return true;
else return false;
}
}
public boolean isNumber(String s) {
int i = 0, n = s.length();
while (i < n && Character.isWhitespace(s.charAt(i))) i++;
if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;
boolean isNumeric = false;
while (i < n && Character.isDigit(s.charAt(i))) {
i++;
isNumeric = true;
}
if (i < n && s.charAt(i) == '.') {
i++;
while (i < n && Character.isDigit(s.charAt(i))) {
i++;
isNumeric = true;
}
}
if (isNumeric && i < n && s.charAt(i) == 'e') {
i++;
isNumeric = false;
if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;
while (i < n && Character.isDigit(s.charAt(i))) {
i++;
isNumeric = true;
}
}
while (i < n && Character.isWhitespace(s.charAt(i))) i++;
return isNumeric && i == n;
}
===================
public class Solution {
public boolean isNumber(String s) {
int i = 0, n = s.length();
while (i < n && Character.isWhitespace(s.charAt(i))) i++;
if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;
boolean isNumeric = false;
while (i < n && Character.isDigit(s.charAt(i))) {
i++;
isNumeric = true;
}
if (i < n && s.charAt(i) == '.') {
i++;
while (i < n && Character.isDigit(s.charAt(i))) {
i++;
isNumeric = true;
}
}
if (isNumeric && i < n && s.charAt(i) == 'e') {
i++;
isNumeric = false;
if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;
while (i < n && Character.isDigit(s.charAt(i))) {
i++;
isNumeric = true;
}
}
while (i < n && Character.isWhitespace(s.charAt(i))) i++;
return isNumeric && i == n;
}
}
========
public class Solution {
public boolean isNumber(String s) {
if (s == null) return false;
int i = 0;
int n = s.length();
boolean numerical = false;
while (i < n && Character.isWhitespace(s.charAt(i))) i++;
if (i < n && (s.charAt(i) == '-' || s.charAt(i) == '+')) i++;
while (i < n && Character.isDigit(s.charAt(i))) {
i++;
numerical = true;
}
if (i < n && s.charAt(i) == '.') {
i++;
while (i < n && Character.isDigit(s.charAt(i))) {
i++;
numerical = true;
}
}
if (i < n && s.charAt(i) == 'e' && numerical) {
i++;
numerical = false;
if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;
while (i < n && Character.isDigit(s.charAt(i))) {
i++;
numerical = true;
}
}
while (i < n && Character.isWhitespace(s.charAt(i))) i++;
return numerical && i == n;
}
}
没有评论:
发表评论