Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
frankcbliu
leetcode-solution
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
leetcode-solution
/
leetcode
/
editor
/
cn
/
2765-longest-alternating-subarray.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
79 lines (74 loc) · 2 KB
main
Breadcrumbs
leetcode-solution
/
leetcode
/
editor
/
cn
/
2765-longest-alternating-subarray.cpp
Copy path
Top
File metadata and controls
Code
Blame
79 lines (74 loc) · 2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//给你一个下标从 0 开始的整数数组 nums 。如果 nums 中长度为 m 的子数组 s 满足以下条件,我们称它是一个 交替子序列 :
//
//
// m 大于 1 。
// s1 = s0 + 1 。
// 下标从 0 开始的子数组 s 与数组 [s0, s1, s0, s1,...,s(m-1) % 2] 一样。也就是说,s1 - s0 = 1 ,s2 -
//s1 = -1 ,s3 - s2 = 1 ,s4 - s3 = -1 ,以此类推,直到 s[m - 1] - s[m - 2] = (-1)ᵐ 。
//
//
// 请你返回 nums 中所有 交替 子数组中,最长的长度,如果不存在交替子数组,请你返回 -1 。
//
// 子数组是一个数组中一段连续 非空 的元素序列。
//
//
//
// 示例 1:
//
//
//输入:nums = [2,3,4,3,4]
//输出:4
//解释:交替子数组有 [3,4] ,[3,4,3] 和 [3,4,3,4] 。最长的子数组为 [3,4,3,4] ,长度为4 。
//
//
// 示例 2:
//
//
//输入:nums = [4,5,6]
//输出:2
//解释:[4,5] 和 [5,6] 是仅有的两个交替子数组。它们长度都为 2 。
//
//
//
//
// 提示:
//
//
// 2 <= nums.length <= 100
// 1 <= nums[i] <= 10⁴
//
//
// Related Topics 数组 枚举 👍 5 👎 0
#include "headers.h"
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public:
int alternatingSubarray(vector<int> &nums) {
int k = 0, maxk = 0;
for (int i = 1; i < nums.size(); ++i) {
cout << "i= " << i << " k=" << k << " " << multiply(k) << endl;
if (nums[i] - nums[i - 1] == multiply(k)) {
k++;
maxk = max(k, maxk);
} else if (nums[i] - nums[i - 1] == multiply(0)) {
k = 1;
} else {
k = 0;
}
}
if (maxk > 0) return maxk + 1;
return -1;
}
int multiply(int k) {
if (k % 2 == 0)
return 1;
else
return -1;
}
};
//leetcode submit region end(Prohibit modification and deletion)
int main() {
Solution s;
vector<int> arr{2, 3, 4, 3, 4};
cout << s.alternatingSubarray(arr) << endl;
}
You can’t perform that action at this time.