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 }
  • 相关阅读:
    《浪潮之巅》读后感
    学习进度表
    学习进度表
    兴趣问题清单
    第六周周结
    价值观作业
    学习进度表
    第三次作业(问卷调查)
    第五周(生成动态表格)
    【Other】LanProxy内网穿透
  • 原文地址:https://www.cnblogs.com/ncznx/p/9193365.html
Copyright © 2011-2022 走看看