C in place algo leetcode has issue with algo


  • 0
    S
    void itoa(int val, char * cStr){
    	sprintf(cStr,"%d",val);
    	
    }
    int evalRPN(char** tokens, int tokensSize) {
        int i=0;
        while(i<tokensSize){
            printf("in:%s\r\n",tokens[i]);
        
            i++;
        }
        i=0;
        while(i<tokensSize){
            printf("in:%s\r\n",tokens[i]);
            if(tokens[i][0]=='+'){
                printf("\r\neval:%s+%s",tokens[i-1],tokens[i-2]);
                itoa(atoi(tokens[i-1])+atoi(tokens[i-2]),tokens[i]);
                printf("=%s\r\n",tokens[i]);
                if(i-3>=0)
                    tokens[i-1]=tokens[i-3];
            }else if(tokens[i][0]=='-'&&tokens[i][1]=='\0'){
                printf("\r\neval:%s-%s",tokens[i-1],tokens[i-2]);
                itoa(atoi(tokens[i-1])-atoi(tokens[i-2]),tokens[i]);
                printf("=%s\r\n",tokens[i]);
                if(i-3>=0)
                    tokens[i-1]=tokens[i-3];
            }else if(tokens[i][0]=='*'){
                printf("\r\neval:%s*%s",tokens[i-1],tokens[i-2]);
                itoa(atoi(tokens[i-1])*atoi(tokens[i-2]),tokens[i]);
                printf("=%s\r\n",tokens[i]);
                if(i-3>=0)
                    tokens[i-1]=tokens[i-3];
            }else if(tokens[i][0]=='/'){
                printf("\r\neval:%s/%s",tokens[i-1],tokens[i-2]);
                 itoa(floor(((float)atoi(tokens[i-2])/(float)atoi(tokens[i-1]))),tokens[i]);
                 printf("=%s\r\n",tokens[i]);
                if(i-3>=0)
                    tokens[i-1]=tokens[i-3];
            }
            i++;
        }
        printf("\r\n%s %d",tokens[i-1],i);
        return atoi(tokens[i-1]);
    }

Log in to reply
 

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