Skip to content

Commit 24d62c1

Browse files
committed
update: 添加问题“3423.循环数组中相邻元素的最大差值”的代码和题解(#982)
Signed-off-by: LetMeFly666 <[email protected]>
1 parent fb4033b commit 24d62c1

8 files changed

+243
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-12 22:49:40
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-12 22:52:31
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int maxAdjacentDistance(vector<int>& nums) {
14+
int ans = abs(nums[0] - nums[nums.size() - 1]);
15+
for (int i = 1; i < nums.size(); i++) {
16+
ans = max(ans, abs(nums[i] - nums[i - 1]));
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-12 22:49:40
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-12 22:58:57
6+
* @Descriptions: AC,100.00%,93.33%
7+
*/
8+
package main
9+
10+
// abs3423 是因为golang没有内置abs函数
11+
func abs3423(x int) int {
12+
if x < 0 {
13+
return -x
14+
}
15+
return x
16+
}
17+
18+
func maxAdjacentDistance(nums []int) int {
19+
ans := abs3423(nums[0] - nums[len(nums)-1])
20+
for i := 1; i < len(nums); i++ {
21+
ans = max(ans, abs3423(nums[i] - nums[i - 1]))
22+
}
23+
return ans
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-12 22:49:40
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-12 23:01:46
6+
*/
7+
class Solution {
8+
public int maxAdjacentDistance(int[] nums) {
9+
int ans = Math.abs(nums[0] - nums[nums.length - 1]);
10+
for (int i = 1; i < nums.length; i++) {
11+
ans = Math.max(ans, Math.abs(nums[i] - nums[i - 1]));
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-06-12 22:49:40
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-06-12 22:53:39
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def maxAdjacentDistance(self, nums: List[int]) -> int:
11+
ans = abs(nums[0] - nums[-1])
12+
for i in range(1, len(nums)):
13+
ans = max(ans, abs(nums[i] - nums[i - 1]))
14+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@
986986
|3375.使数组的值全部为K的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/09/LeetCode%203375.%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%80%BC%E5%85%A8%E9%83%A8%E4%B8%BAK%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147104288" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/solutions/3646138/letmefly-3375shi-shu-zu-de-zhi-quan-bu-w-i71e/" target="_blank">LeetCode题解</a>|
987987
|3392.统计符合条件长度为3的子数组数目|简单|<a href="https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/29/LeetCode%203392.%E7%BB%9F%E8%AE%A1%E7%AC%A6%E5%90%88%E6%9D%A1%E4%BB%B6%E9%95%BF%E5%BA%A6%E4%B8%BA3%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147603015" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/solutions/3665081/letmefly-3392tong-ji-fu-he-tiao-jian-cha-h935/" target="_blank">LeetCode题解</a>|
988988
|3396.使数组元素互不相同所需的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/08/LeetCode%203396.%E4%BD%BF%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E4%BA%92%E4%B8%8D%E7%9B%B8%E5%90%8C%E6%89%80%E9%9C%80%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147080178" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/solutions/3644951/letmefly-3396shi-shu-zu-yuan-su-hu-bu-xi-glg4/" target="_blank">LeetCode题解</a>|
989+
|3423.循环数组中相邻元素的最大差值|简单|<a href="https://leetcode.cn/problems/maximum-difference-between-adjacent-elements-in-a-circular-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/12/LeetCode%203423.%E5%BE%AA%E7%8E%AF%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9B%B8%E9%82%BB%E5%85%83%E7%B4%A0%E7%9A%84%E6%9C%80%E5%A4%A7%E5%B7%AE%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148620597" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-difference-between-adjacent-elements-in-a-circular-array/solutions/3699053/letmefly-3423xun-huan-shu-zu-zhong-xiang-cnde/" target="_blank">LeetCode题解</a>|
989990
|3442.奇偶频次间的最大差值I|简单|<a href="https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/10/LeetCode%203442.%E5%A5%87%E5%81%B6%E9%A2%91%E6%AC%A1%E9%97%B4%E7%9A%84%E6%9C%80%E5%A4%A7%E5%B7%AE%E5%80%BCI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148570777" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/solutions/3697605/letmefly-3442qi-ou-pin-ci-jian-de-zui-da-nwwx/" target="_blank">LeetCode题解</a>|
990991
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
991992
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: 3423.循环数组中相邻元素的最大差值:遍历(模拟)
3+
date: 2025-06-12 23:03:23
4+
tags: [题解, LeetCode, 简单, 数组, 遍历, 模拟]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3423.循环数组中相邻元素的最大差值:遍历(模拟)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/maximum-difference-between-adjacent-elements-in-a-circular-array/](https://leetcode.cn/problems/maximum-difference-between-adjacent-elements-in-a-circular-array/)
11+
12+
<p>给你一个 <strong>循环</strong>&nbsp;数组&nbsp;<code>nums</code>&nbsp;,请你找出相邻元素之间的&nbsp;<strong>最大</strong>&nbsp;绝对差值。</p>
13+
14+
<p><b>注意:</b>一个循环数组中,第一个元素和最后一个元素是相邻的。</p>
15+
16+
<p>&nbsp;</p>
17+
18+
<p><strong class="example">示例 1:</strong></p>
19+
20+
<div class="example-block">
21+
<p><span class="example-io"><b>输入:</b>nums = [1,2,4]</span></p>
22+
23+
<p><span class="example-io"><b>输出:</b>3</span></p>
24+
25+
<p><strong>解释:</strong></p>
26+
27+
<p>由于&nbsp;<code>nums</code>&nbsp;是循环的,<code>nums[0]</code> 和&nbsp;<code>nums[2]</code>&nbsp;是相邻的,它们之间的绝对差值是最大值&nbsp;<code>|4 - 1| = 3</code>&nbsp;。</p>
28+
</div>
29+
30+
<p><strong class="example">示例 2:</strong></p>
31+
32+
<div class="example-block">
33+
<p><span class="example-io"><b>输入:</b>nums = [-5,-10,-5]</span></p>
34+
35+
<p><span class="example-io"><b>输出:</b>5</span></p>
36+
37+
<p><b>解释:</b></p>
38+
39+
<p>相邻元素&nbsp;<code>nums[0]</code> 和&nbsp;<code>nums[1]</code>&nbsp;之间的绝对差值为最大值&nbsp;<code>|-5 - (-10)| = 5</code>&nbsp;。</p>
40+
</div>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><strong>提示:</strong></p>
45+
46+
<ul>
47+
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
48+
<li><code>-100 &lt;= nums[i] &lt;= 100</code></li>
49+
</ul>
50+
51+
52+
53+
## 解题方法:遍历
54+
55+
答案(ans)的默认值是$abs(nums[0] - nums[-1])$,接着使用$i$从$1$到$len(nums) - 1$遍历,更新ans为$max(ans, abs(nums[i] - nums[i - 1]))$
56+
57+
+ 时间复杂度$O(len(nums))$
58+
+ 空间复杂度$O(1)$
59+
60+
### AC代码
61+
62+
#### C++
63+
64+
```cpp
65+
/*
66+
* @Author: LetMeFly
67+
* @Date: 2025-06-12 22:49:40
68+
* @LastEditors: LetMeFly.xyz
69+
* @LastEditTime: 2025-06-12 22:52:31
70+
*/
71+
class Solution {
72+
public:
73+
int maxAdjacentDistance(vector<int>& nums) {
74+
int ans = abs(nums[0] - nums[nums.size() - 1]);
75+
for (int i = 1; i < nums.size(); i++) {
76+
ans = max(ans, abs(nums[i] - nums[i - 1]));
77+
}
78+
return ans;
79+
}
80+
};
81+
```
82+
83+
#### Python
84+
85+
```python
86+
'''
87+
Author: LetMeFly
88+
Date: 2025-06-12 22:49:40
89+
LastEditors: LetMeFly.xyz
90+
LastEditTime: 2025-06-12 22:53:39
91+
'''
92+
from typing import List
93+
94+
class Solution:
95+
def maxAdjacentDistance(self, nums: List[int]) -> int:
96+
ans = abs(nums[0] - nums[-1])
97+
for i in range(1, len(nums)):
98+
ans = max(ans, abs(nums[i] - nums[i - 1]))
99+
return ans
100+
```
101+
102+
#### Java
103+
104+
```java
105+
/*
106+
* @Author: LetMeFly
107+
* @Date: 2025-06-12 22:49:40
108+
* @LastEditors: LetMeFly.xyz
109+
* @LastEditTime: 2025-06-12 23:01:46
110+
*/
111+
class Solution {
112+
public int maxAdjacentDistance(int[] nums) {
113+
int ans = Math.abs(nums[0] - nums[nums.length - 1]);
114+
for (int i = 1; i < nums.length; i++) {
115+
ans = Math.max(ans, Math.abs(nums[i] - nums[i - 1]));
116+
}
117+
return ans;
118+
}
119+
}
120+
```
121+
122+
#### Go
123+
124+
```go
125+
/*
126+
* @Author: LetMeFly
127+
* @Date: 2025-06-12 22:49:40
128+
* @LastEditors: LetMeFly.xyz
129+
* @LastEditTime: 2025-06-12 22:58:57
130+
* @Descriptions: AC,100.00%,93.33%
131+
*/
132+
package main
133+
134+
// abs3423 是因为golang没有内置abs函数
135+
func abs3423(x int) int {
136+
if x < 0 {
137+
return -x
138+
}
139+
return x
140+
}
141+
142+
func maxAdjacentDistance(nums []int) int {
143+
ans := abs3423(nums[0] - nums[len(nums)-1])
144+
for i := 1; i < len(nums); i++ {
145+
ans = max(ans, abs3423(nums[i] - nums[i - 1]))
146+
}
147+
return ans
148+
}
149+
```
150+
151+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/148620597)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/06/12/LeetCode%203423.%E5%BE%AA%E7%8E%AF%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9B%B8%E9%82%BB%E5%85%83%E7%B4%A0%E7%9A%84%E6%9C%80%E5%A4%A7%E5%B7%AE%E5%80%BC/)哦~
152+
>
153+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ categories: [自用]
853853
|<font color="#28bea0" title="二次复习">reverence</font>|n. 尊敬,尊重,尊崇<br/>v. 尊敬,尊重,尊崇|
854854
|||
855855
|hitchhike|v. 搭便车(旅行)|
856-
|memoir|n. 自传,(尤指名人的)回忆录,传说,地方志|
856+
|<font color="#28bea0" title="二次复习">memoir</font>|n. 自传,(尤指名人的)回忆录,传说,地方志|
857857
|||
858858
|clown|n. 丑角,小丑,蠢货,笨蛋<br/>v. 做出蠢相,扮小丑|
859859
|triangular|adj. 三角形的,涉及三方的|
@@ -1212,6 +1212,9 @@ categories: [自用]
12121212
|||
12131213
|prayer|n. 祷告,祈祷文,经文|
12141214
|better-off|adj. 比较富裕的,境况较好的|
1215+
|||
1216+
|courteous|adj. 有礼貌的,客气的|
1217+
|postpone|v. 延期,延迟|
12151218

12161219
<p class="wordCounts">单词收录总数</p>
12171220

tryGoPy/MGJW/thisWeek/temp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-09 12:56:53
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-09 13:10:28
6+
*/
7+
8+
【离谱!把原神设为微信头像,别人点开30秒微信闪退】 【精准空降到 00:38】 https://www.bilibili.com/video/BV1Ze7HznEou/?share_source=copy_web&vd_source=92aeccdc2df6dec97830880acc658895&t=38
9+
10+
11+
【【黑客】如何在几秒内入侵你的电脑 从BadUSB的实现到近源渗透】 【精准空降到 07:27】 https://www.bilibili.com/video/BV1orTUzPEiX/?share_source=copy_web&vd_source=92aeccdc2df6dec97830880acc658895&t=447
12+
键盘灯闪烁获取隔离网络中的文件

0 commit comments

Comments
 (0)