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;
    }
};
相关推荐
203号居民11 分钟前
LeetCode hot 100 —41. 缺失的第一个正数
数据结构·算法·leetcode
Diligently_1 小时前
Anolis 系统更新与密码设置&磁盘扩容
java·后端·spring
mqiqe1 小时前
线程调度与 Schedulers:Project Reactor 并发模型的核心引擎
java·开发语言·网络
我命由我123451 小时前
Android 开发问题:unexpected element <service> found in <manifest>
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
正在走向自律2 小时前
飞算JavaAI能搞定充电站故障处置吗?
java·ai编程·java开发·飞算javaai·java代码生成·ai coding模型·springboo
用户40966601317512 小时前
Guava 不止有 Lists 和 Maps:Cache、EventBus、Multimap 实战
java·后端
专注仿真2 小时前
问答大模型技术方案算法实现-熵权法融合算法 + 交叉编码器重排算法
人工智能·python·算法·语言模型·问答大模型关键算法·熵权融合算法·交叉编码器重排算法
一只叫煤球的猫2 小时前
从 Java 21 到 Java 25:ThreadLocal 以外的选择—— ScopedValue
java·后端·架构
步行cgn3 小时前
MyBatis 多对一关联映射详解
java