6.Z字形变换

# 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:
# P   A   H   N
# A P L S I I G
# Y   I   R
# 之后从左往右,逐行读取字符:"PAHNAPLSIIGYIR"
#
# 示例 1:
# 输入: s = "PAYPALISHIRING", numRows = 3
# 输出: "PAHNAPLSIIGYIR"

# 难度:中等

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

        # 下面是特殊情况处理
        if s == '':
            s1 = ''
        elif 1 <= len(s) <= 2 or numRows == 1 or numRows > len(s):
            s1 = s
        elif numRows == 2:
            for i in range(2):
                print(len(s), len(s) / 2 + 1)
                for j in range(1, int(len(s) / 2 + 1)):
                    s1 += s[(j - 1) * 2 + i]
                    if len(s) % 2 == 1 and j == int(len(s) / 2) and i == 0:
                        s1 += s[len(s) - 1]

        # 下面是一般情况处理
        else:
            a = 2 * numRows - 2  # a是每组有几个字符
            b = len(s) // a      # b是组数
            c = len(s) % a       # c是不在整组中的字符
            if len(s) <= a:
                for j in range(1, numRows + 1):  # j是行数
                    if b == 1:
                        if j == 1:
                            s1 += s[0]
                        elif 1 < j < numRows:
                            s1 += s[j - 1]
                            if j >= c - numRows:
                                s1 += s[a - j + 1]
                        else:
                            s1 += s[j - 1]
                    else:
                        if j == 1:
                            s1 += s[0]
                        elif 1 < j < numRows:
                            s1 += s[j - 1]
                            if len(s) % numRows >= numRows - j:
                                s1 += s[2 * numRows - j - 1]
                        else:
                            s1 += s[j - 1]
            else:
                for j in range(1, numRows + 1):  # j是行数
                    for i in range(1, b + 1):  # b是所在组数
                        if j == 1:
                            s1 += s[a * (i - 1)]
                            if i == b and c != 0:
                                s1 += s[a * i]
                        elif 1 < j < numRows:
                            s1 += s[a * (i - 1) + j - 1]
                            s1 += s[a * i - j + 1]
                            if i == b and c >= j:
                                s1 += s[a * i + j - 1]
                                if c - numRows >= numRows - j:
                                    s1 += s[a * (i + 1) - j + 1]
                        else:
                            s1 += s[a * (i - 1) + j - 1]
                            if b == 2 and j != numRows:
                                s1 += s[a * i - j + 1]
                                if i == b and c >= j:
                                    s1 += s[a * i + j - 1]
                            elif i == b and c >= j:
                                s1 += s[a * i + j - 1]
        return s1


print(Solution.convert(0, 'PAYPALISHIRING', 2))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • https://leetcode-cn.com/problems/zigzag-conversion/ 做这道题,...
    Jaxon张阅读 382评论 0 0
  • 需求 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 "LEETCOD...
    惑也阅读 342评论 0 1
  • 一、题目原型: 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:输入: s = "PAYPA...
    花果山松鼠阅读 3,846评论 3 0
  • 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数: PA H NA P L SI I...
    Gunther17阅读 206评论 0 0
  • 题目 将字符串 "PAYPALISHIRING"以Z字形排列成给定的行数: 之后从左往右,逐行读取字符:"PAHN...
    sxqiong阅读 1,299评论 0 1