Top 150-InterviewEinfach

Kombinationssumme II

Detaillierte Anleitung und Python-Implementierung für das Problem „Combination Sum II“.

Problemstellung

Einfach

Finden Sie anhand einer Sammlung von Kandidatennummern (Kandidaten) und einer Zielnummer (Ziel) alle eindeutigen Kombinationen in Kandidaten, bei denen die Summe der Kandidatennummern das Ziel ergibt.

Jede Zahl in Kandidaten darf in der Kombination nur einmal verwendet werden.

Hinweis: Der Lösungssatz darf keine doppelten Kombinationen enthalten.

Implementieren Sie eine Funktion combinationSum2(candidates: list, target: int) -> list.

Einschränkungen
  • 1 <= candidates.length <= 100
  • 1 <= candidates[i] <= 50
  • 1 <= target <= 30

Beispiele

Example 1
Input
[10,1,2,7,6,1,5], 8
Output
[[1,1,6],[1,2,5],[1,7],[2,6]]
Explanation

All unique combinations that sum to 8, using each element at most once.

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

1+2+2 = 5 and 5 = 5. These are the only unique combinations.

Need a Hint?
Erwägen Sie die Verwendung von Backtracking-spezifischen Datenstrukturen wie Sets oder Heaps.
Edge Cases to Watch
  • Leere Eingabestrukturen
  • Einzelelementeingaben
  • Große numerische Grenzen

Bereit zur Lösung?

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

Im Editor öffnen
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

Empfohlene Python-Ressourcen

Erweitern Sie Ihr Wissen mit zugehörigen interaktiven Tutorials, Spickzetteln und Codevergleichen.