[The sword refers to the offer brushing the question] 31. A string representing a numerical value (ordinary question)

Type of question: common question

No special algorithm is used, just a simple classification discussion

Category discussion

  1. First go to the spaces at the beginning and end
  2. If it starts with +, -, it will be removed to get a new string s
  3. Returns false if s is empty, or if s is a single point
  4. Start the loop, the loop variable is i, set dot and e to record the number of occurrences of e/E respectively
    1. If the i-th character is a number, do nothing;
    2. If the i-th character is ., then add dot to one, and judge whether dot is greater than 1, whether e is greater than 0, and if so, return false
    3. If the i-th character is e/E, then e++ determines whether the front and back of e are integers. Returns false if i equals 0, or i equals length minus 1, or i-1 is a point
    4. Otherwise, return false
  5. Returns true if the loop does not return false after the end of the loop.

topic

Please implement a function to determine whether a string represents a numeric value (including integers and decimals).

For example, the strings "+100", "5e2", "-123", "3.1416" and "-1E-16" all represent numeric values.

But neither "12e", "1a3.14", "1.2.3", "±5" nor "12e+4.3".

Notice:

小数可以没有整数部分,例如.123等于0.123;
小数点后面可以没有数字,例如233.等于233.0;
小数点前面和后面可以有数字,例如233.666;
当e或E前面没有数字时,整个字符串不能表示数字,例如.e1、e1;
当e或E后面没有整数时,整个字符串不能表示数字,例如12e、12e+5.4;

Example:

Input: "0"

output: true

java solution

class Solution {
    
    
    public boolean isNumber(String s) {
    
    
        int star = 0, end = s.length() - 1;
        while (star <= end && s.charAt(star) == ' ') star++;
        while (star <= end && s.charAt(end) == ' ') end--;
        
        if (star > end) return false;
        
        s = s.substring(star, end - star + 1);
        
        // System.out.println(s);
        
        if (s.charAt(0) == '+' || s.charAt(0) == '-') s = s.substring(1);
        
        // 去掉单个的 '+', '-', '.'
        if (s.length() == 0 || (s.charAt(0) == '.' && s.length() == 1)) return false;
        
        int dot = 0, e = 0;  // 记录 '.' 和 'e/E' 的出现次数
        for (int i = 0; i < s.length(); i++) {
    
    
            if (s.charAt(i) >= '0' && s.charAt(i) <= '9');  // 如果是数字,则什么也不做
            else if (s.charAt(i) == '.') {
    
    
                // 当前字符是 '.'
                dot++;
                if (dot > 1 || e > 0)  // 如果出现了多个点,或者点之前出现了e
                    return false;
            }
            else if (s.charAt(i) == 'e' || s.charAt(i) == 'E')  {
    
    
                // 当前字符为e、E
                // e前面必须有整数, e后面必须有整数
                e++;
                if (i == 0 || i == s.length() - 1 || e > 1 || s.charAt(i-1) == '.') return false;
                if (s.charAt(i + 1) == '+' || s.charAt(i+1) == '-') {
    
    
                    if (i + 2 == s.length()) return false;
                    i++;
                }
            }
            else return false;
        }
        return true;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326129250&siteId=291194637