-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 最长交替子数组 | ||
// https://leetcode.cn/problems/longest-alternating-subarray | ||
// INLINE ../../images/array/longest_alternating_subarray.jpeg | ||
|
||
#include <headers.hpp> | ||
|
||
class Solution { | ||
public: | ||
int alternatingSubarray(vector<int> &nums) { | ||
int res = -1; | ||
int n = nums.size(); | ||
for (int firstIndex = 0; firstIndex < n; firstIndex++) { | ||
for (int i = firstIndex + 1; i < n; i++) { | ||
int length = i - firstIndex + 1; | ||
if (nums[i] - nums[firstIndex] == (length - 1) % 2) { | ||
res = max(res, length); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <array/longest_alternating_subarray.cpp> | ||
|
||
TEST(最长交替子数组, alternatingSubarray) { | ||
Solution solution; | ||
// 示例 1: | ||
// 输入:nums = [2,3,4,3,4] | ||
// 输出:4 | ||
// 解释:交替子数组有 [3,4] ,[3,4,3] 和 [3,4,3,4] 。最长的子数组为 [3,4,3,4] | ||
// ,长度为4 。 | ||
vector<int> nums = {2, 3, 4, 3, 4}; | ||
EXPECT_EQ(solution.alternatingSubarray(nums), 4); | ||
|
||
// 示例 2: | ||
// 输入:nums = [4,5,6] | ||
// 输出:2 | ||
// 解释:[4,5] 和 [5,6] 是仅有的两个交替子数组。它们长度都为 2 。 | ||
nums = {4, 5, 6}; | ||
EXPECT_EQ(solution.alternatingSubarray(nums), 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// 执行编译时间:2024-01-21 14:50:51 | ||
// 执行编译时间:2024-01-23 10:16:33 | ||
#include <gtest/gtest.h> | ||
#include <lib.hpp> | ||
|
||
|