diff --git a/notes/20. 表示数值的字符串.md b/notes/20. 表示数值的字符串.md index 60527c11..0990b2e0 100644 --- a/notes/20. 表示数值的字符串.md +++ b/notes/20. 表示数值的字符串.md @@ -1,6 +1,6 @@ # 20. 表示数值的字符串 -[牛客网](https://www.nowcoder.com/practice/e69148f8528c4039ad89bb2546fd4ff8?tpId=13&tqId=11206&tab=answerKey&from=cyc_github) +[Leetcode](https://leetcode.cn/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof/) ## 题目描述 @@ -41,9 +41,9 @@ false ``` ```java -public boolean isNumeric (String str) { - if (str == null || str.length() == 0) +public boolean validNumber(String s) { + if (s == null || s.length() == 0) return false; - return new String(str).matches("[+-]?\\d*(\\.\\d+)?([eE][+-]?\\d+)?"); + return s.trim().matches("[+-]?(\\d+(\\.\\d*)?|\\.\\d+)([eE][+-]?\\d+)?"); } ```