-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
49 additions
and
0 deletions.
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,25 @@ | ||
// 最长交替子数组 | ||
// https://leetcode.cn/problems/longest-alternating-subarray | ||
// INLINE ../../images/array/longest_alternating_subarray.jpeg | ||
|
||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn alternating_subarray(nums: Vec<i32>) -> i32 { | ||
let mut res = -1; | ||
let n = nums.len(); | ||
for first_index in 0..n { | ||
for i in (first_index + 1)..n { | ||
let length = i - first_index + 1; | ||
if nums[i] - nums[first_index] | ||
== TryInto::<i32>::try_into((length - 1) % 2).unwrap() | ||
{ | ||
res = res.max(length as i32); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
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
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,18 @@ | ||
use rust_practice::array::longest_alternating_subarray::Solution; | ||
|
||
#[test] | ||
fn alternating_subarray_test() { | ||
// 示例 1: | ||
// 输入:nums = [2,3,4,3,4] | ||
// 输出:4 | ||
// 解释:交替子数组有 [3,4] ,[3,4,3] 和 [3,4,3,4] 。最长的子数组为 [3,4,3,4] ,长度为4 。 | ||
let nums = vec![2, 3, 4, 3, 4]; | ||
assert_eq!(Solution::alternating_subarray(nums), 4); | ||
|
||
// 示例 2: | ||
// 输入:nums = [4,5,6] | ||
// 输出:2 | ||
// 解释:[4,5] 和 [5,6] 是仅有的两个交替子数组。它们长度都为 2 。 | ||
let nums = vec![4, 5, 6]; | ||
assert_eq!(Solution::alternating_subarray(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