美文网首页
Leetcode 6 Z字形变换

Leetcode 6 Z字形变换

作者: SunnyQjm | 来源:发表于2020-06-26 11:08 被阅读0次

Z 字形变换

题目

将一个给定字符串根据给定的行数,以从上往下、从左到右进行 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
    

解答

  • 思路:

    • numRows 个list记录每行的字符串;
    • 然后顺序遍历字符串,按Z字形顺序放到合适的行中;
    • 最后将每行的字符串拼接即可。
  • 代码:

    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
       :rtype: str
    
       (Knowledge)
    
        算法思路: 
        
         1. 用numRows个list记录每行的字符串;
    
         2. 然后顺序遍历字符串,按Z字形顺序放到合适的行中;
    
         3. 最后将每行的字符串拼接即可
        """
        if numRows == 1:
            return s
    
        lines = [[]  for i in range(numRows)]
        
        # 当前的方向(首先向下,触底向上,触顶向下,依次改变方向)
        goDown = True
    
        # 下一个字符要写入的行号,初始为0,根据方向进行更新,向下则+1,向上则-1
        lineNumber = 0
        
        # 依次遍历字符串,按Z字型顺序写到合适的行
        for i in range(len(s)):
            lines[lineNumber].append(s[i])
    
            # 判断是否需要改变方向
            if goDown and lineNumber == numRows - 1:
                goDown = False
            if not goDown and lineNumber == 0:
                goDown = True
            
            # 根据当前的方向更新行号
            lineNumber = lineNumber + 1 if goDown else lineNumber - 1
    
        # 将所有numRows个list拼接成一个list
        for i in range(1, numRows):
            lines[0] += lines[i]
    
        # 将list转字符串返回
        return "".join(lines[0])
    

测试验证

class Solution:
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
       :rtype: str

       (Knowledge)

        算法思路: 
        
         1. 用numRows个list记录每行的字符串;

         2. 然后顺序遍历字符串,按Z字形顺序放到合适的行中;

         3. 最后将每行的字符串拼接即可
        """
        if numRows == 1:
            return s

        lines = [[]  for i in range(numRows)]
        
        # 当前的方向(首先向下,触底向上,触顶向下,依次改变方向)
        goDown = True

        # 下一个字符要写入的行号,初始为0,根据方向进行更新,向下则+1,向上则-1
        lineNumber = 0
        
        # 依次遍历字符串,按Z字型顺序写到合适的行
        for i in range(len(s)):
            lines[lineNumber].append(s[i])

            # 判断是否需要改变方向
            if goDown and lineNumber == numRows - 1:
                goDown = False
            if not goDown and lineNumber == 0:
                goDown = True
            
            # 根据当前的方向更新行号
            lineNumber = lineNumber + 1 if goDown else lineNumber - 1

        # 将所有numRows个list拼接成一个list
        for i in range(1, numRows):
            lines[0] += lines[i]

        # 将list转字符串返回
        return "".join(lines[0])
        


if __name__ == '__main__':
    solution = Solution()
    print(solution.convert("LEETCODEISHIRING", 3), "= \nLCIRETOESIIGEDHN")
    print(solution.convert("LEETCODEISHIRING", 4), "= \nLDREOEIIECIHNTSG")

相关文章

  • Python算法-模拟过程实现算法

    6. Z 字形变换[https://leetcode-cn.com/problems/zigzag-convers...

  • LeetCode[6] - Z字形变换

    题目 将字符串 "PAYPALISHIRING"以Z字形排列成给定的行数: 之后从左往右,逐行读取字符:"PAHN...

  • [LeetCode]6、Z字形变换

    题目描述 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 "LEETC...

  • Leetcode 6 Z字形变换

    Z 字形变换 题目 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 "...

  • leetcode 6 Z 字形变换

    这个题的关键在于控制到达行0或者行的最大值时往回走的问题,有点像个铁道兵,走到路的尽头就返回。 自己的解法,使用s...

  • LeetCode 6 Z字形变换

    6 Z字形变换 一、题目 将一个给定字符串根据给定的行数,以从上往下、从左到右进行Z字形排列。 比如输入字符串为"...

  • Leetcode 6 Z字形变换

    将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。比如输入字符串为 "LEETCODEISH...

  • leetcode 6 - Z 字形变换

    题目以及题解来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/...

  • Python小白 Leetcode刷题历程 No.6-No

    Python小白 Leetcode刷题历程 No.6-No.10 Z 字形变换、整数反转、字符串转换整...

  • LeetCode6.Z字形变换 JavaScript

    LeetCode6.Z字形变换 JavaScript 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z...

网友评论

      本文标题:Leetcode 6 Z字形变换

      本文链接:https://www.haomeiwen.com/subject/vgwyfktx.html