LeetCode 6. Z 字形变换(C语言详解)

一、题目描述

将一个给定字符串 s 根据给定的行数 numRows ,以 从上往下、从左到右 进行 Z 字形排列。

例如输入字符串为:

复制代码
PAYPALISHIRING

numRows = 3 时,排列如下:

复制代码
P   A   H   N
A P L S I I G
Y   I   R

然后按 逐行读取 的顺序输出:

复制代码
PAHNAPLSIIGYIR

示例:

示例 1

复制代码
输入:s = "PAYPALISHIRING", numRows = 3
输出:"PAHNAPLSIIGYIR"

示例 2

复制代码
输入:s = "PAYPALISHIRING", numRows = 4
输出:"PINALSIGYAHRPI"

排列方式:

复制代码
P     I    N
A   L S  I G
Y A   H R
P     I

二、解题思路

Z 字形的核心规律是:

字符在 行之间上下移动

例如 numRows = 4 时,行变化如下:

复制代码
0 → 1 → 2 → 3 → 2 → 1 → 0 → 1 ...

也就是说:

  • 第一行 时开始 向下

  • 最后一行 时开始 向上

因此我们只需要:

  1. 创建 numRows 个字符串(或字符数组)

  2. 按照 上下移动规则 把字符加入对应行

  3. 最后 按行拼接 即可

示意:

复制代码
row0: P     I    N
row1: A   L S  I G
row2: Y A   H R
row3: P     I

最终结果:

复制代码
row0 + row1 + row2 + row3

三、特殊情况

有一个非常重要的特殊情况:

复制代码
numRows == 1

例如:

复制代码
s = "ABC"
numRows = 1

Z 字形无法形成,直接返回原字符串即可。


四、C语言代码实现

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* convert(char* s, int numRows) {
    if(numRows == 1) return strdup(s);

    int len = strlen(s);

    // 创建每一行
    char **rows = (char**)malloc(sizeof(char*) * numRows);
    int *rowLen = (int*)calloc(numRows, sizeof(int));

    for(int i = 0; i < numRows; i++){
        rows[i] = (char*)malloc(sizeof(char) * (len + 1));
    }

    int curRow = 0;
    int goingDown = 1;

    for(int i = 0; i < len; i++){
        rows[curRow][rowLen[curRow]++] = s[i];

        if(curRow == 0)
            goingDown = 1;
        else if(curRow == numRows - 1)
            goingDown = 0;

        curRow += goingDown ? 1 : -1;
    }

    // 拼接结果
    char *res = (char*)malloc(sizeof(char) * (len + 1));
    int pos = 0;

    for(int i = 0; i < numRows; i++){
        for(int j = 0; j < rowLen[i]; j++){
            res[pos++] = rows[i][j];
        }
        free(rows[i]);
    }

    res[pos] = '\0';

    free(rows);
    free(rowLen);

    return res;
}

五、复杂度分析

时间复杂度

复制代码
O(n)

每个字符只遍历一次。


空间复杂度

复制代码
O(n)

需要额外空间存储每一行的字符。


六、思路总结

本题的关键在于理解 Z 字形的行变化规律

复制代码
向下 → 到底 → 向上 → 到顶 → 向下

即:

复制代码
0 → 1 → 2 → ... → numRows-1 → numRows-2 → ... → 1 → 0

利用一个 方向变量 goingDown 控制移动即可。

解题步骤:

  1. 创建 numRows

  2. 按 Z 字形顺序填充字符

  3. 最后按行拼接输出

相关推荐
sinat_2869451914 小时前
harness engineering
人工智能·算法·chatgpt
少许极端14 小时前
算法奇妙屋(四十三)-贪心算法学习之路10
学习·算法·贪心算法
samroom14 小时前
【鸿蒙应用开发 Dev ECO Studio 5.0版本】从0到1!从无到有!最全!计算器------按钮动画、滑动退格、中缀表达式转后缀表达式、UI设计
数据结构·ui·华为·typescript·harmonyos·鸿蒙
算法鑫探14 小时前
10个数下标排序:最大值、最小值与平均值(下)
c语言·数据结构·算法·排序算法·新人首发
样例过了就是过了14 小时前
LeetCode热题100 爬楼梯
c++·算法·leetcode·动态规划
IronMurphy14 小时前
【算法三十七】51. N 皇后
算法·深度优先
DoUfp0bgq14 小时前
从直觉到算法:贝叶斯思维的技术底层与工程实现
算法
少司府14 小时前
C++基础入门:类和对象(中)
c语言·开发语言·c++·类和对象·运算符重载·默认成员函数
ThisIsMirror14 小时前
leetcode 452 Arrays.sort()排序整数溢出、Integer.compare(a[1], b[1])成功的问题
算法·leetcode
王老师青少年编程14 小时前
csp信奥赛c++之状压枚举
数据结构·c++·算法·csp·信奥赛·csp-s·状压枚举