程序媛詹妮弗
终身学习

Validate if a given string can be interpreted as a decimal number.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
" -90e3   " => true
" 1e" => false
"e3" => false
" 6e-1" => true
" 99e2.5 " => false
"53.5e93" => true
" --6 " => false
"-+3" => false
"95a54e53" => false

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:

  • Numbers 0-9
  • Exponent - "e"
  • Positive/negative sign - "+"/"-"
  • Decimal point - "."

Of course, the context of these characters also matters in the input.

 

思路

There is no complex algorithem, just checking each char from input String

 

代码

 1 // {前缀空格}{浮点数}{e}{正数}{后缀空格}
 2 class Solution {
 3     public boolean isNumber(String s) {
 4         int len = s.length();
 5         int i = 0;
 6         int right = len - 1;
 7         
 8         // delete white spaces on both sides
 9         while (i <= right && Character.isWhitespace(s.charAt(i))) i++;
10         if (i > len - 1) return false;
11         while (i <= right && Character.isWhitespace(s.charAt(right))) right--;
12         
13         // check +/- sign
14         if (s.charAt(i) == '+' || s.charAt(i) == '-') i++;
15         
16         boolean num = false; // is a digit
17         boolean dot = false; // is a '.'
18         boolean exp = false; // is a 'e'
19         
20         while (i <= right) {
21             char c = s.charAt(i);
22             // first char should be digit
23             if (Character.isDigit(c)) {
24                 num = true;
25             }
26             else if (c == '.') {
27                 // exp and dot cannot before '.'
28                 if(exp || dot) return false;
29                 dot = true;
30             }
31             else if (c == 'e') {
32                 // only one 'e'can exist 
33                 // 'e' should after num
34                 if(exp || num == false) return false;
35                 exp = true;
36                 // after 'e' should exist num, so set num as default false
37                 num = false;
38             }
39             else if (c == '+' || c == '-') {
40                 if (s.charAt(i - 1) != 'e') return false;
41             }
42             else {
43                 return false;
44             }
45             i++;
46         }
47         return num;
48     }
49 }

 

变种之简化版本的Valid Number : checking +/- numbers including decimal numbers

需要跟面试官confirm

以下哪些算valid,若以下test case中,蓝色字体是valid的

123
-123
123.123
-123.123
.123
-.123
123.
123-.

那么:

1.  Decimal point 前面必须是数字

2. Decimal point 后面必须是数字

3. 整个valid number最后是以数字结尾

 1 public boolean isNumber(String s) {
 2         int len = s.length();
 3         int i = 0;
 4         int right = len - 1;
 5 
 6         // delete white spaces on both sides
 7         while (i <= right && Character.isWhitespace(s.charAt(i))) i++;
 8         if (i > len - 1) return false;
 9         while (i <= right && Character.isWhitespace(s.charAt(right))) right--;
10 
11         // check +/- sign
12         if (s.charAt(i) == '+' || s.charAt(i) == '-') i++;
13 
14         boolean num = false; // is a digit
15         boolean dot = false; // is a '.'
16         
17         while (i <= right) {
18             char c = s.charAt(i);
19             if (Character.isDigit(c)) {
20                 num = true;
21             }
22             else if (c == '.') {
23                 // (1) .. two dots  (2) no number before dot (3) - before dot
24                 if( dot || num == false || s.charAt(i-1) == '-') return false;
25                 dot = true;
26                 // check whether there is number after decimal point
27                 num = false;
28             }
29             else {
30                 return false;
31             }
32             i++;
33         }
34         return num;
35     }

 

posted on 2018-10-18 15:07  程序媛詹妮弗  阅读(296)  评论(0编辑  收藏  举报