From 56a25f48119dcb046402036ba5293fc25acbb89e Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 30 Oct 2025 07:20:59 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1528 --- .../1500-1599/1528.Shuffle String/README.md | 50 +++++++++++++++---- .../1528.Shuffle String/README_EN.md | 50 +++++++++++++++---- .../1528.Shuffle String/Solution.java | 4 +- .../1500-1599/1528.Shuffle String/Solution.js | 6 +-- .../1500-1599/1528.Shuffle String/Solution.py | 8 +-- .../1500-1599/1528.Shuffle String/Solution.rs | 11 ++++ .../1500-1599/1528.Shuffle String/Solution.ts | 7 +++ 7 files changed, 109 insertions(+), 27 deletions(-) create mode 100644 solution/1500-1599/1528.Shuffle String/Solution.rs create mode 100644 solution/1500-1599/1528.Shuffle String/Solution.ts diff --git a/solution/1500-1599/1528.Shuffle String/README.md b/solution/1500-1599/1528.Shuffle String/README.md index 7375a027abcc2..afc1052fbb971 100644 --- a/solution/1500-1599/1528.Shuffle String/README.md +++ b/solution/1500-1599/1528.Shuffle String/README.md @@ -63,7 +63,11 @@ tags: -### 方法一 +### 方法一:模拟 + +我们创建一个与字符串长度相同的字符数组或字符串 $\textit{ans}$,然后遍历字符串 $\textit{s}$,将每个字符 $\textit{s}[i]$ 放置到 $\textit{ans}$ 的 $\textit{indices}[i]$ 位置上。最后将字符数组或字符串 $\textit{ans}$ 拼接成最终结果并返回。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是字符串的长度。 @@ -72,10 +76,10 @@ tags: ```python class Solution: def restoreString(self, s: str, indices: List[int]) -> str: - ans = [0] * len(s) - for i, c in enumerate(s): - ans[indices[i]] = c - return ''.join(ans) + ans = [None] * len(s) + for c, j in zip(s, indices): + ans[j] = c + return "".join(ans) ``` #### Java @@ -88,7 +92,7 @@ class Solution { for (int i = 0; i < n; ++i) { ans[indices[i]] = s.charAt(i); } - return String.valueOf(ans); + return new String(ans); } } ``` @@ -121,6 +125,34 @@ func restoreString(s string, indices []int) string { } ``` +#### TypeScript + +```ts +function restoreString(s: string, indices: number[]): string { + const ans: string[] = []; + for (let i = 0; i < s.length; i++) { + ans[indices[i]] = s[i]; + } + return ans.join(''); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn restore_string(s: String, indices: Vec) -> String { + let n = s.len(); + let mut ans = vec![' '; n]; + let chars: Vec = s.chars().collect(); + for i in 0..n { + ans[indices[i] as usize] = chars[i]; + } + ans.iter().collect() + } +} +``` + #### JavaScript ```js @@ -130,11 +162,11 @@ func restoreString(s string, indices []int) string { * @return {string} */ var restoreString = function (s, indices) { - let rs = []; + const ans = []; for (let i = 0; i < s.length; i++) { - rs[indices[i]] = s[i]; + ans[indices[i]] = s[i]; } - return rs.join(''); + return ans.join(''); }; ``` diff --git a/solution/1500-1599/1528.Shuffle String/README_EN.md b/solution/1500-1599/1528.Shuffle String/README_EN.md index b5e7e6778064f..8451189373a69 100644 --- a/solution/1500-1599/1528.Shuffle String/README_EN.md +++ b/solution/1500-1599/1528.Shuffle String/README_EN.md @@ -57,7 +57,11 @@ tags: -### Solution 1 +### Solution 1: Simulation + +We create a character array or string $\textit{ans}$ of the same length as the input string, then iterate through the string $\textit{s}$ and place each character $\textit{s}[i]$ at position $\textit{indices}[i]$ in $\textit{ans}$. Finally, we join the character array or string $\textit{ans}$ to form the final result and return it. + +The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the string. @@ -66,10 +70,10 @@ tags: ```python class Solution: def restoreString(self, s: str, indices: List[int]) -> str: - ans = [0] * len(s) - for i, c in enumerate(s): - ans[indices[i]] = c - return ''.join(ans) + ans = [None] * len(s) + for c, j in zip(s, indices): + ans[j] = c + return "".join(ans) ``` #### Java @@ -82,7 +86,7 @@ class Solution { for (int i = 0; i < n; ++i) { ans[indices[i]] = s.charAt(i); } - return String.valueOf(ans); + return new String(ans); } } ``` @@ -115,6 +119,34 @@ func restoreString(s string, indices []int) string { } ``` +#### TypeScript + +```ts +function restoreString(s: string, indices: number[]): string { + const ans: string[] = []; + for (let i = 0; i < s.length; i++) { + ans[indices[i]] = s[i]; + } + return ans.join(''); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn restore_string(s: String, indices: Vec) -> String { + let n = s.len(); + let mut ans = vec![' '; n]; + let chars: Vec = s.chars().collect(); + for i in 0..n { + ans[indices[i] as usize] = chars[i]; + } + ans.iter().collect() + } +} +``` + #### JavaScript ```js @@ -124,11 +156,11 @@ func restoreString(s string, indices []int) string { * @return {string} */ var restoreString = function (s, indices) { - let rs = []; + const ans = []; for (let i = 0; i < s.length; i++) { - rs[indices[i]] = s[i]; + ans[indices[i]] = s[i]; } - return rs.join(''); + return ans.join(''); }; ``` diff --git a/solution/1500-1599/1528.Shuffle String/Solution.java b/solution/1500-1599/1528.Shuffle String/Solution.java index 4ec4d1225ac6a..85348db1442b8 100644 --- a/solution/1500-1599/1528.Shuffle String/Solution.java +++ b/solution/1500-1599/1528.Shuffle String/Solution.java @@ -5,6 +5,6 @@ public String restoreString(String s, int[] indices) { for (int i = 0; i < n; ++i) { ans[indices[i]] = s.charAt(i); } - return String.valueOf(ans); + return new String(ans); } -} \ No newline at end of file +} diff --git a/solution/1500-1599/1528.Shuffle String/Solution.js b/solution/1500-1599/1528.Shuffle String/Solution.js index 12d27f6f3207e..32ead5ebe58f1 100644 --- a/solution/1500-1599/1528.Shuffle String/Solution.js +++ b/solution/1500-1599/1528.Shuffle String/Solution.js @@ -4,9 +4,9 @@ * @return {string} */ var restoreString = function (s, indices) { - let rs = []; + const ans = []; for (let i = 0; i < s.length; i++) { - rs[indices[i]] = s[i]; + ans[indices[i]] = s[i]; } - return rs.join(''); + return ans.join(''); }; diff --git a/solution/1500-1599/1528.Shuffle String/Solution.py b/solution/1500-1599/1528.Shuffle String/Solution.py index 41bd6d91a41b4..fca610b9b7104 100644 --- a/solution/1500-1599/1528.Shuffle String/Solution.py +++ b/solution/1500-1599/1528.Shuffle String/Solution.py @@ -1,6 +1,6 @@ class Solution: def restoreString(self, s: str, indices: List[int]) -> str: - ans = [0] * len(s) - for i, c in enumerate(s): - ans[indices[i]] = c - return ''.join(ans) + ans = [None] * len(s) + for c, j in zip(s, indices): + ans[j] = c + return "".join(ans) diff --git a/solution/1500-1599/1528.Shuffle String/Solution.rs b/solution/1500-1599/1528.Shuffle String/Solution.rs new file mode 100644 index 0000000000000..375728d0ec84b --- /dev/null +++ b/solution/1500-1599/1528.Shuffle String/Solution.rs @@ -0,0 +1,11 @@ +impl Solution { + pub fn restore_string(s: String, indices: Vec) -> String { + let n = s.len(); + let mut ans = vec![' '; n]; + let chars: Vec = s.chars().collect(); + for i in 0..n { + ans[indices[i] as usize] = chars[i]; + } + ans.iter().collect() + } +} diff --git a/solution/1500-1599/1528.Shuffle String/Solution.ts b/solution/1500-1599/1528.Shuffle String/Solution.ts new file mode 100644 index 0000000000000..db78ab7ada148 --- /dev/null +++ b/solution/1500-1599/1528.Shuffle String/Solution.ts @@ -0,0 +1,7 @@ +function restoreString(s: string, indices: number[]): string { + const ans: string[] = []; + for (let i = 0; i < s.length; i++) { + ans[indices[i]] = s[i]; + } + return ans.join(''); +}