solution in c


  • 0
    O

    //is number

    #include <stdio.h>
    #include <stdlib.h>

    typedef struct _node node;
    struct _node{
    int val;
    char other;
    node next;
    };
    node
    init();
    void initNodes();
    int isNumber(char* num);

    node *head;

    void initNodes(){
    head = init();
    head->next = init();
    head->val = 0.0; //just a sample value not necessary
    head->next = head;
    }

    node* init(){
    node* val = (node *) malloc(sizeof(node));
    return val;
    }

    int isNumber(char* num){
    //parser
    initNodes();
    int i = 0;
    while(num[i] !=0x0){
    printf("parser %c\n", num[i]);

    	if(num[i] >='0' && num[i] <= '9'){
    		node *newn = init();
    		newn->val = num[i]-'0';
    		newn->next = head->next;
    		head->next = newn;
    	}
    	else{
    		node *newn = init();
    		newn->other = num[i];
    		newn->val =-1;
    		newn->next = head->next;
    		head->next = newn;
    	}
    	i++;
    }
    //going through each node
    node* retri = init();
    retri = head->next;
    while(retri != head){
    	//checker
    	printf("retreive value is %d\n",retri->val);
    	if(retri->val >=0 && retri->val <= 9 ){
    		
    		retri = retri->next;
    	}
    	else if(retri->val == -1){
    		//check if point or e
    		if(retri->other =='.' || retri->other=='e' || retri->other=='E'){
    			retri = retri->next;
    		}
    		else{
    			return 0;
    		}
    	}
    	else{
    		return 0;
    	}
    }
    return 1;
    

    }

    int main(void){
    printf("is it a number: %s\n",isNumber("2e10") ? "true":"false");
    return 0;
    }


Log in to reply
 

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