Skip to content

Commit 034db12

Browse files
add 1286
1 parent ede2ed6 commit 034db12

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.github.contest
22

33

4+
import com.github.contest.design.wrapper
45
import com.github.contest.math.numberOfPowerfulInt
56
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
67
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
7-
import com.github.contest.slidingWindow.longestSubarray
88
import com.github.contest.strings.fullJustify
99
import com.github.contest.strings.subStrHash
1010
import java.util.TreeMap
@@ -16,7 +16,7 @@ import java.util.TreeMap
1616

1717
fun main() {
1818

19-
longestSubarray(intArrayOf(8, 2, 4, 7), 4).also { println(it) }
19+
wrapper()
2020
}
2121

2222

contest/src/main/java/com/github/contest/design/DesignLeetcode.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,44 @@ class PeekingIterator(iterator: Iterator<Int>) : Iterator<Int> {
205205
override fun hasNext(): Boolean {
206206
return store.isNotEmpty()
207207
}
208+
}
209+
210+
/**
211+
* 1286. Iterator for Combination
212+
*/
213+
214+
class CombinationIterator(characters: String, combinationLength: Int) {
215+
216+
private val store = mutableListOf<String>()
217+
218+
init {
219+
combine(0, characters, StringBuilder(), store, combinationLength)
220+
}
221+
222+
fun next(): String = store.removeFirst()
223+
224+
fun hasNext(): Boolean = store.isNotEmpty()
225+
226+
private fun combine(
227+
index: Int,
228+
str: String,
229+
subset: StringBuilder,
230+
store: MutableList<String>,
231+
limit: Int
232+
) {
233+
234+
if (subset.length == limit) {
235+
store.add(subset.toString())
236+
return
237+
}
238+
239+
if (index == str.length) return
240+
241+
subset.append(str[index])
242+
combine(index + 1, str, subset, store, limit)
243+
244+
subset.deleteAt(subset.length - 1)
245+
combine(index + 1, str, subset, store, limit)
246+
}
247+
208248
}

0 commit comments

Comments
 (0)