6 Z字形變換

2021-09-02 19:01:59 字數 818 閱讀 8981

將字串"paypalishiring"以z字形排列成給定的行數:

p   a    h    n

a  p  l  s i  i   g

y    i    r

之後從左往右,逐行讀取字元:"pahnaplsiigyir"

示例1:

輸入: s = "paypalishiring", numrows = 3

輸出: "pahnaplsiigyir"

示例 2:

輸入: s = "paypalishiring", numrows = 4

輸出: "pinalsigyahrpi"

我自己感覺是v字形變換,就是找規律的題。

解法:按行訪問

首先訪問 行 0 中的所有字元,接著訪問 行 1,然後 行 2,依此類推...

分析c++ code:

#include#include#include#include#include#includeusing namespace std;

class solution

}return res;

}};

int main()

參考

6 Z字形變換

一 題目 將字串 paypalishiring 以z字形排列成給定的行數 p a h n a p l s i i g y i r之後從左往右,逐行讀取字元 pahnaplsiigyir 實現乙個將字串進行指定行數變換的函式 string convert string s,int numrows 示例...

6 Z 字形變換

將乙個給定字串根據給定的行數,以從上往下 從左到右進行 z 字形排列。比如輸入字串為 leetcodeishiring 行數為 3 時,排列如下 l c i r e t o e s i i g e d h n之後,你的輸出需要從左往右逐行讀取,產生出乙個新的字串,比如 lciretoesiigedh...

6 Z 字形變換

將乙個給定字串根據給定的行數,以從上往下 從左到右進行 z 字形排列。比如輸入字串為 leetcodeishiring 行數為 3 時,排列如下 l c i r e t o e s i i g e d h n 之後,你的輸出需要從左往右逐行讀取,產生出乙個新的字串,比如 lciretoesiiged...