题目

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.

题解

正则表达式。

 本文代码引用自:http://blog.csdn.net/fightforyourdream/article/details/12900751

代码:

 1     public boolean isNumber(String s) {
 2         if(s.trim().isEmpty()){  
 3             return false;  
 4         }  
 5         String regex = "[-+]?(\\d+\\.?|\\.\\d+)\\d*(e[-+]?\\d+)?";  
 6         if(s.trim().matches(regex)){  
 7             return true;  
 8         }else{  
 9             return false;  
         }  
     }

如果按照判断的方法可以如下:

 1 public static boolean isNumber(String s) {
 2         int i = 0;
 3         while(s.charAt(i) == ' '){    // 移除前导whitespace
 4             i++;
 5             if(i >= s.length()){
 6                 return false;
 7             }
 8         }
 9         if(s.charAt(i)=='+' || s.charAt(i)=='-'){    // 忽略符号位
             i++;
         }
         int j = s.length()-1;
         while(s.charAt(j) == ' '){    // 移除后缀whitespace
             j--;
         }
         if(i <= j){
             s = s.substring(i, j+1);
         }else{
             return false;
         }
         
         int dot = -1;    // 记录点的位置
         int ee = -1;    // 记录e的位置
         for(i=0; i<s.length(); i++){
             if(dot==-1 && s.charAt(i)=='.'){
                 dot = i;
             }else if(ee==-1 && s.charAt(i)=='e'){
                 ee = i;
                 if(i+1<s.length() && (s.charAt(i+1)=='-' || s.charAt(i+1)=='+')){
                     i++;
                 }
             }else{
                 if(Character.isDigit(s.charAt(i))){
                     continue;
                 }else{
                     return false;
                 }
             }
         }
         
         //xxx.xxexx
         String startStr, midStr, lastStr;
         if(dot==-1 && ee==-1){    //xxx  
             startStr = s;    // xxx
             if(startStr.length()<1){
                 return false;
             }
         }else if(dot!=-1 && ee==-1){    //xxx.yyy  
             startStr = s.substring(0, dot);    // xxx
             midStr = s.substring(dot+1);        // yyy
             if(startStr.length()<1 && midStr.length()<1){
                 return false;
             }
         }else if(dot==-1 && ee!=-1){    // xxxeyyy
             startStr = s.substring(0, ee);    // xxx
             if(startStr.length()<1){
                 return false;
             }
             if(ee+1<s.length() && (s.charAt(ee+1)=='-' || s.charAt(ee+1)=='+')){    // xxxe-zz
                 lastStr = s.substring(ee+2);    // zz
             }else{
                 lastStr = s.substring(ee+1);
             }
             if(lastStr.length() < 1){
                 return false;
             }
         }else{        //xxx.yyezz
             if(dot>ee){        // 位置不对
                 return false;
             }
             startStr = s.substring(0, dot);    // xxx
             midStr = s.substring(dot+1, ee);    // yy
             if(startStr.length()<1 && midStr.length()<1){
                 return false;
             }
             if(ee+1<s.length() && (s.charAt(ee+1)=='-' || s.charAt(ee+1)=='+')){
                 lastStr = s.substring(ee+2);    // zz
             }else{
                 lastStr = s.substring(ee+1);
             }
             if(lastStr.length() < 1){
                 return false;
             }
         }
         return true;
     }

Valid Number leetcode java的更多相关文章

  1. Letter Combinations of a Phone Number leetcode java

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  2. Ugly Number leetcode java

    问题描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...

  3. Single Number leetcode java

    问题描述: Given an array of integers, every element appears twice except for one. Find that single one. ...

  4. Palindrome Number leetcode java

    题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...

  5. Valid Sudoku leetcode java

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  6. Valid Palindrome leetcode java

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  7. Longest Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  8. Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  9. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

随机推荐

  1. Wireshark数据抓包教程之安装Wireshark

    Wireshark数据抓包教程之安装Wireshark 安装Wireshark 通过上一节的学习可以根据自己的操作系统来下载安装Wireshark了.本书中已开发版1.99.7(中文版)为主,下面介绍 ...

  2. Redis如何处理客户端连接

    本文主要介绍了 Redis 处理客户端连接的一些内部实现机制,包括连接处理.超时.缓冲区等一系列内容. 注:本文所述内容基于 Redis2.6 及以上版本. 连接的建立 Redis 通过监听一个 TC ...

  3. FireDAC 下的 Sqlite [10] - 使用 R-Tree 搜索

    R-Tree 主要用于三维空间的搜索, 据说这种搜索算法非常之快, 哪怕百万条记录也是眨眼间的事! SQLite 支持 1-5 维, FireDAC 也提供了 TFDSQLiteRTree 控件以方便 ...

  4. AI 实验--v_JULY_v

    http://blog.csdn.net/v_JULY_v http://www.julyedu.com/

  5. 我来科普一下为毛很多人升级了20M的电信光纤宽带反而感觉速度更卡了

    下载(在线看视频,看网页,下载游戏这类都是属于下载类应用) 为毛很多人升级20M光纤更慢了呢? 因为电信对你的上传速度做了手脚, 8M以及以上家用光纤宽带全部上传限速到100KB/s  也就是1M带宽 ...

  6. delphi时间日期函数

    unit DateProcess; interface const DayOfWeekStrings: ..] of String = ('SUNDAY', 'MONDAY', 'TUESDAY', ...

  7. h股和L股

  8. Java 字符串包含

    函数boolean containsAny(String str, String searchChars) 判断str字符串中是否包含searchChars字符串 String khh_str = & ...

  9. .NET:System.Security.Cryptography.CryptographicException 的解决办法

    详细内容参考此网址:http://social.msdn.microsoft.com/Forums/en-US/ec93922a-fd1e-4225-b5cf-1472ebb3acd1/systems ...

  10. 用drawRect以及CAReplicatorLayer绘制动态水波纹

    用drawRect以及CAReplicatorLayer绘制动态水波纹 大大简化了写水波纹效果的难度,你可以根据示例自己组装水波纹效果,本设计是几个工具组合在一起完成的效果, DrawRectObje ...