题目

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. android 上下边框线

    <!-- 连框颜色值 --> <item> <shape> <solid android:color="@android:color/backgro ...

  2. 【bzoj3451】Tyvj1953 Normal 期望+树的点分治+FFT

    题目描述 给你一棵 $n$ 个点的树,对这棵树进行随机点分治,每次随机一个点作为分治中心.定义消耗时间为每层分治的子树大小之和,求消耗时间的期望. 输入 第一行一个整数n,表示树的大小接下来n-1行每 ...

  3. ThreadLocal 详解

    什么是ThreadLocal 根据JDK文档中的解释:ThreadLocal的作用是提供线程内的局部变量,这种变量在多线程环境下访问时能够保证各个线程里变量的独立性. 从这里可以看出,引入Thread ...

  4. IBM BR10i阵列卡配置Raid0/Raid1(转)

    说明:IBM的阵列卡无论多旧多新操作步骤都基本差不多. RAID1的步骤: 开机自检过程中出现ctrl+c提示,按ctrl+c进入LSI Logic Config Utility v6.10.02.0 ...

  5. Revit API得到类别Category设置类别可见性

    start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class c ...

  6. Sqlite3+EF6踩的坑

    摘要 最近在用winform,有些数据需要本地存储,所以想到了使用sqlite这个文件数据库.在使用Nuget安装sqlite的时候,发现会将Ef也安装上了,所以想着使用EF进行数据的操作吧,所以这就 ...

  7. PHP 7 来了,PHP 6 去哪儿了?

    PHP7来了,那么PHP6去哪儿了呢? PHP7简介 PHP7是PHP编程语言全新的一个版本,主要在性能方面获得了极大的提升.官方的文档显示,PHP7可以达到PHP5.x版本两倍的性能.同时还 对PH ...

  8. 基于设备树的TQ2440 DMA学习(1)—— 芯片手册

    作者 彭东林pengdonglin137@163.com 平台 TQ2440内核Linux4.9 概述 一直想抽时间学习一下DMA驱动,今天就以S3C2440为例,这款芯片的DMA控制器足够简单,也比 ...

  9. Android:intent的基础

    只有一个活动的应用也太简单了吧?没错,你的追求应该更高一点.不管你想创建多少 个活动,方法都和上一节中介绍的是一样的.唯一的问题在于,你在启动器中点击应用的图 标只会进入到该应用的主活动,那么怎样才能 ...

  10. Unity5中新的Shader体系简析

    一.Unity5中新的Shader体系简析 Unity5和之前的书写模式有了一定的改变.Unity5时代的Shader Reference官方文档也进一步地变得丰满. 主要需要了解到的是,在原来的Un ...