《剑指Offer》20. 表示数值的字符串

题目链接

牛客网

题目描述

true

"+100"
"5e2"
"-123"
"3.1416"
"-1E-16"
false

"12e"
"1a3.14"
"1.2.3"
"+-5"
"12e+4.3"

解题思路

使用正则表达式进行匹配。

[]  : 字符集合
()  : 分组
?   : 重复 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+)?");
    }
}
发布了206 篇原创文章 · 获赞 32 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_38611497/article/details/104147450