Skip to content

Commit c2db5c4

Browse files
committed
Add solution #1286
1 parent 3430100 commit c2db5c4

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,209 LeetCode solutions in JavaScript
1+
# 1,210 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -970,6 +970,7 @@
970970
1277|[Count Square Submatrices with All Ones](./solutions/1277-count-square-submatrices-with-all-ones.js)|Medium|
971971
1278|[Palindrome Partitioning III](./solutions/1278-palindrome-partitioning-iii.js)|Hard|
972972
1284|[Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](./solutions/1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js)|Hard|
973+
1286|[Iterator for Combination](./solutions/1286-iterator-for-combination.js)|Medium|
973974
1287|[Element Appearing More Than 25% In Sorted Array](./solutions/1287-element-appearing-more-than-25-in-sorted-array.js)|Easy|
974975
1290|[Convert Binary Number in a Linked List to Integer](./solutions/1290-convert-binary-number-in-a-linked-list-to-integer.js)|Easy|
975976
1291|[Sequential Digits](./solutions/1291-sequential-digits.js)|Medium|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 1286. Iterator for Combination
3+
* https://leetcode.com/problems/iterator-for-combination/
4+
* Difficulty: Medium
5+
*
6+
* Design the CombinationIterator class:
7+
* - CombinationIterator(string characters, int combinationLength) Initializes the object with a
8+
* string characters of sorted distinct lowercase English letters and a number combinationLength
9+
* as arguments.
10+
* - next() Returns the next combination of length combinationLength in lexicographical order.
11+
* - hasNext() Returns true if and only if there exists a next combination.
12+
*/
13+
14+
/**
15+
* @param {string} characters
16+
* @param {number} combinationLength
17+
*/
18+
var CombinationIterator = function(characters, combinationLength) {
19+
this.chars = characters;
20+
this.length = combinationLength;
21+
this.indices = new Array(combinationLength).fill(0).map((_, i) => i);
22+
this.hasMore = true;
23+
};
24+
25+
/**
26+
* @return {string}
27+
*/
28+
CombinationIterator.prototype.next = function() {
29+
const result = this.indices.map(i => this.chars[i]).join('');
30+
31+
this.hasMore = false;
32+
for (let i = this.length - 1; i >= 0; i--) {
33+
if (this.indices[i] < this.chars.length - (this.length - i)) {
34+
this.indices[i]++;
35+
for (let j = i + 1; j < this.length; j++) {
36+
this.indices[j] = this.indices[j - 1] + 1;
37+
}
38+
this.hasMore = true;
39+
break;
40+
}
41+
}
42+
43+
return result;
44+
};
45+
46+
/**
47+
* @return {boolean}
48+
*/
49+
CombinationIterator.prototype.hasNext = function() {
50+
return this.hasMore;
51+
};

0 commit comments

Comments
 (0)