LeetCode Hot100【6. Z 字形变换】

6. Z 字形变换

自己做

分析

解法1:合并多个子串

cpp 复制代码
class Solution {
public:
    string convert(string s, int numRows) {
        vector<string> c(numRows,string());
        int j = 0;
        string save;

        int len = s.size();

        while(j < len){
            for(int i = 0; i < numRows && j < len; i++)            //竖向【从0~numRows-1】
                c[i] += s[j++];
            for(int i = numRows-2; i > 0 && j < len; i--)          //斜向【从numRows-2~1】      
                c[i] += s[j++];
        }

        for(int i = 0; i < numRows; i++)                            //合并
            save += c[i];

        return save;
    }
};

解法2:数学分析

结果比上面的还差

cpp 复制代码
class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows == 1)       //行为1的特殊情况
            return s;

        int len = s.size();
        string save;        //新生成的字符串
        for (int i = 0; i < numRows; i++) {       //逐行累加
            int index = i;      //字符串s在该行对应的起始位置

            if (i == 0) {       //第0行,只有下角
                int j = 2 * numRows - 2 * i - 2;      //下角间隔
                while (index < len) {
                    save.push_back(s[index]);
                    index += j;
                }
            }
            else if (i == numRows - 1) {       //最后一行,只有上角
                while (index < len) {
                    int j = 2 * i;              //上角间隔
                    save.push_back(s[index]);
                    index += j;
                }
            }
            else {                          //剩下的情况
                int j1 = 2 * numRows - 2 * i - 2;       //下角间隔
                int j2 = 2 * i;                         //上角间隔

                //先先下角再上角
                while (index < len) {
                    save.push_back(s[index]);
                    index += j1;
                    if (index < len) {
                        save.push_back(s[index]);
                        index += j2;
                    }
                    else{
                        break;                          //越界
                    }
                }
            }
            cout << save << endl;
        }

        return save;
    }
};

看题解

en------在看人家的代码时发现了,人家初始话vector<string>时只指定了大小,而我又用string()初始化了一遍,优化自己的代码如下

cpp 复制代码
class Solution {
public:
    string convert(string s, int numRows) {
        vector<string> c(numRows);
        int j = 0;
        string save;

        int len = s.size();

        while (j < len) {
            for (int i = 0; i < numRows && j < len; i++)            //竖向【从0~numRows-1】
                c[i] += s[j++];
            for (int i = numRows - 2; i > 0 && j < len; i--)          //斜向【从numRows-2~1】      
                c[i] += s[j++];
        }

        for (int i = 0; i < numRows; i++)                            //合并
            save += c[i];

        return save;
    }
};
相关推荐
阿Y加油吧6 分钟前
力扣打卡——搜索二维矩阵、相交链表
线性代数·leetcode·矩阵
短剑重铸之日6 分钟前
《ShardingSphere解读》16 改写引擎:如何理解装饰器模式下的 SQL 改写实现机制?
java·数据库·后端·sql·shardingsphere·分库分表·装饰器模式
普贤莲花9 分钟前
【2026年第11周---写于20260322】
程序人生·算法·leetcode
q54314708711 分钟前
VScode 开发 Springboot 程序
java·spring boot·后端
小白自救计划12 分钟前
力扣知识点杂集
算法·leetcode·哈希算法
阿贵---15 分钟前
分布式系统C++实现
开发语言·c++·算法
不染尘.16 分钟前
最短路径之Bellman-Ford算法
开发语言·数据结构·c++·算法·图论
小涛不学习19 分钟前
Java高频面试题(带答案版)
java·开发语言
big_rabbit050219 分钟前
JVM堆内存查看命令
java·linux·算法
m0_6625779721 分钟前
C++中的RAII技术深入
开发语言·c++·算法