Skip to content

Commit 4b23c1e

Browse files
add 1286
1 parent 0be5794 commit 4b23c1e

File tree

7 files changed

+145
-5
lines changed

7 files changed

+145
-5
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -584,4 +584,5 @@ LeetCode
584584
|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|c|[c++](./src/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281.cpp)|[python](./src/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281.py)|[go](./src/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281.go)|[js](./src/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281.js)|[java](./src/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281.java)|Easy|
585585
|1282|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|c|[c++](./src/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/1282.cpp)|[python](./src/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/1282.py)|[go](./src/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/1282.go)|[js](./src/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/1282.js)|[java](./src/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/1282.java)|Medium|
586586
|1283|[Find the Smallest Divisor Given a Threshold](https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/)|c|[c++](./src/1283-Find-the-Smallest-Divisor-Given-a-Threshold/1283.cpp)|[python](./src/1283-Find-the-Smallest-Divisor-Given-a-Threshold/1283.py)|[go](./src/1283-Find-the-Smallest-Divisor-Given-a-Threshold/1283.go)|[js](./src/1283-Find-the-Smallest-Divisor-Given-a-Threshold/1283.js)|[java](./src/1283-Find-the-Smallest-Divisor-Given-a-Threshold/1283.java)|Medium|
587-
|1284|[Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/)|c|[c++](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.cpp)|[python](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.py)|[go](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.go)|[js](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.js)|[java](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.java)|Hard|
587+
|1284|[Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/)|c|[c++](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.cpp)|[python](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.py)|[go](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.go)|[js](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.js)|[java](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.java)|Hard|
588+
|1286|[Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/)|c|[c++](./src/1286-Iterator-for-Combination/1286.cpp)|[python](./src/1286-Iterator-for-Combination/1286.py)|[go](./src/1286-Iterator-for-Combination/1286.go)|[js](./src/1286-Iterator-for-Combination/1286.js)|[java](./src/1286-Iterator-for-Combination/1286.java)|Easy|
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class CombinationIterator
2+
{
3+
public:
4+
CombinationIterator(string ch, int l)
5+
{
6+
combine(0, l, "", ch);
7+
}
8+
9+
string next()
10+
{
11+
string res = q.front();
12+
q.pop();
13+
return res;
14+
}
15+
16+
bool hasNext()
17+
{
18+
return !q.empty();
19+
}
20+
private:
21+
queue<string> q;
22+
23+
void combine(int s, int k, string cur, string& ch)
24+
{
25+
if (cur.size() == k) {
26+
q.push(cur);
27+
return;
28+
}
29+
30+
for (int i = s; i <= ch.size() - (k - cur.size()); i++)
31+
{
32+
combine(i + 1, k, cur + ch[i], ch);
33+
}
34+
}
35+
};
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
type CombinationIterator struct {
2+
res []string
3+
}
4+
5+
func Constructor(ch string, l int) CombinationIterator {
6+
data := make([]string, 0)
7+
combine(0, l, "", ch, &data)
8+
return CombinationIterator{res:data}
9+
}
10+
11+
func combine(s, l int, cur, ch string, data *[]string) {
12+
if len(cur) == l {
13+
*data = append(*data, cur)
14+
return
15+
}
16+
17+
for i := s; i <= len(ch) - (l - len(cur)); i++ {
18+
combine(i + 1, l, cur + string(ch[i]), ch, data)
19+
}
20+
}
21+
22+
23+
func (this *CombinationIterator) Next() string {
24+
ret := this.res[0]
25+
this.res = this.res[1:]
26+
return ret
27+
}
28+
29+
30+
func (this *CombinationIterator) HasNext() bool {
31+
return len(this.res) != 0
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class CombinationIterator {
2+
3+
public CombinationIterator(String ch, int l) {
4+
combine(0, l, new StringBuilder(), ch);
5+
}
6+
7+
private Queue<String> q = new LinkedList();
8+
9+
private void combine(int s, int k, StringBuilder cur, String ch) {
10+
if (cur.length() == k) {
11+
q.offer(cur.toString());
12+
return;
13+
}
14+
15+
for (int i = s; i <= ch.length() - (k - cur.length()); i++) {
16+
cur.append(ch.charAt(i));
17+
combine(i + 1, k, cur, ch);
18+
cur.deleteCharAt(cur.length() - 1);
19+
}
20+
}
21+
22+
public String next() {
23+
return q.poll();
24+
}
25+
26+
public boolean hasNext() {
27+
return !q.isEmpty();
28+
}
29+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {string} characters
3+
* @param {number} combinationLength
4+
*/
5+
var CombinationIterator = function(ch, l) {
6+
this.res = [];
7+
var combine = (s, cur) => {
8+
if (cur.length == l) {
9+
this.res.push(cur);
10+
return;
11+
}
12+
for (let i = s; i <= ch.length - (l - cur.length); i++) {
13+
combine(i + 1, cur + ch[i]);
14+
}
15+
}
16+
combine(0, "");
17+
};
18+
19+
/**
20+
* @return {string}
21+
*/
22+
CombinationIterator.prototype.next = function() {
23+
return this.res.shift();
24+
};
25+
26+
/**
27+
* @return {boolean}
28+
*/
29+
CombinationIterator.prototype.hasNext = function() {
30+
return this.res.length != 0;
31+
};
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class CombinationIterator:
2+
def __init__(self, ch: str, l: int):
3+
self.data = itertools.combinations(ch, l)
4+
self.last = ch[-l:]
5+
self.res = None
6+
7+
def next(self) -> str:
8+
self.res = "".join(next(self.data))
9+
return self.res
10+
11+
def hasNext(self) -> bool:
12+
return self.last != self.res

src/addProb.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import os, bisect
33

44
# 题目名称
5-
name = "Minimum Number of Flips to Convert Binary Matrix to Zero Matrix"
6-
ID = 1284
7-
url = "https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/"
8-
difficult = "Hard"
5+
name = "Iterator for Combination"
6+
ID = 1286
7+
url = "https://leetcode.com/problems/iterator-for-combination/"
8+
difficult = "Medium"
99
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']
1010

1111

0 commit comments

Comments
 (0)