Skip to content

Instantly share code, notes, and snippets.

@hxsf
Created February 26, 2017 01:56
Show Gist options
  • Save hxsf/c270c2a85dcbb0bdc00305fae6304865 to your computer and use it in GitHub Desktop.
Save hxsf/c270c2a85dcbb0bdc00305fae6304865 to your computer and use it in GitHub Desktop.
实现长度为 m 的字符串 a 到长度为 n 的字符串 b 的遍历
function* gen(min, max) {
var arr = new Array(min).fill(0)
arr[arr.length - 1] = 1
while (arr.length <= max) {
yield arr.map(x => String.fromCharCode(96 + x)).join('')
arr[arr.length - 1]++
for (var i = arr.length - 1; i >= 0; i--) {
if (arr[i] > 26) {
arr[i] -= 26
if (i == 0) {
arr.unshift(1)
} else {
arr[i - 1] += 1
}
}
}
}
}
// useage
var g = gen(1,2)
while (true) {
var d = g.next()
if (d.done) {
break
}
console.log(d.value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment