My simple C code use a automat,it ac with 6ms


  • 0
    X
    static unsigned char automat_map[12][128] = { 0 };
    bool isNumber(char* s) {
    	//init automat
    	if (!automat_map[0][0]){
    		int i;
    		memset(automat_map, 0x80, 12 * 128);
    		//automat (I) = 0
    		automat_map[0][' '] = 1;
    		automat_map[0]['-'] = automat_map[0]['+'] = 11;
    		for (i = '0'; i <= '9'; i++){
    			automat_map[0][i] = 2;
    		}
    		automat_map[0]['.'] = 10;
    		//automat ( ) = 1
    		automat_map[1][' '] = 1;
    		automat_map[1]['-'] = automat_map[1]['+'] = 11;
    		for (i = '0'; i <= '9'; i++){
    			automat_map[1][i] = 2;
    		}
    		automat_map[1]['.'] = 10;
    		//automat (1) = 2
    		automat_map[2][' '] = 9;
    		automat_map[2][0] = 0xc0;
    		for (i = '0'; i <= '9'; i++){
    			automat_map[2][i] = 2;
    		}
    		automat_map[2]['e'] = automat_map[2]['E'] = 5;
    		automat_map[2]['.'] = 3;
    		//automat (1.) = 3
    		for (i = '0'; i <= '9'; i++){
    			automat_map[3][i] = 4;
    		}
    		automat_map[3][' '] = 9;
    		automat_map[3][0] = 0xc0;
    		automat_map[3]['e'] = automat_map[3]['E'] = 5;
    		//automat (1.1) = 4
    		for (i = '0'; i <= '9'; i++){
    			automat_map[4][i] = 4;
    		}
    		automat_map[4][0] = 0xc0;
    		automat_map[4][' '] = 9;
    		automat_map[4]['e'] = automat_map[4]['E'] = 5;
    		//automat (1E) = 5
    		for (i = '0'; i <= '9'; i++){
    			automat_map[5][i] = 6;
    		}
    		automat_map[5]['+'] = automat_map[5]['-'] = 7;
    		//automat (1E1) = 6
    		for (i = '0'; i <= '9'; i++){
    			automat_map[6][i] = 6;
    		}
    		automat_map[6][0] = 0xc0;
    		automat_map[6][' '] = 9;
    		//automat (1e+-) = 7
    		for (i = '0'; i <= '9'; i++){
    			automat_map[7][i] = 6;
    		}
    		//automat (wait 0) = 9
    		automat_map[9][' '] = 9;
    		automat_map[9][0] = 0xc0;
    		//automat (.) = 10
    		for (i = '0'; i <= '9'; i++){
    			automat_map[10][i] = 4;
    		}
    		//automat (+-) = 11
    		for (i = '0'; i <= '9'; i++){
    			automat_map[11][i] = 2;
    		}
    		automat_map[11]['.'] = 10;
    	}
    
    	unsigned char now_state = 0;
    	while (((now_state = automat_map[now_state][*s++]) & 0x80) == 0){}
    	return now_state & 0x40;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.