※ 引述《mikemagic88 (Mikemagic88)》之銘言:
: 使用者輸入1 印1-9
: 使用者輸入2 印1-98 (11, 22, 33等重複的不印)
: 使用者輸入3 印1-987 (121, 988, 667等有重複的不印)
用 dart 語言實作
dartpad link:
https://dartpad.dartlang.org/c23e2b944f1a93201e75591d0c000baf
code:
main() {
Stopwatch timer = new Stopwatch();
int n = 6;
timer.start();
Iterable<String> result = genUniDigitNumber(n).toList();
print('${timer.elapsedMilliseconds} ms');
print('result length: ${result.length}');
print(result);
}
Iterable<String> genUniDigitNumber([int n = 1, List<String> l0]) sync* {
l0 ??= ['', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
if (n == 1) {
yield* l0..remove('');
} else {
for (String i in l0) {
List<String> l1 = l0.toList();
if (i != '' && (l1..remove(i)).contains('')) l1[0] = '0';
for (String k in genUniDigitNumber(n - 1, l1)) yield '$i$k';
}
}
}