LeetCode 65th title: significant digits (difficult)

LeetCode 65th title: significant digits (difficult)

  • Title: Verify whether a given string can be interpreted as a decimal number. We are interested in the problem statement will be blurred. Before implementing the code, you should think in advance all possible cases. Here is given a valid decimal number may be present in the list of characters: numbers 0-9, Index - "E", the positive / negative sign - "+" / "-", a decimal point - of course, enter the "." the context of these characters is also important.

  • A thought: violent solution, bit by bit, string. In many cases, easier to consider not comprehensive. It has been changed for a long time, tired ah. . .

class Solution {
    public boolean isNumber(String s) {
        int len = s.length();
        int i = 0;
        boolean fuhao = false;
        boolean zhishu = false;
        boolean xiaoshu = false;
        boolean shuzi = false;
        boolean fei = false;
        while(i<len){
            char a = s.charAt(i);
            //其他字符
            if((a>='A' && a<='Z') || (a>='a' && a<='z' && a!='e')){
                return false;
            }else if(a==' '){//空格情况
                if(i==0 && len==1) return false;//只有空格
                while(i<len){
                    a = s.charAt(i);
                    if(a==' '){
                        if(shuzi==false && i==len-1) return false;
                        i++;
                    }else{
                        if(shuzi==true || xiaoshu==true || zhishu==true ||fuhao==true) return false;
                        break;
                    }
                }
                
            }else if(a<='9' && a>='0'){//数字情况
                if(a!='0') fei=true;
                shuzi=true;
                i++;
            }else if(a=='e'){//‘e’情况
                if(zhishu) return false;
                if(shuzi==false || i==len-1) return false;
                zhishu=true;
                shuzi=false;
                fuhao=false;
                xiaoshu =false;
                i++;
            }else if(a=='.'){//小数点情况
                if(shuzi==false && i==len-1) return false;
                if(xiaoshu) return false;
                if(zhishu) return false;
                xiaoshu=true;
                i++; 
            }else if(a=='+' || a=='-'){//正负符号情况
                if(i==len-1) return false;
                if(shuzi==false && fuhao==false && xiaoshu==false){
                    fuhao=true;
                    i++;
                }else{
                    return false;
                }
            }
        }
        return true;
    }
}

Here Insert Picture Description

  • Ideas 2: Using the DFA resolved, probably understand what it meant, it is estimated the next encounter or will not choose this method. Turing annoying. . .
class Solution {
    public int make(char c) {
        switch(c) {
            case ' ': return 0;
            case '+':
            case '-': return 1;
            case '.': return 3;
            case 'e': return 4;
            default:
                if(c >= 48 && c <= 57) return 2;
        }
        return -1;
    }
    
    public boolean isNumber(String s) {
        int state = 0;
        int finals = 0b101101000;
        int[][] transfer = new int[][]{{ 0, 1, 6, 2,-1},
                                       {-1,-1, 6, 2,-1},
                                       {-1,-1, 3,-1,-1},
                                       { 8,-1, 3,-1, 4},
                                       {-1, 7, 5,-1,-1},
                                       { 8,-1, 5,-1,-1},
                                       { 8,-1, 6, 3, 4},
                                       {-1,-1, 5,-1,-1},
                                       { 8,-1,-1,-1,-1}};
        char[] ss = s.toCharArray();
        for(int i=0; i < ss.length; ++i) {
            int id = make(ss[i]);
            if (id < 0) return false;
            state = transfer[state][id];
            if (state < 0) return false;
        }
        return (finals & (1 << state)) > 0;
    }
}

作者:user8973
链接:https://leetcode-cn.com/problems/valid-number/solution/biao-qu-dong-fa-by-user8973/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Here Insert Picture Description

Published 79 original articles · won praise 7 · views 1373

Guess you like

Origin blog.csdn.net/new_whiter/article/details/104314528