Problem-solving ideas: First of all to clarify what is valid number, the topic gives the following example "0" = true "0.1" = True "abc" and false "1 a" and false "2e "+" + True add: "2e" = false "2e+" = False "2e+6" + = true "1 1" = false "+" = False "+5" =&G T True ". 1" + = true "1." = True
Then, a valid number can be decomposed as follows 1, prefix white space [optional] 2, + or-number [optional] 3, Integer or decimal 4, power [optional] if the memory In ' e ', then ' e ' must exist after an integer (can be positive minus) 5, postfix white space [optional]
Pre-condition: I is used to traverse string s,isnumber to record whether part-3 exists, end condition: If I can traverse the string s in order of the above pattern, and isnumber== true, then you can determine that s is a valid number
public class Solution {public Boolean isnumber (String s) {Boolean isnumber = false;
int i = 0;
int n = s.length ();
while (I < n && s.charat (i) = = ") i++;
if (I < n && (S.charat (i) = = ' + ' | | S.charat (i) = = '-')) i++;
while (I < n && character.isdigit (S.charat (i))) {i++;
Isnumber = true; } if (I < n && s.charat (i) = = '. ')
{i + +;
while (I < n && character.isdigit (S.charat (i))) {i++;
Isnumber = true;
}} if (Isnumber && i < n && s.charat (i) = = ' E ') {i + +;
Isnumber = false;
if (I < n && (S.charat (i) = = ' + ' | | S.charat (i) = = '-')) i++;
while (I < n && character.isdigit (S.charat (i))) {i + +;
Isnumber = true;
}
} while (I < n && s.charat (i) = = ") i++;
return Isnumber && i = = N; }
}