[LeetCode] 1286. Iterator for Combination

发布时间:2022-06-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了[LeetCode] 1286. Iterator for Combination脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

Design the CombinationITerator class:

  • CombinationIterator(string characters, int combinationLength) Initializes the object with a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.
  • next() Returns the next combination of length combinationLength in lexicographical order.
  • hasNext() Returns true if and only if there exists a next combination.

Example 1:

Input
["CombinationIterator", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
[["abc", 2], [], [], [], [], [], []]
Output
[null, "ab", true, "ac", true, "bc", false]

Explanation
CombinationIterator itr = new CombinationIterator("abc", 2);
itr.next();    // return "ab"
itr.hasNext(); // return True
itr.next();    // return "ac"
itr.hasNext(); // return True
itr.next();    // return "bc"
itr.hasNext(); // return False

Constraints:

  • 1 <= combinationLength <= characters.length <= 15
  • All the characters of characters are unique.
  • At most 104 calls will be made to next and hasNext.
  • It's guaranteed that all calls of the function next are valid.

字母组合迭代器。

请你设计一个迭代器类,包括以下内容:

一个构造函数,输入参数包括:一个 有序且字符唯一 的字符串 characters(该字符串只包含小写英文字母)和一个数字 combinationLength 。函数 next() ,按 字典序 返回长度为 combinationLength 的下一个字母组合。函数 hasNext() ,只有存在长度为 combinationLength 的下一个字母组合时,才返回 True;否则,返回 False。

:力扣(LeetCode)链接:https://leetcode-cn.COM/PRoblems/iterator-for-combination著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道回溯的题目,如果不熟悉可以先做77题。77题的组合问题是给你从 1 到 N 的数字请你列出所有长度为 K 的数字的组合,这道题无非是把数字换成了字母。

时间O(n)

空间O(n)

Java实现

 1 class CombinationIterator {
 2     Queue<String> queue;
 3 
 4     public CombinationIterator(String characters, int combinationLength) {
 5         queue = new LinkedList<>();
 6         helPEr(characters, 0, "", combinationLength, queue);
 7     }
 8 
 9     public void helper(String characters, int start, String soFar, int k, Queue<String> queue) {
10         // k = how may characters left for current string, k = combinationLength - soFar.length()
11         if (k == 0) {
12             queue.offer(soFar);
13             return;
14         }
15         for (int i = start; i < characters.length(); i++) {
16             helper(characters, i + 1, soFar + characters.charAt(i), k - 1, queue);
17         }
18     }
19     
20     public String next() {
21         return queue.poll();
22     }
23     
24     public boolean hasNext() {
25         return !queue.iSEMpty();
26     }
27 }
28 
29 /**
30  * Your CombinationIterator object will be instantiated and called as such:
31  * CombinationIterator obj = new CombinationIterator(characters, combinationLength);
32  * String param_1 = obj.next();
33  * boolean param_2 = obj.hasNext();
34  */

 

相关题目

77. Combinations

1286. Iterator for Combination

LeetCode 题目总结

脚本宝典总结

以上是脚本宝典为你收集整理的[LeetCode] 1286. Iterator for Combination全部内容,希望文章能够帮你解决[LeetCode] 1286. Iterator for Combination所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。