for (var len = MIN_LENGTH; len <= MAX_LENGTH; len++) { var word_array = []; for (var j = 0; j < len; j++) { word_array[j] = 0 }
while (true) { var word = ''; for (var k = 0; k < len; k++) { word += String.fromCharCode(97 + word_array[k]); } console.log(word);
for (k = len - 1; k >= 0; k--) { if (word_array[k] < 25) { break; } }
if (k == -1) { break; }
word_array[k]++; for (k++; k < len; k++) { word_array[k] = 0; } } }
des
2017-02-26 06:57:20 +08:00
@starvedcat 换我应该会按照一楼说的方式,预先计算 m 到 n 有多少个及总共有多少个,从最小合适的数(如果最少为一位,从零开始) 然后通过类似进制转换的方式进行映射
aheadlead
2017-02-26 07:04:06 +08:00
刚醒 抱歉不是很会写 js 用 python 代替吧
def rich(length): ....alphabet = [chr(_) for _ in range(ord('a'), ord('z')+1)] ....fu = [[], alphabet] ....for i in range(1, length): ........fu.append([c + s for c in alphabet for s in fu[1]]) ....return fu
def rich(length): ....alphabet = [chr(_) for _ in range(ord('a'), ord('z')+1)] ....fu = [[], alphabet] ....for i in range(1, length): ........fu.append([c + s for c in alphabet for s in fu[i]]) # 在这里 ....return fu
忘记加个变量 cache 一下... def hah(length): ....if length == 0: return [[""]] ....cached = hah(length-1) ....return cached + [[c+chr(97+i) for c in (cached[-1]) for i in range(26)]]