N字形变换(麻烦的方法)

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        #先判断z有多少隔开
        s_new=""
        index_now=0
        if len(s)<=numRows or numRows==1:
            return s
        for i in range(numRows-1,-1,-1):
            exchange=0
            index_exchange=index_now
            s_new+=s[index_now]
            #计算每一层的差距
            gap_buttom=int(1+(i-1)*2)
            gap_top=int(1+(numRows-i-2)*2)
            if i==numRows-1 or i==0:
                while index_exchange+int(1+(numRows-2)*2)+1<=len(s)-1:
                    s_new+=s[index_exchange+int(1+(numRows-2)*2)+1]
                    index_exchange+=int(1+(numRows-2)*2)+1
                index_now+=1
                continue
            while i!=numRows-1 and i!=0:
                if exchange==0:
                    if index_exchange+gap_buttom+1>len(s)-1:
                        index_now+=1
                        break
                    else:
                        s_new+=s[index_exchange+gap_buttom+1]
                        index_exchange+=(gap_buttom+1)
                        exchange=1
                else:
                    if index_exchange+gap_top+1>len(s)-1:
                        index_now+=1
                        break
                    else:
                        s_new+=s[index_exchange+gap_top+1]
                        index_exchange+=(gap_top+1)
                        exchange=0
        return s_new

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值