Valid Number leetcode java

描述
Validate if a given string is numeric.
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements
up front before implementing one.
分析
细节实现题。
本题的功能与标准库中的 strtod() 功能类似

有效数字,用正则

代码

 1 public class vaildNumber {
 2 
 3     public static void main(String[] args) {
 4         // TODO Auto-generated method stub
 5 //        String s="2e10";
 6            String s="1.5e10";
 7         System.out.println(vaildNumber(s));
 8     }
 9     private static boolean vaildNumber(String s) {
10         if(s.length()==0) return false;
11 //                "0" => true
12 //                " 0.1 " => true
13 //                "abc" => false
14 //                "1 a" => false
15 //                "2e10" => true
16         String regex="\\d+(\\.\\d+)?(e\\d+)?" ;  
17         return s.matches(regex);
18     }
19 }

猜你喜欢

转载自www.cnblogs.com/ncznx/p/9193365.html