Leetcode Z 字形变换

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

比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:

P   A   H   N
A P L S I I G
Y   I   R

之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"

请你实现这个将字符串进行指定行数变换的函数:

string convert(string s, int numRows);

示例 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

示例 3:

输入:s = "A", numRows = 1
输出:"A"

提示:

  • 1 <= s.length <= 1000
  • s 由英文字母(小写和大写)、',' 和 '.' 组成
  • 1 <= numRows <= 1000

方法一:利用二维矩阵模拟


设 n 为字符串 s 的长度,r=numRows。对于 r=1(只有一行)或者 r≥n(只有一列)的情况,答案与 s 相同,我们可以直接返回 s。对于其余情况,考虑创建一个二维矩阵,然后在矩阵上按 Z 字形填写字符串 s,最后逐行扫描矩阵中的非空字符,组成答案。

根据题意,当我们在矩阵上填写字符时,会向下填写 r 个字符,然后向右上继续填写 r−2 个字符,最后回到第一行,因此 Z 字形变换的周期 t=r+r−2=2r−2,每个周期会占用矩阵上的 1+r−2=r−1 列。

#include
#include
#include
#include
#include
using namespace std;
class Solution {
public:
    string convert(string s, int numRows) {
        string str = s;
        int num = numRows;
        
        vector>v;
        if(num == 1)    return s;
        for(int i = 0; i < num; i++){
            vectort;
            v.push_back(t);
        }
        int k = 0, now = 0;
        while(true){
            //向下填写 r 个字符
            while(k < num && now < str.size()){
                v[k].push_back(str[now]);
                now++;
                k++;
            }
            if(now == str.size())   break;
            k = num - 2;
            int nowh = num - 1;
            //向右上继续填写 r−2 个字符
            while(now < str.size()){
                if(nowh == k && k > 0){
                    v[nowh].push_back(str[now]);
                    now++;
                    k--;
                }else if(nowh == 0 && k == 0){
                    //当行号和列号均为0时,结束,重新向下遍历
                    v[nowh].push_back(' ');
                    break;
                }else{
                    v[nowh].push_back(' ');
                }
                nowh--;
                if(nowh < 0)    now = num -1;
            }
            if(now == str.size())   break;
        }
        string str2;
        // 遍历所有非空字符
        for(auto it = v.begin(); it != v.end(); it++){
            for(auto it2 = it->begin(); it2 != it->end(); it2++){
                if(*it2 != ' ') str2 += *it2;
            }
        }
        return str2;
    }
};

方法二:直接构造


我们来研究方法一中矩阵的每个非空字符会对应到 s 的哪个下标(记作 idx),从而直接构造出答案。

由于 Z 字形变换的周期为 t=2r−2,因此对于矩阵第一行的非空字符,其对应的 idx 均为 t 的倍数,即 idx =0(modt);同理,对于矩阵最后一行的非空字符,应满足 idx=r−1(modt)。

对于矩阵的其余行(行号设为 i),每个周期内有两个字符,第一个字符满足 idx=i(modt),第二个字符满足 idx=t−i(modt)。

通过观察,不难发现以下规律:

Leetcode Z 字形变换_第1张图片

class Solution {
public:
    string convert(string s, int numRows) {
        int n = s.length(), r = numRows;
        if (r == 1 || r >= n) {
            return s;
        }
        string ans;
        int t = r * 2 - 2;
        for (int i = 0; i < r; ++i) { // 枚举矩阵的行
            for (int j = 0; j + i < n; j += t) { // 枚举每个周期的起始下标
                ans += s[j + i]; // 当前周期的第一个字符
                if (0 < i && i < r - 1 && j + t - i < n) {
                    ans += s[j + t - i]; // 当前周期的第二个字符
                }
            }
        }
        return ans;
    }
};

你可能感兴趣的:(Leetcode刷题,leetcode,算法,职场和发展)