Sunday, August 16, 2015

Leetcode 46. Permutations

https://leetcode.com/problems/permutations/

DFS.

Solution:
# T:O(n!) S:O(n)
class Solution:
    # @param num, a list of integer
    # @return a list of lists of integers
    def permute(self, num):
        if len(num) == 0: return []
        if len(num) == 1: return [num]
        res = []
        for i in range(len(num)):
            for j in self.permute(num[:i] + num[i+1:]):
                res.append([num[i]] + j)
        return res
Run Time: 84 ms

No comments:

Post a Comment