-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 对数组执行操作 | ||
// https://leetcode.cn/problems/apply-operations-to-an-array | ||
// INLINE ../../images/array/apply_operations_to_an_array.jpeg | ||
|
||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn apply_operations(mut nums: Vec<i32>) -> Vec<i32> { | ||
let n = nums.len(); | ||
let mut j = 0; // j 是下一个非零元素的索引 | ||
for i in 0..n { | ||
// i 是当前元素的索引 | ||
// 如果当前元素不是最后一个元素,且它等于下一个元素 | ||
if i + 1 < n && nums[i] == nums[i + 1] { | ||
// 将当前元素翻倍 | ||
nums[i] *= 2; | ||
// 将下一个元素设置为 0 | ||
nums[i + 1] = 0; | ||
} | ||
// 如果当前元素不是零 | ||
if nums[i] != 0 { | ||
// 将它与索引 j 的元素交换(下一个非零元素) | ||
nums.swap(i, j); | ||
// 增加 j | ||
j += 1; | ||
} | ||
} | ||
nums | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use rust_practice::array::apply_operations_to_an_array::Solution; | ||
|
||
#[test] | ||
fn apply_operations() { | ||
// 示例 1: | ||
// 输入:nums = [1,2,2,1,1,0] | ||
// 输出:[1,4,2,0,0,0] | ||
// 解释:执行以下操作: | ||
// - i = 0: nums[0] 和 nums[1] 不相等,跳过这步操作。 | ||
// - i = 1: nums[1] 和 nums[2] 相等,nums[1] 的值变成原来的 2 倍,nums[2] 的值变成 0 。数组变成 [1,4,0,1,1,0] 。 | ||
// - i = 2: nums[2] 和 nums[3] 不相等,所以跳过这步操作。 | ||
// - i = 3: nums[3] 和 nums[4] 相等,nums[3] 的值变成原来的 2 倍,nums[4] 的值变成 0 。数组变成 [1,4,0,2,0,0] 。 | ||
// - i = 4: nums[4] 和 nums[5] 相等,nums[4] 的值变成原来的 2 倍,nums[5] 的值变成 0 。数组变成 [1,4,0,2,0,0] 。 | ||
// 执行完所有操作后,将 0 全部移动到数组末尾,得到结果数组 [1,4,2,0,0,0] 。 | ||
let nums = vec![1, 2, 2, 1, 1, 0]; | ||
let result = Solution::apply_operations(nums); | ||
assert_eq!(result, vec![1, 4, 2, 0, 0, 0]); | ||
|
||
// 示例 2: | ||
// 输入:nums = [0,1] | ||
// 输出:[1,0] | ||
// 解释:无法执行任何操作,只需要将 0 移动到末尾。 | ||
let nums = vec![0, 1]; | ||
let result = Solution::apply_operations(nums); | ||
assert_eq!(result, vec![1, 0]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters