# Valid Number

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.

思路：

去掉 手尾的空格

用i trace 现在都到哪了，根据正确 的格式来判断,注意trim后加一个空格

```java
class Solution {
    public boolean isNumber(String s) {
        
        if(s== null || s.length() == 0){
            return false;
        }
        
        s = s.trim()+" ";//给最后的for循环 i < len 后 i == len 判断留的
        
        int i  = 0, len = s.length()-1;
        
        char[] charArr = s.toCharArray();
            
        if(charArr[i] == '+' || charArr[i] == '-'){
            i++;
        }
        
        int isDigit = 0, isPoint = 0;
        
        while(Character.isDigit(charArr[i]) || charArr[i] == '.'){
            if(Character.isDigit(charArr[i])){
                isDigit++;
            }
            
            else if(charArr[i] == '.'){
                isPoint++;
            }
            
            i++;
        }
        
        if(isDigit <= 0 || isPoint >1){
            return false;
        }
        
        if(charArr[i] == 'e'){
            i++;
            if(charArr[i] == '+' || charArr[i] == '-'){
                i++;
            }
            if(i == len){
                return false;
            }
            
            for(;i<len;i++){
                if(!Character.isDigit(charArr[i])){
                    return false;
                }
            }
            
            
        }
        
        return i == len;
        
        
    }
}
```

没有e的版本

```java
// 00 false
// 5. 5- false
// -0 true 如果这个test要求是false, 只需要在最前面加一个s.equals("-0")的判断就行

public class Solution {
    public boolean isNumber(String s) {
        s = s.trim();
                boolean pointSeen = false, numberSeen = false;
        int len = s.length();
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if (!numberSeen && c=='0' && i<len-1 && s.charAt(i+1)=='0') {
                return false;
            }
            if (c >= '0' && c <= '9') {
                numberSeen = true;
            } else if (c == '.') {
                if (pointSeen || i == len - 1) {
                    return false;
                }
                pointSeen = true;
            } else if (c == '+' || c == '-') {
                if (i != 0) {
                    return false;
                }
            } else {
                return false;
            }
        }
        return numberSeen;
    }
}
```
