my output have strange character


  • 0
    F

    when i submit,the result is:
    Input:"ABC" 2
    Output:"ACB��" (my output always have extra character)
    Expected:"ACB"

    but it is ok when i run testcase

    here is my code

    char* convert(char* s, int numRows) {
        int size = 2*numRows - 2;
        int m = 0;
        int total_len = strlen(s);
        if(numRows == 1 || total_len <= numRows)
            return s;
        char* p = (char*)malloc(sizeof(char) * total_len);
        memset(p,'\0',total_len);
        
        int tmp_num = 0;
        for(int n = 1;n <= numRows; n++)
        {
            int cur = n;
            int l = 2*n - 2;
            p[m++] = s[cur - 1];
            while(cur <= total_len)
            {
                if(l != 0 && n != numRows && cur + size - l <= total_len)
                    p[m++] = s[cur + size - l - 1];
                if(cur + size <= total_len)
                    p[m++] = s[cur + size - 1];
                cur += size;
            }
            
        }
        return p;
    }
    

Log in to reply
 

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