Re: [问题] 1-9位数不重复印出来 (dart)

楼主: ticore ({id:"Ticore"};//)   2016-12-13 19:19:20
※ 引述《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';
}
}
}

Links booklink

Contact Us: admin [ a t ] ucptt.com