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;
    }
};
相关推荐
Charlie_lll1 分钟前
力扣解题-移动零
后端·算法·leetcode
chaser&upper2 分钟前
矩阵革命:在 AtomGit 解码 CANN ops-nn 如何构建 AIGC 的“线性基石”
程序人生·算法
weixin_4997715510 分钟前
C++中的组合模式
开发语言·c++·算法
zfoo-framework19 分钟前
帧同步和状态同步
java
charlotte1024102421 分钟前
高并发:关于在等待学校教务系统选课时的碎碎念
java·运维·网络
亓才孓26 分钟前
[JDBC]PreparedStatement替代Statement
java·数据库
iAkuya41 分钟前
(leetcode)力扣100 62N皇后问题 (普通回溯(使用set存储),位运算回溯)
算法·leetcode·职场和发展
近津薪荼42 分钟前
dfs专题5——(二叉搜索树中第 K 小的元素)
c++·学习·算法·深度优先
xiaoye-duck44 分钟前
吃透 C++ STL list:从基础使用到特性对比,解锁链表容器高效用法
c++·算法·stl
松☆1 小时前
CANN与大模型推理:在边缘端高效运行7B参数语言模型的实践指南
人工智能·算法·语言模型