题目描述

// 力扣 & 牛客
// 请实现一个函数用来判断字符串是否表示数值(包括整数和
// 小数)。例如,字符串"+100"、"5e2"、"-123"、"3.1416"、
// "-1E-16"、"0123"都表示数值,但"12e"、"1a3.14"、"1.2.3
// "、"+-5"及"12e+5.4"都不是。
// 建议做牛客的题,这题在力扣里的测试用例会带空格,非常智障
题解
////////////////////////////////// 条件判断 /////////////////////////////////
// 牛客
// 运行时间:10ms
// 占用内存:9448k
public class Solution {
public boolean isNumeric(char[] str) {
int len = str.length
if (len == 0)
return false
boolean existE = false
int existSign = 0
boolean existSignM = false
boolean existPoint = false
// 遍历str元素索引记为i
for (int i = 0
if (str[i] == 'e' || str[i] == 'E') {
if (existE) return false
if (i == 0 || i == len - 1)
return false
existE = true
}
else if (str[i] == '+' || str[i] == '-') {
if (existSign == 2) return false
if (i == 0) {
existSign += 1
continue
}
else if (str[i - 1] != 'E' && str[i - 1] != 'e') {
return false
}
existSign += 1
existSignM = true
}
else if (str[i] == '.') {
if (existPoint) return false
if (i == 0 || i == len - 1) // 头尾出现小数点,false
return false
if (existSignM) return false
existPoint = true
}
else if (str[i] > '9' || str[i] < '0') {
return false
}
}
return true
}
}
// 简化版
// 运行时间:8ms
// 占用内存:9696k
public class Solution {
public boolean isNumeric(char[] str) {
int len = str.length
if (len == 0)
return false
boolean existE = false
boolean existSignM = false
boolean existPoint = false
// 遍历str元素索引记为i
for (int i = 0
if (str[i] == 'e' || str[i] == 'E') {
if (existE) return false
if (i == 0 || i == len - 1)
return false
existE = true
}
else if (str[i] == '+' || str[i] == '-') {
if (existSignM) return false
if (i == 0) { // 第一个存在+-,无视
continue
}
else if (str[i - 1] != 'E' && str[i - 1] != 'e') {
return false
}
existSignM = true
}
else if (str[i] == '.') {
if (existPoint) return false
if (i == 0 || i == len - 1) // 头尾出现小数点,false
return false
if (existSignM) return false
existPoint = true
}
else if (str[i] > '9' || str[i] < '0') {
return false
}
}
return true
}
}
///////////////////////////////// 正则表达式 ////////////////////////////
// 牛客
// 运行时间:14ms
// 占用内存:9892k
// [] : 字符集合
// () : 分组
// ? : 重复 0 ~ 1 次
// + : 重复 1 ~ n 次
// * : 重复 0 ~ n 次
// . : 任意字符
// \\. : 转义后的 .
// \\d : 数字
public class Solution {
public boolean isNumeric(char[] str) {
if (str == null || str.length == 0)
return false;
return new String(str).matches("[+-]?\\d*(\\.\\d+)?([eE][+-]?\\d+)?");
}
}