[leetcode]6. ZigZag Conversion字符串Z形排列

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:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:

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

Solution1:

Step1: for each row, allocate a new StringBuilder to sort characters in such row

Step2: we can observe that for 1st and last row,  chars will be added into sb[0] and sb[numRows-1], seperately.

for sloping slide,  chars will be added in sb[numRows -2 ] ... sb[1]

Step3: convert each row‘s StringBuilder into result String

code:

 1 /*
 2 Time Complexity: O(n)
 3 Space Complexity: O(n)
 4 */
 5
 6 class Solution {
 7    public String convert(String s, int numRows) {
 8         char[] c = s.toCharArray();
 9         int len = c.length;
10         StringBuilder[] sb = new StringBuilder[numRows];
11         // 要按row来进行遍历,每一个row先allocate一个StringBuilder
12         for (int i = 0; i < sb.length; i++) {
13             sb[i] = new StringBuilder();
14         }
15
16         int idx = 0; //用来扫String s
17         while (idx < len) {
18             for (int i = 0; i < numRows && idx < len; i++) {
19                 sb[i].append(c[idx++]);
20             }
21             // sloping side
22             for (int i = numRows - 2; i >= 1 && idx < len; i--) {
23                 sb[i].append(c[idx++]);
24             }
25
26         }
27         //从sb[0]开始,将sb[1], sb[2], sb[3]... append到一个StringBuilder
28         for (int i = 1; i < sb.length; i++) {
29             sb[0].append(sb[i]);
30         }
31         return sb[0].toString();
32     }
33 }

原文地址:https://www.cnblogs.com/liuliu5151/p/10652751.html

时间: 2024-12-25 03:02:52

[leetcode]6. ZigZag Conversion字符串Z形排列的相关文章

leetCode 6. ZigZag Conversion 字符串 (上传费劲)

6. ZigZag Conversion 题目:https://leetcode.com/problems/zigzag-conversion/ string convert2(string s, int numRows) { if (s.length() < 2 || numRows < 2) return s; int cycle = 2 * numRows - 2; string tmp; string result; for (int i = 0; i < numRows; i+

leetCode 6. ZigZag Conversion 字符串

6. ZigZag Conversion

LeetCode 6 ZigZag Conversion(Z型转换)

翻译 字符串"PAYPALISHIRING"通过一个给定的行数写成如下这种Z型模式: P A H N A P L S I I G Y I R 然后一行一行的读取:"PAHNAPLSIIGYIR" 写代码读入一个字符串并通过给定的行数做这个转换: string convert(string text, int nRows); 调用convert("PAYPALISHIRING", 3),应该返回"PAHNAPLSIIGYIR".

LeetCode 006 ZigZag Conversion - Java

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: "PAHNAPLSII

leetcode -day16 ZigZag Conversion

1.  ZigZag Conversion 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 b

leetcode题解||ZigZag Conversion问题

problem: 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: "P

[LeetCode] 6. ZigZag Conversion (Medium)

原题链接 把字符串按照 ↓↓--的顺序,排列成一个 Z 形,返回 从左到右,按行读得的字符串. 思路: 建立一个二维数组来按行保存字符串. 按照 ↓↓--的方向进行对每一行加入字符. 太慢了这个解法,Runtime: 96 ms, faster than 3.61% of C++. class Solution { public: string convert(string s, int numRows) { if (numRows <= 1) return s; string res; str

leetcode——6 ZigZag Conversion(折线行列输出问题)

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: "

[LeetCode][JavaScript]ZigZag Conversion

ZigZag Conversion 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