LeeTCode:6. Z 字形变换 python实现

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

比如输入字符串为 “LEETCODEISHIRING” 行数为 3 时,排列如下:
在这里插入图片描述

方法一:

为一个在这里插入图片描述
类似这种结构的为一个单元结构

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        l = len(s)
        print(l)
        if numRows==1 or l <= numRows or s=="" :
            return s
        f = l%(2*numRows-2)#最后多余几个元素
        w = l//(2*numRows-2)#w个单元结构
        print(w)
        p = 0
        l1 = []
        if l <  2*numRows-2:
            while p < numRows:
                if  p == 0:
                    l1.append(p)
                    p+=1
                elif  0 < p <numRows-1:
                    l1.append(p)
                    if p>=2 and f >= 2* numRows -1-p :
                        l1.append((2*numRows-2)-p)
                    p+=1
                elif p == numRows-1:
                    l1.append(p)
                    p+=1
                else:
                    p+=1  

        while p < numRows:
            if p == 0:
                for i in range(w):
                    l1.append(i*(2*numRows-2))
                if f >0:
                    l1.append(w*(2*numRows-2))
                p += 1 
                # print(p)
            elif 0<p<numRows-1:
                # for m in range(1,2*numRows-2)
                # print('5')
                for i in range(w):
                    m = p+i*(2*numRows-2)
                    # print(m)
                    n = (i+1)*(2*numRows-2)-p
                    l1.append(m)
                    l1.append(n)
                if f > p:
                    l1.append(p+w*(2*numRows-2))
                if  p>= 2 and f >= 2* numRows -1-p and  p != numRows-1 :
                    l1.append((w+1)*(2*numRows-2)-p)
                p+=1
                # print(p)
            else:
                for i in range(w):
                    print(i)
                    m = p+i*(2*numRows-2)
                    # print(m)        
                    l1.append(m)
                if  f > p:
                    l1.append(p+w*(2*numRows-2))
                p+=1
                
        r = ""
        # print(l1)
        for i in l1:
            r += s[i]
        return r

分成三种情况,第一种返回原字符串的,第二种w为0的(没有一个完整的单元结构),第三种有至少一个完整单元结构。p是Z字形的行号,遍历字符串s,找到每一行字符所对应s的index。找到所有index后,按照index打印。

方法二:

参考官方题解的方法,太nb了。

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if s == "" or numRows == 1 :
            return s
        temp = ["" for _ in range(numRows)]
        flag = -1
        i = 0
        for c in s:
            temp[i] += c
            if i == 0 or i==numRows-1:
                flag = -flag
            i = i + flag 
        return "".join(temp)

先建立一个包含numRow个 " " 的list。Z字形每次排列行号都会+1 或者-1 ,所以我们只要遍历s,将每一个字符放在对应行的list元素里,最后再把它们join起来,就完事了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值