Offer surface prove safety questions 20 (java version): a string value

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/littlehaes/article/details/91411234

welcome to my blog

Offer surface prove safety questions 20 (java version): a string value

To master version of the non-regular expression

Title Description

Implement function used to determine whether a string represents a value (including integer and fractional). For example, the string "+100", "5e2", "- 123", "3.1416" and "-1E-16" shows the value. However, "12e", "1a3.14", "1.2.3", "± 5" and "12e + 4.3" not.

notes

  • Regular expression \ d represent numbers, the \ d when placed in java string, \ represents the escape character instead of \ meaning itself, if you want \ express the meaning of their own, need to be written \\
  • "? [E | E] [±]" indicates the first number is an e or E, the second number is + or - to be clear or not there is: a [] describe a position, two [] is described. experience two positions [e | E]. [±] and | difference [e E ±] of
  • There e | E must be followed by the integer can take sign
  • The very beginning of the card using regular expressions in a position, because I think a decimal point out alone, so there are a integer and fractional parts at the same time does not appear, only a decimal point question, why we should not find decimal and decimal point part of the value considered together as a whole, because I did not think of another case: the decimal point does not appear, integer and fractional parts while after I think this case will feel the decimal point should be considered as a whole and fractional part.
  • The value of the fractional part of the decimal point, and considered as a whole with
  • But the use of regular expressions a little problem that e12 will determine this is true, we do not use regular expressions
  • Specific examples: - 123, this is true
  • Special case: 1a3.14, this is false
public class Solution {
    public static boolean isNumeric(char[] str) {
        String s = String.valueOf(str);
        boolean result = s.matches("[+-]?\\d*(\\.\\d*)?([e|E][+-]?\\d+)?"); //把小数点和小数作为整体考虑: 一起出现或者一起不出现
        return result;
    }
}

No regular expression version of the code

Thinking

  • Write two functions: determining a signed integer, unsigned integer determined
  • The number that appears in line with two modes: integer part comprising A [e | EC]; does not contain the integer portion [B] [e | EC] [[B].].
  • Analyzing write code ABC

notes

  • Be sure to pay attention to: result || must be on the right, on the left because if not on the left, while the result is true, then it will not execute the right || a statement.
  • result = scanUnsignedInteger(str) || result;
public class Solution {
    private int index = 0;
    public boolean isNumeric(char[] str) {
        // input check
        if(str.length < 1)
            return false;
        // execute
        boolean result = scanInteger(str);
        if(index < str.length && str[index] == '.'){
            index++;
            // 下面这句表示三种情况:正数部分和小数部分都存在; 整数部分存在,小数部分不存在; 整数部分不存在,小数部分存在
            // 不允许出现正数和小数都不出现的情况
            // 务必留意: result必须在||右边, 不能放在左边. 因为如果放在左边,同时result又是true, 那么就不会执行||右边的语句了
            result = scanUnsignedInteger(str) || result;
        }
        if(index < str.length && (str[index] == 'e' || str[index] == 'E')){
            index++;
            result = result && scanInteger(str);
        }
        // 不仅要返回result, 还要确保已经判断完每个char
        return result && (index == str.length);
    }

    public boolean scanInteger(char[] str){
        if(index < str.length && (str[index] == '+' || str[index] == '-'))
            index++;
        return scanUnsignedInteger(str);
    }
    public boolean scanUnsignedInteger(char[]str){
        int start = index;
        while(index < str.length && str[index] >= '0' && str[index] <= '9'){
            index++;
        }
        // index比start大, 说明扫描的部分有数字, 返回ture
        // index等于start, 说明扫描的部分没有数字, 返回false
        return start < index;
    }
}

Guess you like

Origin blog.csdn.net/littlehaes/article/details/91411234