Skip to content

Commit 5233e00

Browse files
committed
Add solution #386
1 parent 946fb06 commit 5233e00

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@
304304
383|[Ransom Note](./0383-ransom-note.js)|Easy|
305305
384|[Shuffle an Array](./0384-shuffle-an-array.js)|Medium|
306306
385|[Mini Parser](./0385-mini-parser.js)|Medium|
307+
386|[Lexicographical Numbers](./0386-lexicographical-numbers.js)|Medium|
307308
387|[First Unique Character in a String](./0387-first-unique-character-in-a-string.js)|Easy|
308309
389|[Find the Difference](./0389-find-the-difference.js)|Easy|
309310
392|[Is Subsequence](./0392-is-subsequence.js)|Easy|
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 386. Lexicographical Numbers
3+
* https://leetcode.com/problems/lexicographical-numbers/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical order.
7+
*
8+
* You must write an algorithm that runs in O(n) time.
9+
*/
10+
11+
/**
12+
* @param {number} n
13+
* @return {number[]}
14+
*/
15+
var lexicalOrder = function(n) {
16+
const result = [];
17+
let value = 1;
18+
19+
for (let i = 0; i < n; i++) {
20+
result.push(value);
21+
if (value * 10 <= n) {
22+
value *= 10;
23+
} else {
24+
while (value % 10 === 9 || value >= n) {
25+
value = Math.floor(value / 10);
26+
}
27+
value++;
28+
}
29+
}
30+
31+
return result;
32+
};

0 commit comments

Comments
 (0)