Recursive Python solution


  • 1
    L
    class Solution:
        # @param num, a list of integer
        # @return a list of lists of integers
        def permute(self, num):
            if len(num) <= 1:
        		return [num]
        	perms = []
        	for idx, ele in enumerate(num):
        		for permutation in self.permute(num[:idx] + num[idx+1:]):
        			perms.append([ele] + permutation)
        	return perms
    

    This was accepted in leetcode. However I will appreciate any suggestion to improve the performance.


  • 0
    I

    Brilliant! and very readable.


Log in to reply
 

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