Recursive Python solution

  • 1
    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

    Brilliant! and very readable.

Log in to reply

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