1. 程式人生 > 實用技巧 >[LeetCode] 65. Valid Number

[LeetCode] 65. Valid Number

Validate if a given string can be interpreted asa 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.

Update (2015-02-10):
The signature of theC++function had been updated. If you still see your function signature accepts aconst char *argument, please click the reload button to reset your code definition.

有效數字。題意是給你一個input字串,請你判斷他是否為有效數字。題目沒有給出非常具體的有效數字的定義,但是給了一些例子幫你理解什麼叫有效什麼叫無效。

思路只能是從例子下手了。注意到這麼幾點

  • 首先input字串裡面一定需要有數字
  • 之後需要判斷是否有e,如果有e,則必須先看到數字才行,否則就是錯的
  • 如果碰到小數點,但是是在e之後或者已經出現過小數點了,就是錯的
  • 如果遇到加減號,看一下是不是在input的首位或者在不在e之後的那一位上,如果不滿足則是錯的
  • 最後判斷有沒有遇到數字

照著這個思路,程式碼就不難寫了。但是有的case,討論區很多同學都不認同,我也在這裡分享出來。

test(1,"123",true); test(2,"123",true); test(3,"0",true); test(4,"0123",true);//Cannotagree test(5,"00",true);//Cannotagree test(6,"-10",true); test(7,"-0",true); test(8,"123.5",true); test(9,"123.000000",true); test(10,"-500.777",true); test(11,"0.0000001",true); test(12,"0.00000",true); test(13,"0.",true);//Cannotbemoredisagree!!! test(14,"00.5",true);//Stronglycannotagree test(15,"123e1",true); test(16,"1.23e10",true); test(17,"0.5e-10",true); test(18,"1.0e4.5",false); test(19,"0.5e04",true); test(20,"123",false); test(21,"1a3",false); test(22,"",false); test(23,"",false); test(24,null,false); test(25,".1",true);//Ok,ifyousayso test(26,".",false); test(27,"2e0",true);//Really?! test(28,"+.8",true); test(29,"005047e+6",true);//Damn==|||

時間O(n)

空間O(1)

Java實現

 1 class Solution {
 2     public boolean isNumber(String s) {
 3         boolean eSeen = false;
 4         boolean numSeen = false;
 5         boolean dotSeen = false;
 6         s = s.trim();
 7         for (int i = 0; i < s.length(); i++) {
 8             char c = s.charAt(i);
 9             if (Character.isDigit(c)) {
10                 numSeen = true;
11             } else if (c == 'e') {
12                 if (eSeen || !numSeen) {
13                     return false;
14                 }
15                 eSeen = true;
16                 numSeen = false;
17             } else if (c == '.') {
18                 if (eSeen || dotSeen) {
19                     return false;
20                 }
21                 dotSeen = true;
22             } else if (c == '-' || c == '+') {
23                 if (i != 0 && s.charAt(i - 1) != 'e') {
24                     return false;
25                 }
26             } else {
27                 return false;
28             }
29         }
30         return numSeen;
31     }
32 }

LeetCode 題目總結