Python solution


  • 1
    H
    class Solution(object):
        def rec_fit(self,td,start):
            if not td:
                return [start]
            if start not in td:
                return None
            for i,j in enumerate(list(td[start])):
                td[start].pop(i)
                if not td[start]:
                    del td[start]
                res=self.rec_fit(td,j)
                if res!=None:
                    return [start]+res
                if start in td:
                    td[start].insert(i,j)
                else:
                    td[start]=[j]
            return None
        def findItinerary(self, tickets):
            """
            :type tickets: List[List[str]]
            :rtype: List[str]
            """
            ticdict={}
            for i,j in tickets:
                ticdict[i]=ticdict.get(i,[])+[j]
            for i in ticdict:
                ticdict[i].sort()
            return self.rec_fit(ticdict,'JFK')

Log in to reply
 

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