150 najlepszych wywiadówŁatwe

Permutacje

Szczegółowy przewodnik i implementacja Python dla problemu „Permutacje”.

Oświadczenie o problemie

Łatwe

Biorąc pod uwagę tablicę nums różnych liczb całkowitych, zwróć wszystkie możliwe permutacje. Odpowiedź możesz zwrócić w dowolnej kolejności.

Zaimplementuj funkcję permute(nums: list) -> list.

Ograniczenia
  • 1 <= nums.length <= 6
  • -10 <= nums[i] <= 10
  • All the integers of nums are unique

Przykłady

Example 1
Input
[1,2,3]
Output
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Explanation

All 3! = 6 permutations of [1,2,3] are generated.

Example 2
Input
[0,1]
Output
[[0,1],[1,0]]
Explanation

All 2! = 2 permutations of [0,1] are generated.

Example 3
Input
[1]
Output
[[1]]
Explanation

Only one permutation exists for a single element.

Need a Hint?
Rozważ użycie struktur danych specyficznych dla cofania, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Otwórz w Edytorze
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.