Z 字形变换

Z 字形变换

将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。
比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:
P   A   H   N
A P L S I I G
Y   I   R
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"。

																来源:力扣(LeetCode)
								链接:https://leetcode.cn/problems/zigzag-conversion
输入: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

My Code

我做这道题的时候没有仔细读题,考虑原样输出了空格和换行符,所以在输出空格的规律上花了一些时间思考,导致问题变复杂,结果只用输出字母的顺序就行。。。
我的做法其实非常传统,考虑字母、空格的输出规律进行编码的,没有考虑到用矩阵模拟呀这些高端的方法,慢慢学习吸取经验吧。

#include<iostream>
using namespace std;
int main()
{
    string s="PAYPALISHIRING";
    int numRows=4;
    int len;
    int spaceNum;
    // cin >> s;
    // cin >> numRows;
    len = s.length();
    string Z =""; 
    int Max_space = numRows-2;
    int Max_next = (numRows-1)*2;
    if (Max_next == 0)
        Max_next = 1;
    for(int row = 0;row < numRows-1;row++)
    {
        int i = row;
        int flag=0;
        int next;
        int last_spaceNum;
        while (i<len)
        {
            Z += s[i];
            if(flag==0)
                spaceNum = Max_space-row;
            else{
                spaceNum = numRows-3-last_spaceNum;
            }
            for (int j = 0; j < spaceNum; j++)
                Z.append(" ");
            if (flag == 0){
                next = (numRows-row-1)*2;
                if (next == Max_next)
                    flag = 0;
                else{
                    flag=next;
                    last_spaceNum = spaceNum;
                }
            }
            else{
                next = Max_next-flag;
                last_spaceNum = spaceNum;
                flag=0;
            }
            i += next;
        }
        Z += '\n';
    }
    int last = numRows-1;
    while(last<len)
    {
        Z += s[last];
        spaceNum = Max_space;
        for (int j = 0; j < spaceNum; j++)
            Z.append(" ");
        last += Max_next;
    }
    Z += '\n';
    cout << Z << endl;
}

在这里插入图片描述
提交时只需要将空格控制代码和换行符的代码删除就可以正确提交了。
其实我的代码还有改进的空间,就是最后一行我是单独处理的,因为前面的n-1行规律比较统一。
继续努力吧!!!

Better Code(矩阵模拟)

考虑创建一个二维矩阵,然后在矩阵上按 Z 字形填写字符串 s,最后逐行扫描矩阵中的非空字符,组成答案。

class Solution {
public:
    string convert(string s, int numRows) {
        int n = s.length(), r = numRows;
        if (r == 1 || r >= n) {
            return s;
        }
        int t = r * 2 - 2;
        int c = (n + t - 1) / t * (r - 1);
        vector<string> mat(r, string(c, 0));
        for (int i = 0, x = 0, y = 0; i < n; ++i) {
            mat[x][y] = s[i];
            if (i % t < r - 1) {
                ++x; // 向下移动
            } else {
                --x;
                ++y; // 向右上移动
            }
        }
        string ans;
        for (auto &row : mat) {
            for (char ch : row) {
                if (ch) {
                    ans += ch;
                }
            }
        }
        return ans;
    }
};

Tip: 出现vscode调试报错:参数格式不正确错误解决方案

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值