1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-10-18 14:52:09 +02:00
LeetCode/cs/lexicographical-numbers.cs

24 lines
520 B
C#
Raw Normal View History

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