Title:
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.
Test Instructions:
Verifies whether the given string represents a number.
Some examples:
"0"=true
" 0.1 "=true
"abc"=false
"1 a"=false
"2e10"=true
注意: 问题的描述不是特别的明确,在实现算法之前,你需要收集所有的要求。
Algorithm Analysis:
When the first brush Leetcode, the problem is the lowest pass rate. After AC only found that the algorithm is not particularly complex, is the sample of a variety of tests, unexpected AH. It is impossible to debug without committing. Algorithm does not
Complex, as long as you can think of all the circumstances. AC is really for AC.
This is certainly not the attitude that the algorithmic person should have (embarrassed). The correct routine seems to be to analyze the problem first, then solve the problem oh.
Reference http://pisxw.com/algorithm/Valid-Number.html
The basic rule is to follow the scientific notation, so the special characters that appear are as follows: Sign bit ' + ', '-', decimal point '. ', and ' e ' and ' E ', all that remains is the number 0-9, and the other character returns false if it appears to be an illegal character. Where numeric characters appear are OK, we mainly consider the case of several special characters.
For the decimal point to occur, we need to satisfy these conditions:
1. There must be no decimal point or ' e ' and ' e ' in front;
2. The former is a number (not the first) or the latter if the number (not the last one).
For the sign occurrence, the condition should be met:
1. Must be first or after ' E ' and ' e ';
2. The latter one if the number.
For the ' e ' and ' e ' cases, be satisfied:
1. There can be no ' e ' and ' e ' in front;
2. Cannot be the first (no digital scientific count is meaningless) or the last (no number will be used to write the index).
AC Code:
public class Solution {public Boolean isnumber (String s) {if (s==null) return false; s = S.trim (); if (S.length () ==0) return false; Boolean dotflag = false; Boolean eflag = false; for (int i=0;i<s.length (), i++) {switch (S.charat (i)) {case '. ': if (Dotflag | | eflag | | ((i==0| |! (S.charat (i-1) >= ' 0 ' &&s.charat (i-1) <= ' 9 ')) && (I==s.length () -1| |! (S.charat (i+1) >= ' 0 ' &&s.charat (i+1) <= ' 9 ')))) return false; Dotflag = true; Break Case ' + ': Case '-': if ((I>0 && (S.charat (i-1)! = ' E ' && s.charat (i-1)! = ' E ')) | | (I==s.length ()-1 | |! (S.charat (i+1) >= ' 0 ' &&s.charat (i+1) <= ' 9 ' | | S.charat (i+1) = = '. '))) return false; Break Case ' E ': Case ' E ': if (Eflag | | i==s.length ()-1 | | i==0) return f Alse; Eflag = true; Break Case ' 0 ': Case ' 1 ': Case ' 2 ': Case ' 3 ': Case ' 4 ': Case ' 5 ': Case ' 6 ': Case ' 7 ': Case ' 8 ': Case ' 9 ': Break Default:return false; }} return true; }}
Here is also the first time you put the code of the AC, all records
public class Solution {public Boolean isnumber (String s) {int dotnum=0;int expnum=0;int signnum=0; String tems= ""; String nosigns= ""; S=s.trim (); if (s==null| | S.length () ==0) return false;for (int i=0;i<s.length (); i++) {if (S.charat (i) >= ' 0 ' &&s.charat (i) <= ' 9 ') {continue;} else if (S.charat (i) = = '-' &&i==0| | S.charat (i) = = ' + ' &&i==0) {signnum++;if (signnum>1) return false;nosigns=s.substring (1); s= New String ( nosigns); i=-1;} else if (S.charat (i) = = ' E ') {expnum++;if (expnum>1) return false;if (I-1>=0&&i+1<=s.length ()-1) {if ( S.charat (i-1) >= ' 0 ' &&s.charat (i-1) <= ' 9 ' &&s.charat (i+1) >= ' 0 ' &&s.charat (i+1) < = ' 9 ' | | S.charat (i-1) = = '. ' | | (S.charat (i+1) = = '-' &&i+1!=s.length ()-1) | | (S.charat (i+1) = = ' + ' &&i+1!=s.length ()-1)) {if (S.charat (i+1) = = ' + ' | | S.charat (i+1) = = '-') i++;if (!s.substring (i). Contains (".")) Continue;else return false;} Elsereturn false;} Elsereturn false;} else if (S.charat (i) = = '. ') {dotnum++;if (dotnum>1) return False;if (i-1>=0| | I+1<=s.length ()-1) {if (i-1>=0) {if (S.charat (i-1) >= ' 0 ' &&s.charat (i-1) <= ' 9 ') continue;else return false;} if (I+1<=s.length ()) {if (S.charat (i+1) = = ' E ') {if (i-1>=0) {if (S.charat (i-1) >= ' 0 ' &&s.charat (i-1) <= ' 9 ') Continue;else return false;} Elsereturn false;} else if (S.charat (i+1) >= ' 0 ' &&s.charat (i+1) <= ' 9 ') Continue;else return false;}} Elsereturn false;} Elsereturn false;} return true; }}
Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source
[Leetcode] [Java] Valid number