The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N A P L S I I G Y I R And then read line by line: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
Example
1 2 3 4 5 6 7 8 9 10 11 12 13
Example 1: Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR"
Example 2: Input: s = "PAYPALISHIRING", numRows = 4 Output: "PINALSIGYAHRPI" Explanation:
publicclassLeetcode6 { /** * 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 * <p> * 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: * <p> * L C I R * E T O E S I I G * E D H N * 之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN"。 * <p> * 请你实现这个将字符串进行指定行数变换的函数: * <p> * string convert(string s, int numRows); * 示例 1: * <p> * 输入: s = "LEETCODEISHIRING", numRows = 3 * 输出: "LCIRETOESIIGEDHN" * 示例 2: * <p> * 输入: s = "LEETCODEISHIRING", numRows = 4 * 输出: "LDREOEIIECIHNTSG" * 解释: * <p> * L D R * E O E I I * E C I H N * T S G * <p> * 来源:力扣(LeetCode) * 链接:https://leetcode-cn.com/problems/zigzag-conversion * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 */ public String convert(String s, int numRows) {