6.Z字形变换

2019-04-21  本文已影响0人  王王韦王奇
# 将字符串 "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))
上一篇 下一篇

猜你喜欢

热点阅读