Python solution using dicts(hashmaps)


  • 0
    I
    class Solution(object):
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            
            hm = {}
            i=0
            j=0
            n=0
            diag=0
            strt=1
            end=False
            if numRows==1:
                return s
            if len(s)==1:
                return s
            while end==False:
                if n==len(s):
                    break
                    end=True
                if strt==1:
                    if i in hm:
                        hm[i]=hm[i]+s[n]
                    else:
                        hm[i]=s[n]
                    n+=1
                    i+=1
                if diag==1:
                    if i in hm:
                        hm[i]=hm[i]+s[n]
                    else:
                        hm[i]=s[n]
                    n+=1
                    j+=1
                    i-=1
                if i==numRows:
                    i=i-2
                    j=j+1
                    strt=0
                    diag=1
                if i<0:
                    i=i+2
                    j=j-1
                    diag=0
                    strt=1
            s=""
            for i in hm:
                s=s+hm[i]
            return s
    

Log in to reply
 

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