LeetCode/cs/lexicographical-numbers.cs
Matej Focko 2e1df9c0e0
cs: add «386. Lexicographical Numbers»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-09-22 21:41:11 +02:00

23 lines
520 B
C#

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;
}
}