Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions solution/0400-0499/0417.Pacific Atlantic Water Flow/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ tags:
<strong>Input:</strong> heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
<strong>Output:</strong> [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
<strong>Explanation:</strong> The following cells can flow to the Pacific and Atlantic oceans, as shown below:
[0,4]: [0,4] -&gt; Pacific Ocean
[0,4]: [0,4] -&gt; Pacific Ocean
&nbsp; [0,4] -&gt; Atlantic Ocean
[1,3]: [1,3] -&gt; [0,3] -&gt; Pacific Ocean
[1,3]: [1,3] -&gt; [0,3] -&gt; Pacific Ocean
&nbsp; [1,3] -&gt; [1,4] -&gt; Atlantic Ocean
[1,4]: [1,4] -&gt; [1,3] -&gt; [0,3] -&gt; Pacific Ocean
[1,4]: [1,4] -&gt; [1,3] -&gt; [0,3] -&gt; Pacific Ocean
&nbsp; [1,4] -&gt; Atlantic Ocean
[2,2]: [2,2] -&gt; [1,2] -&gt; [0,2] -&gt; Pacific Ocean
[2,2]: [2,2] -&gt; [1,2] -&gt; [0,2] -&gt; Pacific Ocean
&nbsp; [2,2] -&gt; [2,3] -&gt; [2,4] -&gt; Atlantic Ocean
[3,0]: [3,0] -&gt; Pacific Ocean
[3,0]: [3,0] -&gt; Pacific Ocean
&nbsp; [3,0] -&gt; [4,0] -&gt; Atlantic Ocean
[3,1]: [3,1] -&gt; [3,0] -&gt; Pacific Ocean
[3,1]: [3,1] -&gt; [3,0] -&gt; Pacific Ocean
&nbsp; [3,1] -&gt; [4,1] -&gt; Atlantic Ocean
[4,0]: [4,0] -&gt; Pacific Ocean
[4,0]: [4,0] -&gt; Pacific Ocean
[4,0] -&gt; Atlantic Ocean
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3692.Majority%20Frequency%20Characters/README.md
rating: 1384
source: 第 166 场双周赛 Q1
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3692.Majority%20Frequency%20Characters/README_EN.md
rating: 1384
source: Biweekly Contest 166 Q1
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3600-3699/3693.Climbing Stairs II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3693.Climbing%20Stairs%20II/README.md
rating: 1560
source: 第 166 场双周赛 Q2
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3600-3699/3693.Climbing Stairs II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3693.Climbing%20Stairs%20II/README_EN.md
rating: 1560
source: Biweekly Contest 166 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3694.Distinct%20Points%20Reachable%20After%20Substring%20Removal/README.md
rating: 1739
source: 第 166 场双周赛 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3694.Distinct%20Points%20Reachable%20After%20Substring%20Removal/README_EN.md
rating: 1739
source: Biweekly Contest 166 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3695.Maximize%20Alternating%20Sum%20Using%20Swaps/README.md
rating: 1984
source: 第 166 场双周赛 Q4
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3695.Maximize%20Alternating%20Sum%20Using%20Swaps/README_EN.md
rating: 1984
source: Biweekly Contest 166 Q4
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3697.Compute%20Decimal%20Representation/README.md
rating: 1250
source: 第 469 场周赛 Q1
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3697.Compute%20Decimal%20Representation/README_EN.md
rating: 1250
source: Weekly Contest 469 Q1
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3698.Split%20Array%20With%20Minimum%20Difference/README.md
rating: 1648
source: 第 469 场周赛 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3698.Split%20Array%20With%20Minimum%20Difference/README_EN.md
rating: 1648
source: Weekly Contest 469 Q2
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3600-3699/3699.Number of ZigZag Arrays I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3699.Number%20of%20ZigZag%20Arrays%20I/README.md
rating: 2123
source: 第 469 场周赛 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3699.Number%20of%20ZigZag%20Arrays%20I/README_EN.md
rating: 2123
source: Weekly Contest 469 Q3
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3700-3799/3700.Number of ZigZag Arrays II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3700.Number%20of%20ZigZag%20Arrays%20II/README.md
rating: 2435
source: 第 469 场周赛 Q4
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3700.Number%20of%20ZigZag%20Arrays%20II/README_EN.md
rating: 2435
source: Weekly Contest 469 Q4
---

<!-- problem:start -->
Expand Down
150 changes: 150 additions & 0 deletions solution/3700-3799/3701.Compute Alternating Sum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3701.Compute%20Alternating%20Sum/README.md
---

<!-- problem:start -->

# [3701. 计算交替和](https://leetcode.cn/problems/compute-alternating-sum)

[English Version](/solution/3700-3799/3701.Compute%20Alternating%20Sum/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给你一个整数数组 <code>nums</code>。</p>

<p><strong>交替和&nbsp;</strong>定义为:将 <code>nums</code> 中偶数下标位置的元素&nbsp;<strong>相加&nbsp;</strong>,<strong>减去</strong> 奇数下标位置的元素。即:<code>nums[0] - nums[1] + nums[2] - nums[3]...</code></p>

<p>返回表示 <code>nums</code> 的交替和的整数。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">nums = [1,3,5,7]</span></p>

<p><strong>输出:</strong> <span class="example-io">-4</span></p>

<p><strong>解释:</strong></p>

<ul>
<li>偶数下标位置的元素是 <code>nums[0] = 1</code> 和 <code>nums[2] = 5</code>,因为 0 和 2 是偶数。</li>
<li>奇数下标位置的元素是 <code>nums[1] = 3</code> 和 <code>nums[3] = 7</code>,因为 1 和 3 是奇数。</li>
<li>交替和为 <code>nums[0] - nums[1] + nums[2] - nums[3] = 1 - 3 + 5 - 7 = -4</code>。</li>
</ul>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">nums = [100]</span></p>

<p><strong>输出:</strong> <span class="example-io">100</span></p>

<p><strong>解释:</strong></p>

<ul>
<li>唯一的偶数下标位置的元素是 <code>nums[0] = 100</code>,因为 0 是偶数。</li>
<li>没有奇数下标位置的元素。</li>
<li>交替和为 <code>nums[0] = 100</code>。</li>
</ul>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:模拟

我们可以直接遍历数组 $\textit{nums}$,对于每个下标 $i$,如果 $i$ 是偶数,则将 $\textit{nums}[i]$ 加到答案中,否则将 $\textit{nums}[i]$ 减去。

最后返回答案即可。

时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def alternatingSum(self, nums: List[int]) -> int:
return sum(nums[0::2]) - sum(nums[1::2])
```

#### Java

```java
class Solution {
public int alternatingSum(int[] nums) {
int ans = 0;
for (int i = 0; i < nums.length; ++i) {
ans += (i % 2 == 0 ? nums[i] : -nums[i]);
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int alternatingSum(vector<int>& nums) {
int ans = 0;
for (int i = 0; i < nums.size(); ++i) {
ans += (i % 2 == 0 ? nums[i] : -nums[i]);
}
return ans;
}
};
```

#### Go

```go
func alternatingSum(nums []int) (ans int) {
for i, x := range nums {
if i%2 == 0 {
ans += x
} else {
ans -= x
}
}
return
}
```

#### TypeScript

```ts
function alternatingSum(nums: number[]): number {
let ans: number = 0;
for (let i = 0; i < nums.length; ++i) {
ans += i % 2 === 0 ? nums[i] : -nums[i];
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading