zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    hdu 1021
    hdu 1231 最大连续和
    hdu 1421 DP
    日记本开发
    hdu 2570 贪心
    hdu 2102 BFS
    hdu 1312 DFS
    奇葩的数组越界
    AngularJS 工具方法以及AngularJS中使用jQuery
    AngularJS $scope里面的$apply方法和$watch方法
  • 原文地址:https://www.cnblogs.com/ncznx/p/9193365.html
Copyright © 2011-2022 走看看