Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def permute(self, nums: List[int]) -> List[List[int]]:
- def gen(i: int, n: int, mask: int) -> List[List[int]]:
- if i == n:
- return [[None for _ in range(n)]]
- res = []
- for bit in range(n):
- if (mask >> bit) & 1:
- continue
- else:
- cur = gen(i + 1, n, mask | (1 << bit))
- for v in cur:
- v[bit] = i
- res.append(v.copy())
- return res
- n = len(nums)
- perms = gen(0, n, 0)
- for i in range(len(perms)):
- for j in range(len(perms[i])):
- perms[i][j] = nums[perms[i][j]]
- return perms
- https://leetcode.com/problems/permutations/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement