ZigZag Conversion - Runtime error "A", 1. Please help, runs fine in my machine


  • 1
    V

    I get a runtime error while the below code is run. It throws

    Last executed input: "A", 1

    Any pointers please ?

    #include <string.h>
    #define MAX 300
    
    char *convert(char *s, int nRows) {
            int str_len = strlen(s);
            int a[MAX] = {0}, k = 0;
            int i, j;
            char ns[MAX];
    
            if (nRows==1) return s;
            if (str_len == 0) return s;
            
            for (i =0; i < MAX; i++) ns[i] = 0;
            
            for (i = 0; i <= nRows -1; i++)
            {
                    ns[k++]=s[i];
                    a[i] = 1;
                    j = -i + (2 * nRows - 2); 
                    while (j <= str_len)
                    {
                            if (a[j] != 1) { 
                                    ns[k++]=s[j];
                                    a[j] = 1;
                            }
                            if ((a[j+2*i] != 1) && (j+2*i)<=str_len) {
                                     ns[k++] = s[j+ 2*i];
                                     a[j + 2*i] = 1; 
                            }
                    j = j + (2 * nRows - 2);
                    }
                    //printf ("\n"); 
            }
            ns[k] = '\0'; 
            return ns;  
    }

  • 1
    V

    There seems to be error in the logic. Few cases are not covered. Also, limited array was an issue. I changed the code, now its working and is accepted.

    int str_len = strlen(s);
    int k = 0, i, j;
    char *ns = (char *) malloc(sizeof(char) * (str_len + 1));
    
    if (nRows==1 || str_len==0) return s;
    if (nRows >= str_len) return s;
    for (i = 0; i < nRows && i < str_len; i++)
    {
        ns[k++]=s[i];
        j = i;
        while (j < str_len)
        {
    		j = j + (2 * nRows - 2);
    		if ((i == 0 || i == nRows -1) && j < str_len)
    		   ns[k++] = s[j];
    		else {
               if ((j - 2 * i) < str_len)
                  ns[k++] = s[j - 2*i];
               if (j < str_len) 
                  ns[k++] = s[j];
    		}
        }
    }
    ns[str_len] = '\0';
    return ns;

  • 1
    V

    A more easily understandable solution..

    string convert(string s, int nRows) {
        int len = s.length();
        if (nRows <= 1 || len == 0)   
           return s;  
      
        string res="";
        for (int i = 0; i < len && i < nRows; ++i)  
        {
            int indx = i;  
            res += s[indx];  
      
            for (int k = 1; indx < len; ++k)  
            {       
                if (i == 0 || i == nRows - 1)  
                {       
                    indx += 2 * nRows - 2;  
                }       
                else    
                {
                    if (k & 0x1)  
                        indx += 2 * (nRows - 1 - i);   
                    else indx += 2 * i;  
                }       
      
                if (indx < len)  
                {
                    res += s[indx];  
                }
            }
        }       
        return res;        
    }

Log in to reply
 

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