Skip to content

Commit

Permalink
add: 对数组执行操作
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jun 5, 2023
1 parent b806bc8 commit eb3a90f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 数组/队列/集合/映射

- [对数组执行操作](src/array/apply_operations_to_an_array.rs) [数组, 模拟]

- LeetCode 2460. 对数组执行操作 <https://leetcode.cn/problems/apply-operations-to-an-array>

- [统计范围内的元音字符串数](src/array/count_vowel_strings_in_ranges.rs) [数组, 字符串, 前缀和]

- LeetCode 2559. 统计范围内的元音字符串数 <https://leetcode.cn/problems/count-vowel-strings-in-ranges>
Expand Down
Binary file added images/array/apply_operations_to_an_array.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions src/array/apply_operations_to_an_array.rs
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
}
}
1 change: 1 addition & 0 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ pub mod teemo_attacking;
pub mod the_employee_that_worked_on_the_longest_task;
pub mod two_sum;
pub mod zero_matrix_lcci;
pub mod apply_operations_to_an_array;
26 changes: 26 additions & 0 deletions tests/array/apply_operations_to_an_array_test.rs
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]);
}
1 change: 1 addition & 0 deletions tests/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ pub mod statistics_from_a_large_sample_test;
pub mod find_the_kth_smallest_sum_of_a_matrix_with_sorted_rows_test;
pub mod average_value_of_even_numbers_that_are_divisible_by_three_test;
pub mod count_vowel_strings_in_ranges_test;
pub mod apply_operations_to_an_array_test;
8 changes: 6 additions & 2 deletions tests/sort/number_of_distinct_averages_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ fn distinct_averages() {
// 2. 删除 1 和 4 ,平均值是 (1 + 4) / 2 = 2.5 ,现在 nums = [4,3] 。
// 3. 删除 3 和 4 ,平均值是 (3 + 4) / 2 = 3.5 。
// 2.5 ,2.5 和 3.5 之中总共有 2 个不同的数,我们返回 2 。
let nums = vec![4, 1, 4, 0, 3, 5];
let result = Solution::distinct_averages(nums);
assert_eq!(result, 2);

// 示例 2:
// 输入:nums = [1,100]
// 输出:1
// 解释:
// 删除 1 和 100 后只有一个平均值,所以我们返回 1 。

assert_eq!(Solution::distinct_averages(), 1);
let nums = vec![1, 100];
let result = Solution::distinct_averages(nums);
assert_eq!(result, 1);
}

0 comments on commit eb3a90f

Please sign in to comment.