LeetCode #6 ZigZag Conversion Z 字形变换

6 ZigZag Conversion Z 字形变换

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

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

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

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

L   C   I   R
E T O E S I I G
E   D   H   N

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

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

string convert(string s, int numRows);

示例 :

示例 1:

输入: s = "LEETCODEISHIRING", numRows = 3
输出: "LCIRETOESIIGEDHN"

示例 2:

输入: s = "LEETCODEISHIRING", numRows = 4
输出: "LDREOEIIECIHNTSG"
解释:

L     D     R
E   O E   I I
E C   I H   N
T     S     G

思路:

  1. 按 Z字形遍历字符串, 记录当前遍历的行数, 如果遍历到给定的行数(0/numRows)就反向
  2. 写出 Z字形字符串的下标, 找到下标的规律, 分成第一行/最后一行和其他行, 按照下标输出
    时间复杂度O(n), 空间复杂度O(n)

代码:
C++:

class Solution 
{
public:
    string convert(string s, int numRows) 
    {
        if (numRows == 1) return s;
        string result;
        int step = 2 * numRows - 2;
        for (int i = 0; i < numRows; i++) 
        {
            for (int j = 0; j + i < s.size(); j += step) 
            {
                result += s[j + i];
                if (i and i != numRows - 1 and j + step - i < s.size()) result += s[j + step - i];
            }
        }
        return result;
    }
};

Java:

class Solution {
    public String convert(String s, int numRows) {
        if (numRows == 1) return s;
        List<StringBuilder> rows = new ArrayList<>();
        for (int i = 0; i < Math.min(numRows, s.length()); i++) rows.add(new StringBuilder());
        int cur = 0;
        boolean next = false;
        for (char c : s.toCharArray()) {
            rows.get(cur).append(c);
            if (cur == 0 || cur == numRows - 1) next = !next;
            cur += next ? 1 : -1;
        }
        StringBuilder result = new StringBuilder();
        for (StringBuilder row : rows) result.append(row);
        return result.toString();
    }
}

Python:

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if numRows == 1:
            return s;
        result, step = '', 2 * numRows - 2
        for i in range(numRows):
            j = 0
            while i + j < len(s):
                result += s[i + j]
                if i and i != numRows - 1 and j + step - i < len(s):
                    result += s[j + step - i]
                j += step
        return result
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 题目描述: 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。比如输入字符串为 "LEETC...
    LeeYunFeng阅读 959评论 0 50
  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 3,391评论 0 2
  • 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 "LEETCODEIS...
    埋没随百草阅读 313评论 0 0
  • 需求 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 "LEETCOD...
    惑也阅读 340评论 0 1
  • 早上五点被工地噪声吵醒,搬来快两年,东面步行街马上都快开业了,北面塔楼才修到十多层,照这进度,修好那368米的主楼...
    不舍札记阅读 245评论 0 0