1 import itertools
 2 class CombinationIterator:
 3 
 4     def __init__(self, characters: str, combinationLength: int):
 5         self.lists = list(itertools.combinations(characters,combinationLength))
 6         self.index = 0
 7 
 8     def next(self) -> str:
 9         tp = self.lists[self.index]
10         self.index += 1
11         return ''.join(tp)
12 
13     def hasNext(self) -> bool:
14         return self.index < len(self.lists)

直接调用itertools内置函数,快速生成符合条件的组合。

1286. Iterator for Combination

posted on 2019-12-15 01:19  Sempron2800+  阅读(139)  评论(0编辑  收藏  举报