cs: add «386. Lexicographical Numbers»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
52718bec4d
commit
2e1df9c0e0
1 changed files with 23 additions and 0 deletions
23
cs/lexicographical-numbers.cs
Normal file
23
cs/lexicographical-numbers.cs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue