题目

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. JavaScript的计时器的工作原理

    最近都在看一些JavaScript原理层面的文章,恰巧看到了jQuery的作者的一篇关于JavaScript计时器原理的解析,于是诚惶诚恐地决定把原文翻译成中文,一来是为了和大家分享,二来是为了加深自 ...

  2. GBDT算法

    GBDT通过多轮迭代,每轮迭代产生一个弱分类器,其中弱分类器通常选择为CART树,每个分类器在上一轮分类器的残差基础上进行训练. 对于GBDT算法,其中重要的知识点为: 1.GBDT是梯度下降法从参数 ...

  3. tomcat配置问题:访问http://localhost:8080/ 遇到 Access Error: 404

    win7: 8080端口已经被其他应用使用,比如nixxxxxxxxxxxxx When I had an error Access Error: 404 -- Not Found I fixed i ...

  4. CocoaPods第三方库管理工具

    http://code4app.com/article/cocoapods-install-usage

  5. 关于Android中传递数据的一些讨论

    在Android中编写过程序的开发人员都知道.在Activity.Service等组件之间传递数据(尤其是复杂类型的数据)很不方便.一般可以使用Intent来传递可序列化或简单类型的数据.看下面的代码 ...

  6. DMA Stream/Channel Outputting via GPIOC[0..7]

    Ok, so quickly mashing up another example using a different TIM, DMA Stream/Channel for illustration ...

  7. TrinityCore 魔兽世界私服11159 完整配置

    为什么要研究TrinityCore ? (1)它是一个完整成熟的可运行调试的网游服务器框架. (2)它是一个跨平台的标准C++编写的项目,在Windows.Linux.MacOSX上都可编译运行. ( ...

  8. x86 TargetPlatform with XBAPs

    I've got a XAML Browser Hosted Application (XBAP) project that has a dependency on another project t ...

  9. delphi udp文件传输

    客户端: unit UnitClient; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Contr ...

  10. Windows Phone本地数据库(SQLCE):10、创建数据库(翻译) (转)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...