【leetcode系列】【算法】【中等】Z 字形变换

题目:

原题链接: https://leetcode-cn.com/problems/zigzag-conversion/

 

解题思路:

抛开原始字符串,直接使用字符串编号,转换后的排列如下:

3阶:

0  4(0+4) 8(4+4) 12(8+4) 16(12+4) 
13(1+2)5(3+2)7(5+2)9(7+2)11(9+2)13(11+2)15(13+2)17(15+2)19(17+2)
2 6(2+4) 10(6+4) 14(10+4) 18(14+4) 

4阶:

0  6(0+6)  12(6+6)  18(12+6)
1 5(1+4)7(5+2) 11(7+4)13(11+2) 17(13+4)19(17+2)
24(2+2) 8(4+4)10(8+2) 14(10+4)16(14+2) 20(16+4)
3  9(3+6)  15(9+6)  21(15+6)

 

3阶的可能不太明显,4阶比较明显,用4阶的为例

每行的下标间隔总数为6(2 * (4 - 1)),将每行的间隔分为左间隔和右间隔,变化如下:

左间隔右间隔
60
42
24
06

每往下一行,左间隔-2,右间隔+2

每行的第一个字符的编号就是行号,然后根据行号,进行左右间隔轮换跳转获取字符

每个字符只循环了一次,时间复杂度为O(n)

 

代码实现:

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if 0 == len(s):
            return s
        
        total = max(2 * (numRows - 1), 1)

        # 使用list的原因,是因为''.join()的效率,比str的+效率要高
        res_str = []
        
        cycle_num = 0
        for row in range(0, numRows):
            index = row
            if index >= len(s): break
            res_str.append(s[index])
            left = total - 2 * row
            right = 2 * row
            while index < len(s):
                cycle_num += 1
                if 0 != left:
                    index += left
                    if index < len(s): res_str.append(s[index])

                if 0 != right:
                    index += right
                    if index < len(s): res_str.append(s[index])
                    
        return ''.join(res_str)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值