Python, keep track of direction


  • 0
    L

    '''

    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows ==1:
            return s
            
        res = [""]*numRows
        row=0
        direction = 1
        for i in range(len(s)):
            res[row]+=s[i]
            row+=direction
            if row == numRows-1 or row==0:
                direction *= -1
    
        return ''.join(res)
    

    '''


Log in to reply
 

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