My very easy to understand brute force python solution.


  • 0
    Y

    Create numRows of empty lists, and appending each character into the corresponding list as you iterate through the given string

    def convert(self, s, numRows):
            d = {}
            count = 0
            res = ""
            count_up = True #if up is True, count up, else count down
            if numRows == 1:
                return s
            for i in range(0, numRows):
                d[i] = []
            for char in s:
                if count == numRows-1:  #
                    count_up = False    #
                if count == 0:          # Toggle switch
                    count_up = True     #
    
                if count_up:
                    d[count].append(char)
                    count += 1
                else:
                    d[count].append(char)
                    count -= 1
            for i in range(0, numRows):
                for item in d[i]:
                    res += item
            
            return res
    

Log in to reply
 

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