Created
February 26, 2017 01:56
-
-
Save hxsf/c270c2a85dcbb0bdc00305fae6304865 to your computer and use it in GitHub Desktop.
实现长度为 m 的字符串 a 到长度为 n 的字符串 b 的遍历
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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