c 9ms, really


  • 0
    S
    char* convert(char* s, int numRows) 
    {
        size_t l;
        int i, j, r, m, n, p, q;
        char *result;
        if (numRows == 1)
        {
            return s;
        }
        l = strlen(s);
        if (l < 3)
        {
            return s;
        }
        result = (char *)alloca(sizeof(char) * (l));
        p = numRows - 1;
        m = p + p;
        r = 0;
        i = 0;
        while(i < l)
        {
            result[r++] = s[i];
            i += m;
        }
        for (i = 1; i < p; ++i)
        {
            j = i;
            n = j + j;
            q = m - n;
            while (true)
            {
                if (j >= l)
                {
                    break;
                }
                result[r++] = s[j];
                j += q;
                if (j >= l)
                {
                    break;
                }
                result[r++] = s[j];
                j += n;
            }
        }
        while(i < l)
        {
            result[r++] = s[i];
            i += m;
        }
        memcpy(s, result, l);
        return s;
    }

Log in to reply
 

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