cs: add «386. Lexicographical Numbers»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-09-22 21:41:11 +02:00
parent 52718bec4d
commit 2e1df9c0e0
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,23 @@
public class Solution {
public IList<int> LexicalOrder(int n) {
var numbers = new List<int>();
var current = 1;
for (int i = 0; i < n; ++i) {
numbers.Add(current);
if (current * 10 <= n) {
current *= 10;
continue;
}
// backtrack and try next
while (current >= n || current % 10 == 9) {
current /= 10;
}
++current;
}
return numbers;
}
}