From 8a0ee72b15bb9f5a4a64e2426bb312fd04d7a979 Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Mon, 29 Jul 2024 10:16:47 +0800 Subject: [PATCH] leetcode: Add index to 2574 --- src/SUMMARY.md | 2 ++ .../Cargo.toml | 2 +- .../index.md | 19 +++++++++++++++++-- .../src/main.rs | 8 +++++--- src/leetcode/by-id/2501-2600.md | 1 + src/leetcode/tags/prefix-sum.md | 5 +++-- 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index e7a77924f..cc8680bbd 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -300,6 +300,8 @@ - [2101-2200](leetcode/by-id/2101-2200.md) - [2108. 找出数组中的第一个回文字符串 Find First Palindromic String in the Array](leetcode/2108.find-first-palindromic-string-in-the-array/index.md) - [2119. 反转两次的数字 A Number After a Double Reversal](leetcode/2119.a-number-after-a-double-reversal/index.md) + - [2501-2600](leetcode/by-id/2501-2600.md) + - [2574. 左右元素和的差值 Left and Right Sum Differences](leetcode/2574.left-and-right-sum-differences/index.md) - [2801-2900](leetcode/by-id/2801-2900.md) - [2848. 与车相交的点 Points That Intersect With Cars](leetcode/2848.points-that-intersect-with-cars/index.md) - [3001-3100](leetcode/by-id/3001-3100.md) diff --git a/src/leetcode/2574.left-and-right-sum-differences/Cargo.toml b/src/leetcode/2574.left-and-right-sum-differences/Cargo.toml index 06d239521..a4c1d9d87 100644 --- a/src/leetcode/2574.left-and-right-sum-differences/Cargo.toml +++ b/src/leetcode/2574.left-and-right-sum-differences/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "lc-0000-name" +name = "lc-2574-left-and-right-sum-differences" version = "0.1.0" edition = "2021" publish = false diff --git a/src/leetcode/2574.left-and-right-sum-differences/index.md b/src/leetcode/2574.left-and-right-sum-differences/index.md index 845f89887..6da895d1b 100644 --- a/src/leetcode/2574.left-and-right-sum-differences/index.md +++ b/src/leetcode/2574.left-and-right-sum-differences/index.md @@ -1,4 +1,19 @@ +# 2574. 左右元素和的差值 Left and Right Sum Differences -# +[问题描述](https://leetcode.com/problems/left-and-right-sum-differences) -[问题描述](https://leetcode.com/problems/) +这是一个简单的前缀和 prefix sum 问题. + +## Prefix sum + +处理思路如下: + +1. 计算 `left_sum`, 从左到右遍历原数组, 并计算前缀和, `left_sum[i + 1] = left_sum[i] + nums[i]` +2. 计算 `right_sum` 从左到右遍历原数组, 并计算前缀和, `right_sum[i - 1] = right_sum[i] + nums[i]` +3. 计算遍历两个数组, 并计算 `(left_sum[i] - right_sum[i]).abs()`, 就得到了答案 + +```rust +{{#include src/main.rs:5:31}} +``` + +该算法的时间复杂度是 `O(n)`, 空间复杂度是 `O(n)`. \ No newline at end of file diff --git a/src/leetcode/2574.left-and-right-sum-differences/src/main.rs b/src/leetcode/2574.left-and-right-sum-differences/src/main.rs index 759ea471d..81b3c3532 100644 --- a/src/leetcode/2574.left-and-right-sum-differences/src/main.rs +++ b/src/leetcode/2574.left-and-right-sum-differences/src/main.rs @@ -9,6 +9,7 @@ pub fn left_right_difference1(nums: Vec) -> Vec { let len = nums.len(); let mut left_sum = vec![0; len]; left_sum[0] = 0; + // 从左向右遍历 for i in 0..(len - 1) { left_sum[i + 1] = left_sum[i] + nums[i]; } @@ -16,14 +17,15 @@ pub fn left_right_difference1(nums: Vec) -> Vec { let mut right_sum = vec![0; len]; right_sum[0] = 0; - for i in 0..(len - 1) { - right_sum[i + 1] = right_sum[i] + nums[len - i - 1]; + // 从右向左遍历 + for i in (1..=(len - 1)).rev() { + right_sum[i - 1] = right_sum[i] + nums[i]; } //println!("right sum: {right_sum:?}"); left_sum .into_iter() - .zip(right_sum.into_iter().rev()) + .zip(right_sum) .map(|(left, right)| (left - right).abs()) .collect() } diff --git a/src/leetcode/by-id/2501-2600.md b/src/leetcode/by-id/2501-2600.md index e69de29bb..22db17d1c 100644 --- a/src/leetcode/by-id/2501-2600.md +++ b/src/leetcode/by-id/2501-2600.md @@ -0,0 +1 @@ +# 2501-2600 \ No newline at end of file diff --git a/src/leetcode/tags/prefix-sum.md b/src/leetcode/tags/prefix-sum.md index ff9dfa52e..3f8238867 100644 --- a/src/leetcode/tags/prefix-sum.md +++ b/src/leetcode/tags/prefix-sum.md @@ -8,8 +8,9 @@ 4. [1480. 一维数组的动态和 Running Sum of 1d Array](../1480.running-sum-of-1d-array/index.md) 5. [1732. 找到最高海拔 Find the Highest Altitude](../1732.find-the-highest-altitude/index.md) 6. [1991. 找到数组的中间位置 Find the Middle Index in Array](../1991.find-the-middle-index-in-array/index.md) -7. [2848. 与车相交的点 Points That Intersect With Cars](../2848.points-that-intersect-with-cars/index.md) -8. [3028. 边界上的蚂蚁 Ant on the Boundary](../3028.ant-on-the-boundary/index.md) +7. [2574. 左右元素和的差值 Left and Right Sum Differences](../2574.left-and-right-sum-differences/index.md) +8. [2848. 与车相交的点 Points That Intersect With Cars](../2848.points-that-intersect-with-cars/index.md) +9. [3028. 边界上的蚂蚁 Ant on the Boundary](../3028.ant-on-the-boundary/index.md) ## 中等