diff --git a/solution/1400-1499/1469.Find All The Lonely Nodes/README.md b/solution/1400-1499/1469.Find All The Lonely Nodes/README.md index 7c2d6f89ead81..15fa8d0ecd445 100644 --- a/solution/1400-1499/1469.Find All The Lonely Nodes/README.md +++ b/solution/1400-1499/1469.Find All The Lonely Nodes/README.md @@ -74,11 +74,16 @@ tags: -### 方法一:递归 +### 方法一:DFS -递归搜索二叉树,如果当前节点的左右子节点都不为空,则继续递归搜索左右子树;如果当前节点的左右子节点有一个为空,则将不为空的子节点的值加入结果数组中,然后继续递归搜索左右子树。 +我们可以使用深度优先搜索遍历整棵树,设计一个函数 $\textit{dfs}$,它的作用是遍历树中的每个节点,如果当前节点是独生节点,那么将其值加入答案数组中。函数 $\textit{dfs}$ 的执行过程如下: -时间复杂度 $O(n)$,其中 $n$ 为二叉树的节点个数。需要对二叉树进行一次遍历。 +1. 如果当前节点为空,或者当前节点是叶子节点,即当前节点的左右子节点都为空,那么直接返回。 +2. 如果当前节点的左子节点为空,那么将当前节点的右子节点是独生节点,将其值加入答案数组中。 +3. 如果当前节点的右子节点为空,那么将当前节点的左子节点是独生节点,将其值加入答案数组中。 +4. 递归遍历当前节点的左子节点和右子节点。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树中节点的个数。 @@ -93,8 +98,8 @@ tags: # self.right = right class Solution: def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]: - def dfs(root): - if root is None or (root.left is None and root.right is None): + def dfs(root: Optional[TreeNode]): + if root is None or root.left == root.right: return if root.left is None: ans.append(root.right.val) @@ -135,7 +140,7 @@ class Solution { } private void dfs(TreeNode root) { - if (root == null || (root.left == null && root.right == null)) { + if (root == null || (root.left == root.right)) { return; } if (root.left == null) { @@ -168,15 +173,20 @@ class Solution { public: vector getLonelyNodes(TreeNode* root) { vector ans; - function dfs; - dfs = [&](TreeNode* root) { - if (!root || (!root->left && !root->right)) return; - if (!root->left) ans.push_back(root->right->val); - if (!root->right) ans.push_back(root->left->val); - dfs(root->left); - dfs(root->right); + auto dfs = [&](auto&& dfs, TreeNode* root) { + if (!root || (root->left == root->right)) { + return; + } + if (!root->left) { + ans.push_back(root->right->val); + } + if (!root->right) { + ans.push_back(root->left->val); + } + dfs(dfs, root->left); + dfs(dfs, root->right); }; - dfs(root); + dfs(dfs, root); return ans; } }; @@ -193,11 +203,10 @@ public: * Right *TreeNode * } */ -func getLonelyNodes(root *TreeNode) []int { - ans := []int{} + func getLonelyNodes(root *TreeNode) (ans []int) { var dfs func(*TreeNode) dfs = func(root *TreeNode) { - if root == nil || (root.Left == nil && root.Right == nil) { + if root == nil || (root.Left == root.Right) { return } if root.Left == nil { @@ -210,7 +219,44 @@ func getLonelyNodes(root *TreeNode) []int { dfs(root.Right) } dfs(root) - return ans + return +} +``` + +#### TypeScript + +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function getLonelyNodes(root: TreeNode | null): number[] { + const ans: number[] = []; + const dfs = (root: TreeNode | null) => { + if (!root || root.left === root.right) { + return; + } + if (!root.left) { + ans.push(root.right.val); + } + if (!root.right) { + ans.push(root.left.val); + } + dfs(root.left); + dfs(root.right); + }; + dfs(root); + return ans; } ``` diff --git a/solution/1400-1499/1469.Find All The Lonely Nodes/README_EN.md b/solution/1400-1499/1469.Find All The Lonely Nodes/README_EN.md index d3f68549623b9..c89cf1c99af6c 100644 --- a/solution/1400-1499/1469.Find All The Lonely Nodes/README_EN.md +++ b/solution/1400-1499/1469.Find All The Lonely Nodes/README_EN.md @@ -67,7 +67,16 @@ All other nodes are lonely. -### Solution 1 +### Solution 1: DFS + +We can use Depth-First Search (DFS) to traverse the entire tree. We design a function $\textit{dfs}$, which traverses each node in the tree. If the current node is a lone child, we add its value to the answer array. The execution process of the function $\textit{dfs}$ is as follows: + +1. If the current node is null, or the current node is a leaf node (i.e., both the left and right children of the current node are null), then return directly. +2. If the left child of the current node is null, then the right child of the current node is a lone child, and we add its value to the answer array. +3. If the right child of the current node is null, then the left child of the current node is a lone child, and we add its value to the answer array. +4. Recursively traverse the left and right children of the current node. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree. @@ -82,8 +91,8 @@ All other nodes are lonely. # self.right = right class Solution: def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]: - def dfs(root): - if root is None or (root.left is None and root.right is None): + def dfs(root: Optional[TreeNode]): + if root is None or root.left == root.right: return if root.left is None: ans.append(root.right.val) @@ -124,7 +133,7 @@ class Solution { } private void dfs(TreeNode root) { - if (root == null || (root.left == null && root.right == null)) { + if (root == null || (root.left == root.right)) { return; } if (root.left == null) { @@ -157,15 +166,20 @@ class Solution { public: vector getLonelyNodes(TreeNode* root) { vector ans; - function dfs; - dfs = [&](TreeNode* root) { - if (!root || (!root->left && !root->right)) return; - if (!root->left) ans.push_back(root->right->val); - if (!root->right) ans.push_back(root->left->val); - dfs(root->left); - dfs(root->right); + auto dfs = [&](auto&& dfs, TreeNode* root) { + if (!root || (root->left == root->right)) { + return; + } + if (!root->left) { + ans.push_back(root->right->val); + } + if (!root->right) { + ans.push_back(root->left->val); + } + dfs(dfs, root->left); + dfs(dfs, root->right); }; - dfs(root); + dfs(dfs, root); return ans; } }; @@ -182,11 +196,10 @@ public: * Right *TreeNode * } */ -func getLonelyNodes(root *TreeNode) []int { - ans := []int{} + func getLonelyNodes(root *TreeNode) (ans []int) { var dfs func(*TreeNode) dfs = func(root *TreeNode) { - if root == nil || (root.Left == nil && root.Right == nil) { + if root == nil || (root.Left == root.Right) { return } if root.Left == nil { @@ -199,7 +212,44 @@ func getLonelyNodes(root *TreeNode) []int { dfs(root.Right) } dfs(root) - return ans + return +} +``` + +#### TypeScript + +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function getLonelyNodes(root: TreeNode | null): number[] { + const ans: number[] = []; + const dfs = (root: TreeNode | null) => { + if (!root || root.left === root.right) { + return; + } + if (!root.left) { + ans.push(root.right.val); + } + if (!root.right) { + ans.push(root.left.val); + } + dfs(root.left); + dfs(root.right); + }; + dfs(root); + return ans; } ``` diff --git a/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.cpp b/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.cpp index 41da38b6a1d84..bd7829428e46d 100644 --- a/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.cpp +++ b/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.cpp @@ -13,15 +13,20 @@ class Solution { public: vector getLonelyNodes(TreeNode* root) { vector ans; - function dfs; - dfs = [&](TreeNode* root) { - if (!root || (!root->left && !root->right)) return; - if (!root->left) ans.push_back(root->right->val); - if (!root->right) ans.push_back(root->left->val); - dfs(root->left); - dfs(root->right); + auto dfs = [&](auto&& dfs, TreeNode* root) { + if (!root || (root->left == root->right)) { + return; + } + if (!root->left) { + ans.push_back(root->right->val); + } + if (!root->right) { + ans.push_back(root->left->val); + } + dfs(dfs, root->left); + dfs(dfs, root->right); }; - dfs(root); + dfs(dfs, root); return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.go b/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.go index 85e3a36897934..5701339a95c72 100644 --- a/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.go +++ b/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.go @@ -6,11 +6,10 @@ * Right *TreeNode * } */ -func getLonelyNodes(root *TreeNode) []int { - ans := []int{} + func getLonelyNodes(root *TreeNode) (ans []int) { var dfs func(*TreeNode) dfs = func(root *TreeNode) { - if root == nil || (root.Left == nil && root.Right == nil) { + if root == nil || (root.Left == root.Right) { return } if root.Left == nil { @@ -23,5 +22,5 @@ func getLonelyNodes(root *TreeNode) []int { dfs(root.Right) } dfs(root) - return ans -} \ No newline at end of file + return +} diff --git a/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.java b/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.java index 3d68dac17f970..9eb2ad5625ad5 100644 --- a/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.java +++ b/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.java @@ -22,7 +22,7 @@ public List getLonelyNodes(TreeNode root) { } private void dfs(TreeNode root) { - if (root == null || (root.left == null && root.right == null)) { + if (root == null || (root.left == root.right)) { return; } if (root.left == null) { @@ -34,4 +34,4 @@ private void dfs(TreeNode root) { dfs(root.left); dfs(root.right); } -} \ No newline at end of file +} diff --git a/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.py b/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.py index 164512c14e0b9..b7fce7072eb0d 100644 --- a/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.py +++ b/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.py @@ -6,8 +6,8 @@ # self.right = right class Solution: def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]: - def dfs(root): - if root is None or (root.left is None and root.right is None): + def dfs(root: Optional[TreeNode]): + if root is None or root.left == root.right: return if root.left is None: ans.append(root.right.val) diff --git a/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.ts b/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.ts new file mode 100644 index 0000000000000..b9617fb9dcecf --- /dev/null +++ b/solution/1400-1499/1469.Find All The Lonely Nodes/Solution.ts @@ -0,0 +1,32 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function getLonelyNodes(root: TreeNode | null): number[] { + const ans: number[] = []; + const dfs = (root: TreeNode | null) => { + if (!root || root.left === root.right) { + return; + } + if (!root.left) { + ans.push(root.right.val); + } + if (!root.right) { + ans.push(root.left.val); + } + dfs(root.left); + dfs(root.right); + }; + dfs(root); + return ans; +} diff --git a/solution/1400-1499/1470.Shuffle the Array/README.md b/solution/1400-1499/1470.Shuffle the Array/README.md index 5eccc5d9fc7b5..376f0ef21ca39 100644 --- a/solution/1400-1499/1470.Shuffle the Array/README.md +++ b/solution/1400-1499/1470.Shuffle the Array/README.md @@ -27,7 +27,7 @@ tags:

示例 1:

输入:nums = [2,5,1,3,4,7], n = 3
-输出:[2,3,5,4,1,7] 
+输出:[2,3,5,4,1,7]
 解释:由于 x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 ,所以答案为 [2,3,5,4,1,7]
 
@@ -59,7 +59,13 @@ tags: -### 方法一 +### 方法一:模拟 + +我们在 $[0, n)$ 的范围内遍历下标 $i$,每次取出 $\textit{nums}[i]$ 和 $\textit{nums}[i+n]$,并将它们依次放入答案数组中。 + +遍历结束后,返回答案数组即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 @@ -68,11 +74,7 @@ tags: ```python class Solution: def shuffle(self, nums: List[int], n: int) -> List[int]: - ans = [] - for i in range(n): - ans.append(nums[i]) - ans.append(nums[i + n]) - return ans + return [x for pair in zip(nums[:n], nums[n:]) for x in pair] ``` #### Java @@ -109,13 +111,12 @@ public: #### Go ```go -func shuffle(nums []int, n int) []int { - var ans []int +func shuffle(nums []int, n int) (ans []int) { for i := 0; i < n; i++ { ans = append(ans, nums[i]) ans = append(ans, nums[i+n]) } - return ans + return } ``` @@ -123,9 +124,9 @@ func shuffle(nums []int, n int) []int { ```ts function shuffle(nums: number[], n: number): number[] { - let ans = []; - for (let i = 0; i < n; i++) { - ans.push(nums[i], nums[n + i]); + const ans: number[] = []; + for (let i = 0; i < n; ++i) { + ans.push(nums[i], nums[i + n]); } return ans; } @@ -137,12 +138,12 @@ function shuffle(nums: number[], n: number): number[] { impl Solution { pub fn shuffle(nums: Vec, n: i32) -> Vec { let n = n as usize; - let mut res = Vec::new(); + let mut ans = Vec::new(); for i in 0..n { - res.push(nums[i]); - res.push(nums[n + i]); + ans.push(nums[i]); + ans.push(nums[i + n]); } - res + ans } } ``` @@ -154,13 +155,13 @@ impl Solution { * Note: The returned array must be malloced, assume caller calls free(). */ int* shuffle(int* nums, int numsSize, int n, int* returnSize) { - int* res = (int*) malloc(sizeof(int) * n * 2); + int* ans = (int*) malloc(sizeof(int) * n * 2); for (int i = 0; i < n; i++) { - res[2 * i] = nums[i]; - res[2 * i + 1] = nums[i + n]; + ans[2 * i] = nums[i]; + ans[2 * i + 1] = nums[i + n]; } *returnSize = n * 2; - return res; + return ans; } ``` @@ -168,43 +169,6 @@ int* shuffle(int* nums, int numsSize, int n, int* returnSize) { - - -### 方法二 - - - -#### Python3 - -```python -class Solution: - def shuffle(self, nums: List[int], n: int) -> List[int]: - nums[::2], nums[1::2] = nums[:n], nums[n:] - return nums -``` - -#### Rust - -```rust -impl Solution { - pub fn shuffle(mut nums: Vec, n: i32) -> Vec { - let n = n as usize; - for i in 0..n * 2 { - let mut j = i; - while nums[i] > 0 { - j = if j < n { 2 * j } else { 2 * (j - n) + 1 }; - nums.swap(i, j); - nums[j] *= -1; - } - } - for i in 0..n * 2 { - nums[i] *= -1; - } - nums - } -} -``` - diff --git a/solution/1400-1499/1470.Shuffle the Array/README_EN.md b/solution/1400-1499/1470.Shuffle the Array/README_EN.md index 9f900c27ae752..efdc95874d1c1 100644 --- a/solution/1400-1499/1470.Shuffle the Array/README_EN.md +++ b/solution/1400-1499/1470.Shuffle the Array/README_EN.md @@ -30,7 +30,7 @@ tags: Input: nums = [2,5,1,3,4,7], n = 3 -Output: [2,3,5,4,1,7] +Output: [2,3,5,4,1,7] Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7]. @@ -76,7 +76,13 @@ tags: -### Solution 1 +### Solution 1: Simulation + +We traverse the indices $i$ in the range $[0, n)$. Each time, we take $\textit{nums}[i]$ and $\textit{nums}[i+n]$ and place them sequentially into the answer array. + +After the traversal is complete, we return the answer array. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -85,11 +91,7 @@ tags: ```python class Solution: def shuffle(self, nums: List[int], n: int) -> List[int]: - ans = [] - for i in range(n): - ans.append(nums[i]) - ans.append(nums[i + n]) - return ans + return [x for pair in zip(nums[:n], nums[n:]) for x in pair] ``` #### Java @@ -126,13 +128,12 @@ public: #### Go ```go -func shuffle(nums []int, n int) []int { - var ans []int +func shuffle(nums []int, n int) (ans []int) { for i := 0; i < n; i++ { ans = append(ans, nums[i]) ans = append(ans, nums[i+n]) } - return ans + return } ``` @@ -140,9 +141,9 @@ func shuffle(nums []int, n int) []int { ```ts function shuffle(nums: number[], n: number): number[] { - let ans = []; - for (let i = 0; i < n; i++) { - ans.push(nums[i], nums[n + i]); + const ans: number[] = []; + for (let i = 0; i < n; ++i) { + ans.push(nums[i], nums[i + n]); } return ans; } @@ -154,12 +155,12 @@ function shuffle(nums: number[], n: number): number[] { impl Solution { pub fn shuffle(nums: Vec, n: i32) -> Vec { let n = n as usize; - let mut res = Vec::new(); + let mut ans = Vec::new(); for i in 0..n { - res.push(nums[i]); - res.push(nums[n + i]); + ans.push(nums[i]); + ans.push(nums[i + n]); } - res + ans } } ``` @@ -171,54 +172,13 @@ impl Solution { * Note: The returned array must be malloced, assume caller calls free(). */ int* shuffle(int* nums, int numsSize, int n, int* returnSize) { - int* res = (int*) malloc(sizeof(int) * n * 2); + int* ans = (int*) malloc(sizeof(int) * n * 2); for (int i = 0; i < n; i++) { - res[2 * i] = nums[i]; - res[2 * i + 1] = nums[i + n]; + ans[2 * i] = nums[i]; + ans[2 * i + 1] = nums[i + n]; } *returnSize = n * 2; - return res; -} -``` - - - - - - - -### Solution 2 - - - -#### Python3 - -```python -class Solution: - def shuffle(self, nums: List[int], n: int) -> List[int]: - nums[::2], nums[1::2] = nums[:n], nums[n:] - return nums -``` - -#### Rust - -```rust -impl Solution { - pub fn shuffle(mut nums: Vec, n: i32) -> Vec { - let n = n as usize; - for i in 0..n * 2 { - let mut j = i; - while nums[i] > 0 { - j = if j < n { 2 * j } else { 2 * (j - n) + 1 }; - nums.swap(i, j); - nums[j] *= -1; - } - } - for i in 0..n * 2 { - nums[i] *= -1; - } - nums - } + return ans; } ``` diff --git a/solution/1400-1499/1470.Shuffle the Array/Solution.c b/solution/1400-1499/1470.Shuffle the Array/Solution.c index 348c1b1be4cc0..2479a223f20b5 100644 --- a/solution/1400-1499/1470.Shuffle the Array/Solution.c +++ b/solution/1400-1499/1470.Shuffle the Array/Solution.c @@ -2,11 +2,11 @@ * Note: The returned array must be malloced, assume caller calls free(). */ int* shuffle(int* nums, int numsSize, int n, int* returnSize) { - int* res = (int*) malloc(sizeof(int) * n * 2); + int* ans = (int*) malloc(sizeof(int) * n * 2); for (int i = 0; i < n; i++) { - res[2 * i] = nums[i]; - res[2 * i + 1] = nums[i + n]; + ans[2 * i] = nums[i]; + ans[2 * i + 1] = nums[i + n]; } *returnSize = n * 2; - return res; -} \ No newline at end of file + return ans; +} diff --git a/solution/1400-1499/1470.Shuffle the Array/Solution.go b/solution/1400-1499/1470.Shuffle the Array/Solution.go index 21b37afaddd26..501eb371a6fd2 100644 --- a/solution/1400-1499/1470.Shuffle the Array/Solution.go +++ b/solution/1400-1499/1470.Shuffle the Array/Solution.go @@ -1,8 +1,7 @@ -func shuffle(nums []int, n int) []int { - var ans []int +func shuffle(nums []int, n int) (ans []int) { for i := 0; i < n; i++ { ans = append(ans, nums[i]) ans = append(ans, nums[i+n]) } - return ans -} \ No newline at end of file + return +} diff --git a/solution/1400-1499/1470.Shuffle the Array/Solution.py b/solution/1400-1499/1470.Shuffle the Array/Solution.py index 945a57ee32fbd..5b07afa87cae4 100644 --- a/solution/1400-1499/1470.Shuffle the Array/Solution.py +++ b/solution/1400-1499/1470.Shuffle the Array/Solution.py @@ -1,7 +1,3 @@ class Solution: def shuffle(self, nums: List[int], n: int) -> List[int]: - ans = [] - for i in range(n): - ans.append(nums[i]) - ans.append(nums[i + n]) - return ans + return [x for pair in zip(nums[:n], nums[n:]) for x in pair] diff --git a/solution/1400-1499/1470.Shuffle the Array/Solution.rs b/solution/1400-1499/1470.Shuffle the Array/Solution.rs index 0938364be7187..7d3d2712a3e7e 100644 --- a/solution/1400-1499/1470.Shuffle the Array/Solution.rs +++ b/solution/1400-1499/1470.Shuffle the Array/Solution.rs @@ -1,11 +1,11 @@ impl Solution { pub fn shuffle(nums: Vec, n: i32) -> Vec { let n = n as usize; - let mut res = Vec::new(); + let mut ans = Vec::new(); for i in 0..n { - res.push(nums[i]); - res.push(nums[n + i]); + ans.push(nums[i]); + ans.push(nums[i + n]); } - res + ans } } diff --git a/solution/1400-1499/1470.Shuffle the Array/Solution.ts b/solution/1400-1499/1470.Shuffle the Array/Solution.ts index 5b062512a6a5a..14522a5e56cd6 100644 --- a/solution/1400-1499/1470.Shuffle the Array/Solution.ts +++ b/solution/1400-1499/1470.Shuffle the Array/Solution.ts @@ -1,7 +1,7 @@ function shuffle(nums: number[], n: number): number[] { - let ans = []; - for (let i = 0; i < n; i++) { - ans.push(nums[i], nums[n + i]); + const ans: number[] = []; + for (let i = 0; i < n; ++i) { + ans.push(nums[i], nums[i + n]); } return ans; } diff --git a/solution/1400-1499/1470.Shuffle the Array/Solution2.py b/solution/1400-1499/1470.Shuffle the Array/Solution2.py deleted file mode 100644 index ae53df289da3e..0000000000000 --- a/solution/1400-1499/1470.Shuffle the Array/Solution2.py +++ /dev/null @@ -1,4 +0,0 @@ -class Solution: - def shuffle(self, nums: List[int], n: int) -> List[int]: - nums[::2], nums[1::2] = nums[:n], nums[n:] - return nums diff --git a/solution/1400-1499/1470.Shuffle the Array/Solution2.rs b/solution/1400-1499/1470.Shuffle the Array/Solution2.rs deleted file mode 100644 index 7a07624809757..0000000000000 --- a/solution/1400-1499/1470.Shuffle the Array/Solution2.rs +++ /dev/null @@ -1,17 +0,0 @@ -impl Solution { - pub fn shuffle(mut nums: Vec, n: i32) -> Vec { - let n = n as usize; - for i in 0..n * 2 { - let mut j = i; - while nums[i] > 0 { - j = if j < n { 2 * j } else { 2 * (j - n) + 1 }; - nums.swap(i, j); - nums[j] *= -1; - } - } - for i in 0..n * 2 { - nums[i] *= -1; - } - nums - } -}