None recursive python code, accepted


  • 0
    F
     class Solution:
            # @param num, a list of integer
            # @return a list of lists of integers
            def permute(self, num):
                result = [[]]
                permutations = []
                for i in num:
                    if result == [[]]:
                        result = [[i]]
                    else :
                        for j in result :
                            for k in range(0,len(j)+1) :
                                temp = j[:]
                                temp.insert(k,i)
                                permutations.append(temp)
                        result = permutations[:]
                        permutations = []
                return result

  • 0
    R

    Good to know this different way. Thanks!

    By the way, it is a little surprise (to me) that this non-recursive solution is not really faster than recursive solution. I think those intensive list mutation is also expensive.


Log in to reply
 

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