码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode-15双周赛-1286-字母组合迭代器

时间:2019-12-23 16:39:02      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:ted   code   length   pre   +=   png   迭代   elf   tor   

题目描述:

技术图片

 

 方法:

class CombinationIterator:

    def __init__(self, characters: str, combinationLength: int):
        self.s = characters
        self.pos = [x for x in range(combinationLength)]
        self.finished = False

    def next(self) -> str:
        ans = "".join([self.s[p] for p in self.pos])
        i = -1
        for k in range(len(self.pos) - 1, -1, -1):
            if self.pos[k] != len(self.s) - len(self.pos) + k:
                i = k
                break
        if i == -1:
            self.finished = True
        else:
            self.pos[i] += 1
            for j in range(i + 1, len(self.pos)):
                self.pos[j] = self.pos[j - 1] + 1
        return ans

    def hasNext(self) -> bool:
        return not self.finished


# Your CombinationIterator object will be instantiated and called as such:
# obj = CombinationIterator(characters, combinationLength)
# param_1 = obj.next()
# param_2 = obj.hasNext()

leetcode-15双周赛-1286-字母组合迭代器

标签:ted   code   length   pre   +=   png   迭代   elf   tor   

原文地址:https://www.cnblogs.com/oldby/p/12084432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!