Why Python time limit exceeded for 7 Zigzag Conversion?


  • 0
    # -*- coding: utf-8 -*-
    """
    Created on Sat Oct 24 21:31:17 2015
    
    @author: Jian Yang
    """
    
    class Solution(object):
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            zigzag = str('')
            if numRows == 1 or numRows>=len(s):
                return s
            index= 0
    
            maxcol = int(len(s)/(numRows-1))+1
            for i in range(0,maxcol):
                index = (2*numRows-2)*i
                if index > len(s) -1:
                    break
                zigzag += s[index]
                
            for row in range(2, numRows):
                for i in range(0,maxcol):
                    index = i*(2*numRows-2) + row - 1
                    if index < len(s):
                        zigzag += s[index]
                    index = i*(2*numRows-2) + 2*numRows - (row+1)
                    if index < len(s):
                        zigzag += s[index]
    
            for i in range(0,maxcol):
                index = (2*numRows-2)*i + numRows - 1
                if index > len(s) -1:`enter code here`
                    break
                zigzag += s[index]
            return zigzag
      
    def main(s):
        test= Solution()
        print (test.convert(s,3))`enter code here`
        pass
    
    if __name__ == "__main__":
        s='PAYPALISHIRING'
        main(s)

Log in to reply
 

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