Easy Python O(n) solution


  • 0
    A
    def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            Mat = [ [] for i in range(numRows)]
            index = 0
            revFlag = 0
            string = ""
            if len(s) is 0:
                return string
            if numRows is 1:
                return s
            for i in range(len(s)):
                if revFlag ==0:
                    if index == numRows-1:
                        Mat[index].append(s[i])
                        revFlag = 1
                        index -=1
                    else:
                        Mat[index].append(s[i])
                        index +=1
                else:
                    if index == 0:
                        Mat[index].append(s[i])
                        revFlag = 0 
                        index += 1
                    else:
                        Mat[index].append(s[i])
                        index -=1 
            for i in range(numRows):
                for j in range(len(Mat[i])):
                    string += Mat[i][j]
            return string
    

Log in to reply
 

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