From 7dbfda8c9c6f4b6d9b3f3dca4350b3ea6360eef0 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 3 Jun 2024 19:27:00 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.386 No.0386.Lexicographical Numbers --- .../0386.Lexicographical Numbers/README.md | 232 +++++++----------- .../0386.Lexicographical Numbers/README_EN.md | 232 +++++++----------- .../0386.Lexicographical Numbers/Solution.cpp | 19 +- .../0386.Lexicographical Numbers/Solution.go | 26 +- .../Solution.java | 25 +- .../0386.Lexicographical Numbers/Solution.js | 22 +- .../0386.Lexicographical Numbers/Solution.py | 18 +- .../0386.Lexicographical Numbers/Solution.rs | 27 +- .../0386.Lexicographical Numbers/Solution.ts | 16 ++ .../Solution2.cpp | 17 -- .../0386.Lexicographical Numbers/Solution2.go | 16 -- .../Solution2.java | 18 -- 12 files changed, 267 insertions(+), 401 deletions(-) create mode 100644 solution/0300-0399/0386.Lexicographical Numbers/Solution.ts delete mode 100644 solution/0300-0399/0386.Lexicographical Numbers/Solution2.cpp delete mode 100644 solution/0300-0399/0386.Lexicographical Numbers/Solution2.go delete mode 100644 solution/0300-0399/0386.Lexicographical Numbers/Solution2.java diff --git a/solution/0300-0399/0386.Lexicographical Numbers/README.md b/solution/0300-0399/0386.Lexicographical Numbers/README.md index 6b75dc161a0cb..320ca2c8811d3 100644 --- a/solution/0300-0399/0386.Lexicographical Numbers/README.md +++ b/solution/0300-0399/0386.Lexicographical Numbers/README.md @@ -51,7 +51,11 @@ tags: -### 方法一:DFS +### 方法一:迭代 + +我们首先定义一个变量 $v$,初始时 $v = 1$。然后我们从 $1$ 开始迭代,每次迭代都将 $v$ 添加到答案数组中。然后,如果 $v \times 10 \leq n$,我们将 $v$ 更新为 $v \times 10$;否则,如果 $v \bmod 10 = 9$ 或者 $v + 1 > n$,我们就循环将 $v$ 除以 $10$。循环结束后,我们将 $v$ 加一。继续迭代,直到我们添加了 $n$ 个数到答案数组中。 + +时间复杂度 $O(n)$,其中 $n$ 是给定的整数 $n$。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 @@ -60,16 +64,16 @@ tags: ```python class Solution: def lexicalOrder(self, n: int) -> List[int]: - def dfs(u): - if u > n: - return - ans.append(u) - for i in range(10): - dfs(u * 10 + i) - ans = [] - for i in range(1, 10): - dfs(i) + v = 1 + for _ in range(n): + ans.append(v) + if v * 10 <= n: + v *= 10 + else: + while v % 10 == 9 or v + 1 > n: + v //= 10 + v += 1 return ans ``` @@ -78,131 +82,7 @@ class Solution: ```java class Solution { public List lexicalOrder(int n) { - List ans = new ArrayList<>(); - for (int i = 1; i < 10; ++i) { - dfs(i, n, ans); - } - return ans; - } - - private void dfs(int u, int n, List ans) { - if (u > n) { - return; - } - ans.add(u); - for (int i = 0; i < 10; ++i) { - dfs(u * 10 + i, n, ans); - } - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - vector lexicalOrder(int n) { - vector ans; - for (int i = 1; i < 10; ++i) dfs(i, n, ans); - return ans; - } - - void dfs(int u, int n, vector& ans) { - if (u > n) return; - ans.push_back(u); - for (int i = 0; i < 10; ++i) dfs(u * 10 + i, n, ans); - } -}; -``` - -#### Go - -```go -func lexicalOrder(n int) []int { - var ans []int - var dfs func(u int) - dfs = func(u int) { - if u > n { - return - } - ans = append(ans, u) - for i := 0; i < 10; i++ { - dfs(u*10 + i) - } - } - for i := 1; i < 10; i++ { - dfs(i) - } - return ans -} -``` - -#### Rust - -```rust -impl Solution { - fn dfs(mut num: i32, n: i32, res: &mut Vec) { - if num > n { - return; - } - res.push(num); - for i in 0..10 { - Self::dfs(num * 10 + i, n, res); - } - } - - pub fn lexical_order(n: i32) -> Vec { - let mut res = vec![]; - for i in 1..10 { - Self::dfs(i, n, &mut res); - } - res - } -} -``` - -#### JavaScript - -```js -/** - * @param {number} n - * @return {number[]} - */ -var lexicalOrder = function (n) { - let ans = []; - function dfs(u) { - if (u > n) { - return; - } - ans.push(u); - for (let i = 0; i < 10; ++i) { - dfs(u * 10 + i); - } - } - for (let i = 1; i < 10; ++i) { - dfs(i); - } - return ans; -}; -``` - - - - - - - -### 方法二 - - - -#### Java - -```java -class Solution { - public List lexicalOrder(int n) { - List ans = new ArrayList<>(); + List ans = new ArrayList<>(n); int v = 1; for (int i = 0; i < n; ++i) { ans.add(v); @@ -230,10 +110,12 @@ public: int v = 1; for (int i = 0; i < n; ++i) { ans.push_back(v); - if (v * 10 <= n) + if (v * 10 <= n) { v *= 10; - else { - while (v % 10 == 9 || v + 1 > n) v /= 10; + } else { + while (v % 10 == 9 || v + 1 > n) { + v /= 10; + } ++v; } } @@ -245,8 +127,7 @@ public: #### Go ```go -func lexicalOrder(n int) []int { - var ans []int +func lexicalOrder(n int) (ans []int) { v := 1 for i := 0; i < n; i++ { ans = append(ans, v) @@ -259,10 +140,79 @@ func lexicalOrder(n int) []int { v++ } } - return ans + return +} +``` + +#### TypeScript + +```ts +function lexicalOrder(n: number): number[] { + const ans: number[] = []; + let v = 1; + for (let i = 0; i < n; ++i) { + ans.push(v); + if (v * 10 <= n) { + v *= 10; + } else { + while (v % 10 === 9 || v === n) { + v = Math.floor(v / 10); + } + ++v; + } + } + return ans; } ``` +#### Rust + +```rust +impl Solution { + pub fn lexical_order(n: i32) -> Vec { + let mut ans = Vec::with_capacity(n as usize); + let mut v = 1; + for _ in 0..n { + ans.push(v); + if v * 10 <= n { + v *= 10; + } else { + while v % 10 == 9 || v + 1 > n { + v /= 10; + } + v += 1; + } + } + ans + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} n + * @return {number[]} + */ +var lexicalOrder = function (n) { + const ans = []; + let v = 1; + for (let i = 0; i < n; ++i) { + ans.push(v); + if (v * 10 <= n) { + v *= 10; + } else { + while (v % 10 === 9 || v === n) { + v = Math.floor(v / 10); + } + ++v; + } + } + return ans; +}; +``` + diff --git a/solution/0300-0399/0386.Lexicographical Numbers/README_EN.md b/solution/0300-0399/0386.Lexicographical Numbers/README_EN.md index c9f25bca6a202..0575a4d9ef39b 100644 --- a/solution/0300-0399/0386.Lexicographical Numbers/README_EN.md +++ b/solution/0300-0399/0386.Lexicographical Numbers/README_EN.md @@ -42,7 +42,11 @@ tags: -### Solution 1 +### Solution 1: Iteration + +We first define a variable $v$, initially $v = 1$. Then we start iterating from $1$, adding $v$ to the answer array each time. Then, if $v \times 10 \leq n$, we update $v$ to $v \times 10$; otherwise, if $v \bmod 10 = 9$ or $v + 1 > n$, we loop to divide $v$ by $10$. After the loop ends, we increment $v$. Continue iterating until we have added $n$ numbers to the answer array. + +The time complexity is $O(n)$, where $n$ is the given integer $n$. Ignoring the space consumption of the answer array, the space complexity is $O(1)$. @@ -51,16 +55,16 @@ tags: ```python class Solution: def lexicalOrder(self, n: int) -> List[int]: - def dfs(u): - if u > n: - return - ans.append(u) - for i in range(10): - dfs(u * 10 + i) - ans = [] - for i in range(1, 10): - dfs(i) + v = 1 + for _ in range(n): + ans.append(v) + if v * 10 <= n: + v *= 10 + else: + while v % 10 == 9 or v + 1 > n: + v //= 10 + v += 1 return ans ``` @@ -69,131 +73,7 @@ class Solution: ```java class Solution { public List lexicalOrder(int n) { - List ans = new ArrayList<>(); - for (int i = 1; i < 10; ++i) { - dfs(i, n, ans); - } - return ans; - } - - private void dfs(int u, int n, List ans) { - if (u > n) { - return; - } - ans.add(u); - for (int i = 0; i < 10; ++i) { - dfs(u * 10 + i, n, ans); - } - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - vector lexicalOrder(int n) { - vector ans; - for (int i = 1; i < 10; ++i) dfs(i, n, ans); - return ans; - } - - void dfs(int u, int n, vector& ans) { - if (u > n) return; - ans.push_back(u); - for (int i = 0; i < 10; ++i) dfs(u * 10 + i, n, ans); - } -}; -``` - -#### Go - -```go -func lexicalOrder(n int) []int { - var ans []int - var dfs func(u int) - dfs = func(u int) { - if u > n { - return - } - ans = append(ans, u) - for i := 0; i < 10; i++ { - dfs(u*10 + i) - } - } - for i := 1; i < 10; i++ { - dfs(i) - } - return ans -} -``` - -#### Rust - -```rust -impl Solution { - fn dfs(mut num: i32, n: i32, res: &mut Vec) { - if num > n { - return; - } - res.push(num); - for i in 0..10 { - Self::dfs(num * 10 + i, n, res); - } - } - - pub fn lexical_order(n: i32) -> Vec { - let mut res = vec![]; - for i in 1..10 { - Self::dfs(i, n, &mut res); - } - res - } -} -``` - -#### JavaScript - -```js -/** - * @param {number} n - * @return {number[]} - */ -var lexicalOrder = function (n) { - let ans = []; - function dfs(u) { - if (u > n) { - return; - } - ans.push(u); - for (let i = 0; i < 10; ++i) { - dfs(u * 10 + i); - } - } - for (let i = 1; i < 10; ++i) { - dfs(i); - } - return ans; -}; -``` - - - - - - - -### Solution 2 - - - -#### Java - -```java -class Solution { - public List lexicalOrder(int n) { - List ans = new ArrayList<>(); + List ans = new ArrayList<>(n); int v = 1; for (int i = 0; i < n; ++i) { ans.add(v); @@ -221,10 +101,12 @@ public: int v = 1; for (int i = 0; i < n; ++i) { ans.push_back(v); - if (v * 10 <= n) + if (v * 10 <= n) { v *= 10; - else { - while (v % 10 == 9 || v + 1 > n) v /= 10; + } else { + while (v % 10 == 9 || v + 1 > n) { + v /= 10; + } ++v; } } @@ -236,8 +118,7 @@ public: #### Go ```go -func lexicalOrder(n int) []int { - var ans []int +func lexicalOrder(n int) (ans []int) { v := 1 for i := 0; i < n; i++ { ans = append(ans, v) @@ -250,10 +131,79 @@ func lexicalOrder(n int) []int { v++ } } - return ans + return +} +``` + +#### TypeScript + +```ts +function lexicalOrder(n: number): number[] { + const ans: number[] = []; + let v = 1; + for (let i = 0; i < n; ++i) { + ans.push(v); + if (v * 10 <= n) { + v *= 10; + } else { + while (v % 10 === 9 || v === n) { + v = Math.floor(v / 10); + } + ++v; + } + } + return ans; } ``` +#### Rust + +```rust +impl Solution { + pub fn lexical_order(n: i32) -> Vec { + let mut ans = Vec::with_capacity(n as usize); + let mut v = 1; + for _ in 0..n { + ans.push(v); + if v * 10 <= n { + v *= 10; + } else { + while v % 10 == 9 || v + 1 > n { + v /= 10; + } + v += 1; + } + } + ans + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} n + * @return {number[]} + */ +var lexicalOrder = function (n) { + const ans = []; + let v = 1; + for (let i = 0; i < n; ++i) { + ans.push(v); + if (v * 10 <= n) { + v *= 10; + } else { + while (v % 10 === 9 || v === n) { + v = Math.floor(v / 10); + } + ++v; + } + } + return ans; +}; +``` + diff --git a/solution/0300-0399/0386.Lexicographical Numbers/Solution.cpp b/solution/0300-0399/0386.Lexicographical Numbers/Solution.cpp index ccf7ec79db4fa..4f18e564b06dc 100644 --- a/solution/0300-0399/0386.Lexicographical Numbers/Solution.cpp +++ b/solution/0300-0399/0386.Lexicographical Numbers/Solution.cpp @@ -2,13 +2,18 @@ class Solution { public: vector lexicalOrder(int n) { vector ans; - for (int i = 1; i < 10; ++i) dfs(i, n, ans); + int v = 1; + for (int i = 0; i < n; ++i) { + ans.push_back(v); + if (v * 10 <= n) { + v *= 10; + } else { + while (v % 10 == 9 || v + 1 > n) { + v /= 10; + } + ++v; + } + } return ans; } - - void dfs(int u, int n, vector& ans) { - if (u > n) return; - ans.push_back(u); - for (int i = 0; i < 10; ++i) dfs(u * 10 + i, n, ans); - } }; \ No newline at end of file diff --git a/solution/0300-0399/0386.Lexicographical Numbers/Solution.go b/solution/0300-0399/0386.Lexicographical Numbers/Solution.go index dd0622ffa1e67..e61a27a6448c9 100644 --- a/solution/0300-0399/0386.Lexicographical Numbers/Solution.go +++ b/solution/0300-0399/0386.Lexicographical Numbers/Solution.go @@ -1,17 +1,15 @@ -func lexicalOrder(n int) []int { - var ans []int - var dfs func(u int) - dfs = func(u int) { - if u > n { - return +func lexicalOrder(n int) (ans []int) { + v := 1 + for i := 0; i < n; i++ { + ans = append(ans, v) + if v*10 <= n { + v *= 10 + } else { + for v%10 == 9 || v+1 > n { + v /= 10 + } + v++ } - ans = append(ans, u) - for i := 0; i < 10; i++ { - dfs(u*10 + i) - } - } - for i := 1; i < 10; i++ { - dfs(i) } - return ans + return } \ No newline at end of file diff --git a/solution/0300-0399/0386.Lexicographical Numbers/Solution.java b/solution/0300-0399/0386.Lexicographical Numbers/Solution.java index 6716bacebda33..e4ec7850aac06 100644 --- a/solution/0300-0399/0386.Lexicographical Numbers/Solution.java +++ b/solution/0300-0399/0386.Lexicographical Numbers/Solution.java @@ -1,19 +1,18 @@ class Solution { public List lexicalOrder(int n) { - List ans = new ArrayList<>(); - for (int i = 1; i < 10; ++i) { - dfs(i, n, ans); + List ans = new ArrayList<>(n); + int v = 1; + for (int i = 0; i < n; ++i) { + ans.add(v); + if (v * 10 <= n) { + v *= 10; + } else { + while (v % 10 == 9 || v + 1 > n) { + v /= 10; + } + ++v; + } } return ans; } - - private void dfs(int u, int n, List ans) { - if (u > n) { - return; - } - ans.add(u); - for (int i = 0; i < 10; ++i) { - dfs(u * 10 + i, n, ans); - } - } } \ No newline at end of file diff --git a/solution/0300-0399/0386.Lexicographical Numbers/Solution.js b/solution/0300-0399/0386.Lexicographical Numbers/Solution.js index 9946d7a30490d..45f7c4bb57878 100644 --- a/solution/0300-0399/0386.Lexicographical Numbers/Solution.js +++ b/solution/0300-0399/0386.Lexicographical Numbers/Solution.js @@ -3,18 +3,18 @@ * @return {number[]} */ var lexicalOrder = function (n) { - let ans = []; - function dfs(u) { - if (u > n) { - return; + const ans = []; + let v = 1; + for (let i = 0; i < n; ++i) { + ans.push(v); + if (v * 10 <= n) { + v *= 10; + } else { + while (v % 10 === 9 || v === n) { + v = Math.floor(v / 10); + } + ++v; } - ans.push(u); - for (let i = 0; i < 10; ++i) { - dfs(u * 10 + i); - } - } - for (let i = 1; i < 10; ++i) { - dfs(i); } return ans; }; diff --git a/solution/0300-0399/0386.Lexicographical Numbers/Solution.py b/solution/0300-0399/0386.Lexicographical Numbers/Solution.py index 1109325ba1b89..966ad614913e8 100644 --- a/solution/0300-0399/0386.Lexicographical Numbers/Solution.py +++ b/solution/0300-0399/0386.Lexicographical Numbers/Solution.py @@ -1,13 +1,13 @@ class Solution: def lexicalOrder(self, n: int) -> List[int]: - def dfs(u): - if u > n: - return - ans.append(u) - for i in range(10): - dfs(u * 10 + i) - ans = [] - for i in range(1, 10): - dfs(i) + v = 1 + for _ in range(n): + ans.append(v) + if v * 10 <= n: + v *= 10 + else: + while v % 10 == 9 or v + 1 > n: + v //= 10 + v += 1 return ans diff --git a/solution/0300-0399/0386.Lexicographical Numbers/Solution.rs b/solution/0300-0399/0386.Lexicographical Numbers/Solution.rs index a0b2c1d8a6a71..6b356a438fa13 100644 --- a/solution/0300-0399/0386.Lexicographical Numbers/Solution.rs +++ b/solution/0300-0399/0386.Lexicographical Numbers/Solution.rs @@ -1,19 +1,18 @@ impl Solution { - fn dfs(mut num: i32, n: i32, res: &mut Vec) { - if num > n { - return; - } - res.push(num); - for i in 0..10 { - Self::dfs(num * 10 + i, n, res); - } - } - pub fn lexical_order(n: i32) -> Vec { - let mut res = vec![]; - for i in 1..10 { - Self::dfs(i, n, &mut res); + let mut ans = Vec::with_capacity(n as usize); + let mut v = 1; + for _ in 0..n { + ans.push(v); + if v * 10 <= n { + v *= 10; + } else { + while v % 10 == 9 || v + 1 > n { + v /= 10; + } + v += 1; + } } - res + ans } } diff --git a/solution/0300-0399/0386.Lexicographical Numbers/Solution.ts b/solution/0300-0399/0386.Lexicographical Numbers/Solution.ts new file mode 100644 index 0000000000000..35cbf4af0aa0c --- /dev/null +++ b/solution/0300-0399/0386.Lexicographical Numbers/Solution.ts @@ -0,0 +1,16 @@ +function lexicalOrder(n: number): number[] { + const ans: number[] = []; + let v = 1; + for (let i = 0; i < n; ++i) { + ans.push(v); + if (v * 10 <= n) { + v *= 10; + } else { + while (v % 10 === 9 || v === n) { + v = Math.floor(v / 10); + } + ++v; + } + } + return ans; +} diff --git a/solution/0300-0399/0386.Lexicographical Numbers/Solution2.cpp b/solution/0300-0399/0386.Lexicographical Numbers/Solution2.cpp deleted file mode 100644 index 1e2746fd3e240..0000000000000 --- a/solution/0300-0399/0386.Lexicographical Numbers/Solution2.cpp +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - vector lexicalOrder(int n) { - vector ans; - int v = 1; - for (int i = 0; i < n; ++i) { - ans.push_back(v); - if (v * 10 <= n) - v *= 10; - else { - while (v % 10 == 9 || v + 1 > n) v /= 10; - ++v; - } - } - return ans; - } -}; \ No newline at end of file diff --git a/solution/0300-0399/0386.Lexicographical Numbers/Solution2.go b/solution/0300-0399/0386.Lexicographical Numbers/Solution2.go deleted file mode 100644 index 1cb885b1567a7..0000000000000 --- a/solution/0300-0399/0386.Lexicographical Numbers/Solution2.go +++ /dev/null @@ -1,16 +0,0 @@ -func lexicalOrder(n int) []int { - var ans []int - v := 1 - for i := 0; i < n; i++ { - ans = append(ans, v) - if v*10 <= n { - v *= 10 - } else { - for v%10 == 9 || v+1 > n { - v /= 10 - } - v++ - } - } - return ans -} \ No newline at end of file diff --git a/solution/0300-0399/0386.Lexicographical Numbers/Solution2.java b/solution/0300-0399/0386.Lexicographical Numbers/Solution2.java deleted file mode 100644 index a7cf5b1b9dccb..0000000000000 --- a/solution/0300-0399/0386.Lexicographical Numbers/Solution2.java +++ /dev/null @@ -1,18 +0,0 @@ -class Solution { - public List lexicalOrder(int n) { - List ans = new ArrayList<>(); - int v = 1; - for (int i = 0; i < n; ++i) { - ans.add(v); - if (v * 10 <= n) { - v *= 10; - } else { - while (v % 10 == 9 || v + 1 > n) { - v /= 10; - } - ++v; - } - } - return ans; - } -} \ No newline at end of file