import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串
* @return bool布尔型
*/
//常规解法
public boolean isNumeric (String str) {
if(str == null || str.length() == 0) {
return false ;
}
str = str.trim() ;
boolean hasSign = false ;//1
boolean hasNum = false ;//2
boolean hasPoint = false ;//3
boolean hasE = false ;//4
char arr[] = str.toCharArray() ;
for(int i = 0 ; i < arr.length ; i ++) {
char ch = arr[i] ;
if(type(ch) == -1) {
return false ;
}
if(type(ch) == 2) {
hasNum = true ;
}
if(type(ch) == 4) {//为E时掐面必须要有数字
if(hasE||!hasNum) return false ;
else {
//将hasE置为true,其他则重置,因为E前后互不影响
hasE = true ;hasPoint=false ;hasNum=false;hasSign=false ;
}
}
if(type(ch) == 1) {
if(hasSign || hasPoint || hasNum) return false ;
else hasSign = true ;
}
if(type(ch)==3) {
if(hasE||hasPoint) return false ;
else hasPoint = true ;
}
}
//防止 '.'/'E' 的出现
if(!hasNum) {
return false ;
}
return true ;
}
public int type(char ch) {
if(ch == '+' || ch == '-') {
return 1 ;
}
if(ch >= '0' && ch <= '9') {
return 2 ;
}
if(ch == '.') {
return 3 ;
}
if(ch == 'e' || ch == 'E') {
return 4 ;
}
return -1 ;
}
}