Python Solution


  • 0
    J
    class Solution(object):
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            if len(s) == 0:
                return ''
            if numRows < 1:
                print 'Please enter a valid numRows value.'
                return
            # create a list of len = numRows
            list = [''] * numRows
            i = 0
            inc = 1
            for string in s:
                if i == numRows - 1:
                    inc = -1
                if i == 0:
                    inc = 1
                list[i % numRows] += string
                i = i + inc
            # combine the strings together
            rstr = ''
            for i in list:
                rstr += i
            return rstr
    ```![alt text](image url)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.