The big question is not difficult, the main consideration several aspects:
1. The sign bit, can occur only once, only appear in front of the digital
2. decimal point, can appear only once, it must appear at the end of numbers
3. After the index e or E, can occur before the digital or digital
4. The necessary condition is called Digital must contain a number (e count does not count and E may be set according to the subject, this time is not assumed)
. 1 class Solution { 2 public : . 3 BOOL that isNumeric ( char * STR) . 4 { . 5 IF (STR == NULL || STR [ 0 ] == ' \ 0 ' ) . 6 return to false ; . 7 int SIG = 0 ; // Analyzing the number of symbol bits . 8 int NUM = 0 ; // number of digits . 9 int Point = 0 ; // number of decimal 10 . 11 @Analyzing the first space, skip space 12 is the while (* STR == ' ' ) 13 is STR ++ ; 14 15 // determines the symbol bit 16 the while (* STR == ' + ' || * STR == ' - ' ) . 17 { 18 is ++ SIG ; . 19 STR ++ ; 20 is IF (SIG> . 1 ) 21 is { 22 is return to false ; 23 is } 24 } 25 // number of digits is determined 26 is 27 the while (* STR> = ' 0 ' && * STR <= ' . 9 ' ) 28 { 29 NUM ++ ; 30 STR ++ ; 31 is } 32 the while (* STR == ' . ' ) / / what I wrote, that strange results can not pass the test case, see below 33 // the while (NUM && * str == '.') 34 // in front of the decimal point must have digital is digital it's not a very simple truth it 35 { 36 Point ++ ; 37 str++; 38 if (sig > 1) 39 { 40 return false; 41 } 42 } 43 while (*str >= '0' && *str <= '9') 44 { 45 num++; 46 str++; 47 } 48 if (*str == 'e' || *str == 'E' ) 49 { 50 str ++ ; 51 { 52 themselves = 0 ; 53 while (* str == ' + ' || * str == ' - ' ) 54 { 55 itself ++ ; 56 str ++ ; 57 IF (at> 1 ) 58 { 59 return false ; 60 } 61 } 62 // number determination number 63 is NUM = 0 ; 64 the while (* STR> = ' 0 ' && * STR <= ' . 9 ' ) 65 { 66 NUM ++ ; 67 STR ++ ; 68 } 69 IF (NUM = = 0 ) // rear e must digital 70 return to false ; 71 is IF (STR == *' \ 0 ' ) 72 return to true ; 73 is the else 74 { 75 return to false ; 76 } 77 } 78 } 79 the else 80 IF (STR * == ' \ 0 ' && NUM) // must include a digital number is 81 return to true ; 82 the else 83 { 84 return to false ; 85 } 86 87 } 88 89 };
There is a test case, "-.123" sample output turned out to be true?
Is that right figures, cattle passenger network in the end how you think, not in front of the decimal point can it?