Python simple solution (12ms)


  • 0

    Just prepare strong for each row and store characters in the given string in zig zag order
    to the rows, then join all rows.

    class Solution(object):
        def convert(self, s, numRows):
            leng = len(s)
            if leng == 0:      return ''
            if numRows <= 0:   return ''
            elif numRows == 1: return s
            
            resRows = [''] * numRows
            
            i = 0
            resRowNum = 0
            step = 1
            while i < leng:
                resRows[resRowNum] += s[i]
                
                if (step == 1 and resRowNum == numRows - 1) or (step == -1 and resRowNum == 0):
                    step = -1 * step
                
                resRowNum += step
                i += 1
            
            return ''.join(resRows)

Log in to reply
 

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