Valid Number leetcode java

愛做飯的小瑩子發表於2014-08-06

題目

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;  
10         }  
11     }

 

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

 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)=='-'){    // 忽略符號位
10             i++;
11         }
12         int j = s.length()-1;
13         while(s.charAt(j) == ' '){    // 移除字尾whitespace
14             j--;
15         }
16         if(i <= j){
17             s = s.substring(i, j+1);
18         }else{
19             return false;
20         }
21         
22         int dot = -1;    // 記錄點的位置
23         int ee = -1;    // 記錄e的位置
24         for(i=0; i<s.length(); i++){
25             if(dot==-1 && s.charAt(i)=='.'){
26                 dot = i;
27             }else if(ee==-1 && s.charAt(i)=='e'){
28                 ee = i;
29                 if(i+1<s.length() && (s.charAt(i+1)=='-' || s.charAt(i+1)=='+')){
30                     i++;
31                 }
32             }else{
33                 if(Character.isDigit(s.charAt(i))){
34                     continue;
35                 }else{
36                     return false;
37                 }
38             }
39         }
40         
41         //xxx.xxexx
42         String startStr, midStr, lastStr;
43         if(dot==-1 && ee==-1){    //xxx  
44             startStr = s;    // xxx
45             if(startStr.length()<1){
46                 return false;
47             }
48         }else if(dot!=-1 && ee==-1){    //xxx.yyy  
49             startStr = s.substring(0, dot);    // xxx
50             midStr = s.substring(dot+1);        // yyy
51             if(startStr.length()<1 && midStr.length()<1){
52                 return false;
53             }
54         }else if(dot==-1 && ee!=-1){    // xxxeyyy
55             startStr = s.substring(0, ee);    // xxx
56             if(startStr.length()<1){
57                 return false;
58             }
59             if(ee+1<s.length() && (s.charAt(ee+1)=='-' || s.charAt(ee+1)=='+')){    // xxxe-zz
60                 lastStr = s.substring(ee+2);    // zz
61             }else{
62                 lastStr = s.substring(ee+1);
63             }
64             if(lastStr.length() < 1){
65                 return false;
66             }
67         }else{        //xxx.yyezz
68             if(dot>ee){        // 位置不對
69                 return false;
70             }
71             startStr = s.substring(0, dot);    // xxx
72             midStr = s.substring(dot+1, ee);    // yy
73             if(startStr.length()<1 && midStr.length()<1){
74                 return false;
75             }
76             if(ee+1<s.length() && (s.charAt(ee+1)=='-' || s.charAt(ee+1)=='+')){
77                 lastStr = s.substring(ee+2);    // zz
78             }else{
79                 lastStr = s.substring(ee+1);
80             }
81             if(lastStr.length() < 1){
82                 return false;
83             }
84         }
85         return true;
86     }

 

相關文章