[Swift] LeetCode1286 letter combinations iterator |. Iterator for Combination

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: Shan Wing Chi ( let_us_code)
➤ bloggers domain: https://www.zengqiang.org
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address:
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Design an Iterator class, which has:

A constructor that takes a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.
A function next() that returns the next combination of length combinationLength in lexicographical order.
A function hasNext() that returns True if and only if there exists a next combination.
 

Example:

CombinationIterator iterator = new CombinationIterator("abc", 2); // creates the iterator.

iterator.next(); // returns "ab"
iterator.hasNext(); // returns true
iterator.next(); // returns "ac"
iterator.hasNext(); // returns true
iterator.next(); // returns "bc"
iterator.hasNext(); // returns false
 

Constraints:

1 <= combinationLength <= characters.length <= 15
There will be at most 10^4 function calls per test.
It's guaranteed that all calls of the function next are valid.


You design an iterator class, including the following:

A constructor function, the input parameters comprising: a unique character string of ordered and characters (the string contains only lowercase letters) and a digital combinationLength.
Function next (), returns the length of a lexicographical ordering for the next letter of the combination combinationLength.
The hasNext function (), only the presence of the length of the next letter combination combinationLength, returns True; otherwise, return False.
 

Example:

CombinationIterator iterator = new CombinationIterator ( "abc", 2); // create an iterator iterator

iterator.next (); // Returns "ab &"
iterator.hasNext (); // Returns to true
iterator.next (); // Returns "AC"
iterator.hasNext (); // Returns to true
Iterator.next (); // returns "bc"
iterator.hasNext (); // returns false
 

prompt:

1 <= combinationLength <= characters.length < = 15
Each test contains up to 10 ^ 4 times a function is called.
We are entitled to ensure the presence of a combination of letters each time the function next call.

Guess you like

Origin www.cnblogs.com/strengthen/p/12151590.html