Anyone help me why my python solution for ZigZag Conversion hints "unboundlocalerror"?


  • 0
    A
    class Solution(object):
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
    
            if numRows == 1:
                return s
                
            for i in range(1, numRows+1):
                row[i] = ""
            l = 2*(numRows-1)
            for i in range(len(s)):
                if (i+1)%l <= numRows:
                    j = (i+1)%l
                else:
                    j = l - (i+1)%l + 2 
                row[j] += s[i]
            for i in range(1, numRows+1):
                row += row[i]
            return row

  • 0
    R
      for i in range(1, numRows+1):
            row[i] = ""
    

    row should be defined before you use it here

       row = [] 
       for i in range(1, numRows+1):
            row.append('')
    

    however, after fix this, also have bugs.


Log in to reply
 

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