leetcode--6. ZigZag Conversion

来源:互联网 发布:网络教育 心理学 编辑:程序博客网 时间:2024/05/29 17:31

题目:6. ZigZag Conversion

链接:https://leetcode.com/problems/zigzag-conversion/description/

嗯怎么说呢,大概是将字符串像拉链一样穿起来吧。

Python:

class Solution(object):    def convert(self, s, numRows):        """        :type s: str        :type numRows: int        :rtype: str        """        if len(s)<=numRows:            return s        res=[""]*numRows        length=len(s)        i=0        while i<length:            cnt=0            while cnt<numRows and i<length:                res[cnt]+=s[i]                cnt+=1                i+=1            cnt=numRows-2            while cnt>0 and i<length:                res[cnt]+=s[i]                cnt-=1                i+=1        ans=""        for i in range(numRows):            ans+=res[i]        return ans


原创粉丝点击