Le migliori 150 intervisteFacile

Sottoinsiemi

Guida dettagliata e implementazione Python per il problema dei "sottoinsiemi".

Dichiarazione del problema

Facile

Dato un array intero di elementi univoci, restituisce tutti i possibili sottoinsiemi (il set di potenza).

Il set di soluzioni non deve contenere sottoinsiemi duplicati. Restituire la soluzione in qualsiasi ordine.

Implementa una funzione subsets(nums: list) -> list.

Vincoli
  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • All the numbers of nums are unique

Esempi

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

All 2^3 = 8 subsets of [1,2,3] are generated, including the empty set and the full set.

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

The two subsets of [0] are the empty set [] and [0].

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche del backtracking come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.