class Solution:
    def permute(self, nums):
        ans = []
        def p(A, B):
            if not B: return ans.append(A)
            for i in range(len(B)):
                p(A+[B[i]], B[:i]+B[i+1:])
        p([], nums)
        return ans

print(Solution().permute([1,2,3,4]))
