LeetCode 6.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


思路:

一开始没看懂题目,看了评论才知道意思,这里列一下我认为比较容易理解的的方法。 主体思想是找到矩阵填入字符规律。

方法一:

第一种,是模拟Z型矩阵,造对应行数的二维数组存入字串,这个方法虽然不怎么聪明,但是更能直观的理解处理思想。

class Solution(object):
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows == 1 or len(s) <= numRows:
            return s
        Dict = self.getArray(s, numRows)
        rows = min(len(s), numRows)
        strList = []
        for line in range(rows):
            strList.extend(Dict[line])
        return ''.join(strList)

    def getArray(self, s, numRows):
        rows = min(len(s), numRows)
        Dict = {}
        for row in range(rows):
            Dict[row] = []
        curRow = 0
        flag = False
        for index in range(len(s)):
            Dict[curRow].extend(s[index])
            if curRow == 0 or curRow == numRows - 1:
                flag = not flag
            curRow += 1 if flag else -1
        return Dict

执行用时 :84 ms, 在所有 Python 提交中击败了30.31%的用户

内存消耗 :12.7 MB, 在所有 Python 提交中击败了12.50%的用户

方法二:

前面是用二维数组存入对应字符,这个方法是按照行数numRows生成一个字符串数组rows_str_list,rows_str_list[0]即第一行的字符串,rows_str_list[1]即第二行字符串,以此类推,然后取出输入的字符串s的每个字符在这个数组上扫描添加,step表示扫描方向,扫描到index=0或者index==numRows就变换方向,依次把每个字符填入对应的行,最后字符串拼接一下。

class Solution:
    def convert(self, s, numRows) :
        if numRows == 1 or len(s) <= numRows:
            return s
        row, step = 0, 1
        rows_str_list = ['' for _ in range(numRows)]
        for char in s:
            rows_str_list[row] += char
            if row == 0:
                step = 1
            if row == numRows-1:
                step = -1
            row += step
        return ''.join(rows_str_list)

执行用时 :52 ms, 在所有 Python 提交中击败了78.46%的用户

内存消耗 :12.8 MB, 在所有 Python 提交中击败了12.50%的用户

方法三:

前面分别用二维和一维数组存入字符,现在直接通过字符进行拼接,这个方法也比较巧妙,直接通过对塞入的字符index规律进行拼接。这个规律需要实际写一个Z型,标记index比较好理解该规律。

class Solution(object):
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows == 1 or len(s) <= numRows:
            return s
        res = ""
        for i in range(numRows):
            j = 0
            count = i
            tempCount = -1
            while count < len(s):
                if tempCount != count:
                    res += s[count]
                tempCount = count
                if j % 2 == 0:
                    count += 2 * (numRows - (i + 1))       
                else:  
                    count += 2 * i
                j += 1
        return res

执行用时 :68 ms, 在所有 Python 提交中击败了49.71%的用户

内存消耗 :12.7 MB, 在所有 Python 提交中击败了12.50%的用户

原文地址:https://www.cnblogs.com/xiaoqiangink/p/12935595.html