ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

刷题日记(6. Z 字形变换)

2021-10-28 23:59:42  阅读:182  来源: 互联网

标签:tmp bPosition return 字形 Bottom int NULL 日记 刷题


6. Z 字形变换

#define ElementType char
struct MNode;
typedef struct MNode* PtrToMNode;
typedef PtrToMNode Vector;
typedef PtrToMNode rPosition;
typedef PtrToMNode bPosition;

void bInsert(bPosition B);
void PrintVector(Vector V);
Vector CreateVector(int row);
int isLast(rPosition P);
void Push(ElementType X, bPosition B);

struct MNode {
    ElementType Element;
    rPosition Right;
    bPosition Bottom;
};

Vector CreateVector(int row) {
    if (row < 0) {
        printf("Invalid row!");
        return NULL;
    }
    Vector V = calloc(1, sizeof(struct MNode));
    if ( V == NULL ) {
        printf("Out of space!");
        return NULL;
    }
    V->Element = 0;
    V->Right = NULL;
    V->Bottom = NULL;
    int i = 1;
    while ( i < row ) {
        bInsert(V);
        ++i;
    }
    return V;
}

void bInsert(bPosition B) {
    bPosition tmp;
    tmp = calloc(1, sizeof(struct MNode));
    if (tmp == NULL) {
        printf("out of space!");
        return;
    }
    tmp->Element = 0;
    tmp->Bottom = B->Bottom;
    B->Bottom = tmp;
}

bPosition bFind(int Index, Vector V) {
    if (Index < 0) {
        printf("Invalid Index!");
        return NULL;
    }
    bPosition B;
    B = V;
    int i = 0;
    while ( B->Bottom != NULL && i < Index ) {
        B = B->Bottom;
        ++i;
    }
    if ( i == Index ) {
        return B;
    }
    while ( i < Index ) {
        bInsert(B);
        ++i;
        B = B->Bottom;
    }
    return B;
}

int isLast(rPosition P) {
    return P->Right == NULL;
}

void Push(ElementType X, bPosition B) {
    rPosition P;
    P = B;
    while ( !isLast(P) ) {
        P = P->Right;
    }
    rPosition tmp = calloc(1, sizeof(struct MNode));
    if (tmp == NULL) {
        printf("out of space");
        return;
    }
    tmp->Element = X;
    tmp->Right = P->Right;
    P->Right = tmp;
}


char * convert(char * s, int numRows){
 Vector V = CreateVector(numRows);
    int goingDown = 1;

    int i = 0;
    int currRow = 0;
    while (s[i]) {
        bPosition B = bFind(currRow, V);
        Push(s[i], B);
        if (goingDown) {
            currRow++;
        } else {
            currRow--;
        }
        if (currRow == numRows) {
            goingDown = 0;
            currRow -= 2;
        }
        if (currRow < 0) {
            goingDown = 1;
            currRow += 2;
        }
        ++i;
    }
    char *res = calloc(i+1, sizeof(char));
    rPosition R;
    bPosition tB;
    tB = V;
    int ii = 0;
    while ( tB ) {
        R = tB->Right;
        while ( R ) {
            res[ii++] = R->Element;
            R = R->Right;
        }
        tB = tB->Bottom;
    }
    return res;
}

解法是看的官方解答,但是C语言没有Vector,自己写了个链表简单实现一下,但是运行速度和内存消耗都还有很大的优化空间。

标签:tmp,bPosition,return,字形,Bottom,int,NULL,日记,刷题
来源: https://blog.csdn.net/qq_40302228/article/details/121025965

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有