Leetcode Problem 1286. Iterator for Combination

1286. Iterator for Combination

Leetcode Solutions

Algorithm L by D. E. Knuth: Lexicographic Combinations: Next Combination

  1. Initialize a list nums with integers from 0 to combinationLength - 1.
  2. Define a method getNextCombination that updates nums to the next combination.
  3. In getNextCombination, find the rightmost index j where nums[j] can be incremented.
  4. Increment nums[j] and adjust the subsequent indices to form the next lexicographical combination.
  5. In next(), convert the indices in nums to the corresponding string and call getNextCombination.
  6. In hasNext(), return whether the current combination is not the last one.
UML Thumbnail

Bitmasking: Precomputation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...