|
| 1 | +--- |
| 2 | +id: lexicographical-numbers |
| 3 | +title: Lexicographical-Numbers |
| 4 | +sidebar_label: 0389-Lexicographical-Numbers |
| 5 | +tags: |
| 6 | + - Leet code |
| 7 | +description: "Solution to leetocde 386" |
| 8 | +--- |
| 9 | + |
| 10 | +### Problem Description |
| 11 | + |
| 12 | +Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order. |
| 13 | + |
| 14 | +You must write an algorithm that runs in $O(n)$ time and uses $O(1)$ extra space. |
| 15 | + |
| 16 | +Example 1: |
| 17 | + |
| 18 | +``` |
| 19 | +Input: n = 13 |
| 20 | +Output: [1,10,11,12,13,2,3,4,5,6,7,8,9] |
| 21 | +
|
| 22 | +``` |
| 23 | + |
| 24 | +Example 2: |
| 25 | + |
| 26 | +``` |
| 27 | +Input: n = 2 |
| 28 | +Output: [1,2] |
| 29 | +
|
| 30 | +``` |
| 31 | + |
| 32 | +### Constraints: |
| 33 | + |
| 34 | +- `1 <= n <= 5 * 10^4` |
| 35 | + |
| 36 | +### Algorithm |
| 37 | + |
| 38 | +The basic idea is to find the next number to add. |
| 39 | +Take 45 for example: if the current number is 45, the next one will be `450 (450 == 45 * 10)(if 450 <= n)`, or `46 (46 == 45 + 1) (if 46 <= n)` or `5 (5 == 45 / 10 + 1)`(5 is less than 45 so it is for sure less than n). |
| 40 | +We should also consider `n = 600`, and the current number = 499, the next number is `5` because there are all "9"s after "4" in `"499"` so we should divide `499 by 10` until the last digit is not `"9"`. |
| 41 | +It is like a tree, and we are easy to get a sibling, a left most child and the parent of any node. |
| 42 | + |
| 43 | +### Code Implementation |
| 44 | + |
| 45 | +**Python:** |
| 46 | + |
| 47 | +```python |
| 48 | +def lexicalOrder(self, n): |
| 49 | + top = 1 |
| 50 | + while top * 10 <= n: |
| 51 | + top *= 10 |
| 52 | + def mycmp(a, b, top=top): |
| 53 | + while a < top: a *= 10 |
| 54 | + while b < top: b *= 10 |
| 55 | + return -1 if a < b else b < a |
| 56 | + return sorted(xrange(1, n+1), mycmp) |
| 57 | + |
| 58 | +``` |
| 59 | + |
| 60 | +**C++:** |
| 61 | + |
| 62 | +```c++ |
| 63 | +class Solution { |
| 64 | +public: |
| 65 | + vector<int> lexicalOrder(int n) { |
| 66 | + vector<int> res(n); |
| 67 | + int cur = 1; |
| 68 | + for (int i = 0; i < n; i++) { |
| 69 | + res[i] = cur; |
| 70 | + if (cur * 10 <= n) { |
| 71 | + cur *= 10; |
| 72 | + } else { |
| 73 | + if (cur >= n) |
| 74 | + cur /= 10; |
| 75 | + cur += 1; |
| 76 | + while (cur % 10 == 0) |
| 77 | + cur /= 10; |
| 78 | + } |
| 79 | + } |
| 80 | + return res; |
| 81 | + } |
| 82 | +}; |
| 83 | +``` |
| 84 | +
|
| 85 | +**Java:** |
| 86 | +
|
| 87 | +```java |
| 88 | +public List<Integer> lexicalOrder(int n) { |
| 89 | + List<Integer> list = new ArrayList<>(n); |
| 90 | + int curr = 1; |
| 91 | + for (int i = 1; i <= n; i++) { |
| 92 | + list.add(curr); |
| 93 | + if (curr * 10 <= n) { |
| 94 | + curr *= 10; |
| 95 | + } else if (curr % 10 != 9 && curr + 1 <= n) { |
| 96 | + curr++; |
| 97 | + } else { |
| 98 | + while ((curr / 10) % 10 == 9) { |
| 99 | + curr /= 10; |
| 100 | + } |
| 101 | + curr = curr / 10 + 1; |
| 102 | + } |
| 103 | + } |
| 104 | + return list; |
| 105 | + } |
| 106 | +``` |
| 107 | + |
0 commit comments