1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/字母组合迭代器 [iterator-for-combination].html
2022-03-29 12:43:11 +08:00

39 lines
1.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>请你设计一个迭代器类&nbsp;<code>CombinationIterator</code>&nbsp;,包括以下内容:</p>
<ul>
<li><code>CombinationIterator(string characters, int combinationLength)</code>&nbsp;一个构造函数,输入参数包括:用一个&nbsp;<strong>有序且字符唯一&nbsp;</strong>的字符串&nbsp;<code>characters</code>(该字符串只包含小写英文字母)和一个数字&nbsp;<code>combinationLength</code>&nbsp;</li>
<li>函数&nbsp;<em><code>next()</code>&nbsp;</em>,按&nbsp;<strong>字典序&nbsp;</strong>返回长度为&nbsp;<code>combinationLength</code> 的下一个字母组合。</li>
<li>函数&nbsp;<em><code>hasNext()</code>&nbsp;</em>,只有存在长度为&nbsp;<code>combinationLength</code> 的下一个字母组合时,才返回&nbsp;<code>true</code></li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>
["CombinationIterator", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
[["abc", 2], [], [], [], [], [], []]
<strong>输出:</strong>
[null, "ab", true, "ac", true, "bc", false]
<strong>解释:
</strong>CombinationIterator iterator = new CombinationIterator("abc", 2); // 创建迭代器 iterator
iterator.next(); // 返回 "ab"
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 "ac"
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 "bc"
iterator.hasNext(); // 返回 false
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= combinationLength &lt;=&nbsp;characters.length &lt;= 15</code></li>
<li>&nbsp;<code>characters</code>&nbsp;中每个字符都 <strong>不同</strong></li>
<li>每组测试数据最多对&nbsp;<code>next</code>&nbsp;&nbsp;<code>hasNext</code>&nbsp;调用&nbsp;<code>10<sup>4</sup></code></li>
<li>题目保证每次调用函数&nbsp;<code>next</code>&nbsp;时都存在下一个字母组合。</li>
</ul>