Leetcode 6. ZigZag Conversion(python)

这个想法很简单,用一个列表表示每一行的字符串,最后再输出即可。

重点是找规律。

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

  

posted @ 2016-04-04 16:22  colors  阅读(189)  评论(0编辑  收藏  举报