Advertisement
999ms

Untitled

Feb 10th, 2022
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. class Solution:
  2.     def permute(self, nums: List[int]) -> List[List[int]]:
  3.         def gen(i: int, n: int, mask: int) -> List[List[int]]:
  4.             if i == n:
  5.                 return [[None for _ in range(n)]]
  6.             res = []
  7.             for bit in range(n):
  8.                 if (mask >> bit) & 1:
  9.                     continue
  10.                 else:
  11.                     cur = gen(i + 1, n, mask | (1 << bit))
  12.                     for v in cur:
  13.                         v[bit] = i
  14.                         res.append(v.copy())
  15.             return res
  16.  
  17.         n = len(nums)
  18.         perms = gen(0, n, 0)
  19.         for i in range(len(perms)):
  20.             for j in range(len(perms[i])):
  21.                 perms[i][j] = nums[perms[i][j]]
  22.         return perms
  23.  
  24. https://leetcode.com/problems/permutations/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement