O(n) algorithm, got answer <10ms locally, submission result as TLE


  • 0
    C

    here follows my code, it gets the answer for the TLE test case instantaneously on my computer, anyone has an idea about why it can't work when submitted? Thanks a lot!

    import textwrap
    class Solution(object):
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            if numRows>1:
                partSs=textwrap.wrap(s,2*numRows-2)
                result=''
                for i in range(numRows):
                    for item in partSs:
                        try:
                            result += item[i]
                            if 0<i<numRows-1:
                                result += item[2*numRows-2-i]
                        except:
                            pass
                return result
            else:
                return s

Log in to reply
 

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