请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
public class Solution54 { private static int index=0; public boolean isNumeric(char[] str) { if(str.length > 0){ index= 0; if(str[index] == ‘-‘ || str[index] == ‘+‘){ index ++; } if(index < str.length){ index = zhengshu(str); if(index == str.length)return true; if(str[index] == ‘.‘){ index ++; if(index ==str.length) return false; index = zhengshu(str); if(index == str.length)return true; if(str[index] == ‘.‘)return false; else if(str[index] == ‘e‘ || str[index] == ‘E‘){ index ++; if(index == str.length) return false; if(str[index] == ‘+‘ || str[index] == ‘-‘){ index ++; } if(index == str.length) return false; index = zhengshu(str); if(index ==str.length) return true; }else{ if(index == str.length)return true; else return false; } }else if(str[index] == ‘e‘ || str[index] == ‘E‘){ index ++; if(index == str.length) return false; if(str[index] == ‘+‘ || str[index] == ‘-‘){ index ++; } if(index == str.length) return false; index = zhengshu(str); if(index ==str.length) return true; } return false; } } return false; } private static int zhengshu(char[] str) { while(index < str.length && str[index] >=‘0‘ && str[index]<=‘9‘){ index ++; } return index; } }
时间: 2024-10-11 07:15:24