首页 > 编程语言 > 详细

Valid Number leetcode java

时间:2018-06-17 18:36:08      阅读:170      评论:0      收藏:0      [点我收藏+]

描述
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 }

 

Valid Number leetcode java

原文:https://www.cnblogs.com/ncznx/p/9193365.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!