码迷,mamicode.com
首页 > 其他好文 > 详细

将字符串中的字符按Z字形排列,按行输出

时间:2019-03-14 13:14:11      阅读:507      评论:0      收藏:0      [点我收藏+]

标签:elf   ict   排列   tput   bsp   输出   self   output   图片   

示例1:

  Input: s = "PAYPALISHIRING", numRows = 3  

       技术图片

  Output: "PAHNAPLSIIGYIR"

示例2:

技术图片

解决方案:

    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if len(s) <= numRows or numRows==1 :
            return s
        s_dict = {i:"" for i in range(numRows)}
        unit = 2*numRows - 2
        for i in range(len(s)):
            remain = i%unit
            if remain <= numRows - 1:
                s_dict[remain] += s[i]
            else:
                pos = unit - remain
                s_dict[pos] += s[i]
        out = ""
        for i in range(numRows):
            out += s_dict[i]
        return out

 

将字符串中的字符按Z字形排列,按行输出

标签:elf   ict   排列   tput   bsp   输出   self   output   图片   

原文地址:https://www.cnblogs.com/wenqinchao/p/10529461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!