分别讨论三种不同的cases, '-', 'e', '.'。 写出来的程序无比复杂,要把所有的条件都排除。这里要用两个flags, 一个记录负数,一记录e。
另外可以使用正则表达式。[-+]?(\\d+\\.?|\\.\\d+)\\d*(e[-+]?\\d+)?
这道题总写总错,原来是自己写的方法不是非常好,这里把存在e的数字前面搞成两部分。
ex:e2 false (firstPart==false)
2e (eFound==true && secondPart==false) false
其他的为true
代码写起来非常容易。
45.e3 is that correct?
public boolean isNumber(String s) {
if (s==null) return false;
char[] sArr=s.trim().toCharArray();
if (sArr.length==0) return false;
int index=0;
if (sArr[index]=='+'||sArr[index]=='-') index++;
boolean eFound=false;
boolean dotFound=false;
boolean firstPart=false;
boolean secondPart=false;
boolean spaceFound=false;
while(index<sArr.length){
if (sArr[index]=='.'){
if(dotFound||eFound||spaceFound) return false;
else dotFound=true;
}
else if (sArr[index]=='e'||sArr[index]=='E'){
if (eFound||!firstPart||spaceFound) return false;
else eFound=true;
}
else if (Character.isDigit(sArr[index])){
if (spaceFound) return false;
if (!eFound) firstPart=true;
else secondPart=true;
}
else if (sArr[index]=='+'||sArr[index]=='-'){
if (spaceFound) return false;
if (!eFound||!(sArr[index-1]=='e'||sArr[index-1]=='E'))
return false;
}
else if (sArr[index]==' ') spaceFound=true;
else return false;
index++;
}
if (!firstPart) return false;
else if (eFound && !secondPart) return false;
else return true;
}
public boolean isNumber(String s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(s==null)
return false;
char[] sArr = s.trim().toCharArray();
if(sArr.length==0)
return false;
if(sArr.length==1&&!Character.isDigit(sArr[0]))
return false;
boolean decimalFound = false;
boolean eFound = false;
int end = sArr.length-1;
for(int i=0;i<=end;i++){
char nextChar = i>=end?'x':sArr[i+1];
char prevChar = i<=0?'x':sArr[i-1];
switch(sArr[i]){
case '+':
case '-':
if(prevChar!='e'&&i!=0)
return false;
if(prevChar=='e'&&i==end)
return false;
if (i==0&&nextChar=='e')
return false;
break;
case '.':
if(decimalFound || eFound)
return false;
if(i>=end && i<=0)
return false;
if(!Character.isDigit(prevChar) && !Character.isDigit(nextChar))
return false;
decimalFound = true;
break;
case 'e':
if(eFound)
return false;
if(!Character.isDigit(prevChar) && !Character.isDigit(nextChar)
&&nextChar!='-'|| end==i || i==0){
return false;
}
eFound = true;
break;
case ' ':
return false;
default:
if(!Character.isDigit(sArr[i]))
return false;
}
}
return true;
}
Better Solution from book of LEETCODE
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))) {
isNumeric = true;
i++;
}
if (i<n && s.charAt(i) == '.') {
i++;
while (i<n && Character.isDigit(s.charAt(i))) {
isNumeric = true;
i++;
}
}
if (isNumeric && i<n && (s.charAt(i) == 'e' || 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))) {
isNumeric = true;
i++;
}
}
while (i<n && Character.isWhitespace(s.charAt(i))) i++;
return isNumeric && i == n;
}
No comments:
Post a Comment