3101. Count Alternating Subarrays Medium

@problem@discussion
#Array#Math



1/**
2 * [3101] Count Alternating Subarrays
3 *
4 * You are given a <span data-keyword="binary-array">binary array</span> nums.
5 * We call a <span data-keyword="subarray-nonempty">subarray</span> alternating if no two adjacent elements in the subarray have the same value.
6 * Return the number of alternating subarrays in nums.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [0,1,1,1]</span>
11 * Output: <span class="example-io">5</span>
12 * Explanation:
13 * The following subarrays are alternating: [0], [1], [1], [1], and [0,1].
14 * </div>
15 * <strong class="example">Example 2:
16 * <div class="example-block">
17 * Input: <span class="example-io">nums = [1,0,1,0]</span>
18 * Output: <span class="example-io">10</span>
19 * Explanation:
20 * Every subarray of the array is alternating. There are 10 possible subarrays that we can choose.
21 * </div>
22 *  
23 * Constraints:
24 * 
25 * 	1 <= nums.length <= 10^5
26 * 	nums[i] is either 0 or 1.
27 * 
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/count-alternating-subarrays/
32// discuss: https://leetcode.com/problems/count-alternating-subarrays/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37    pub fn count_alternating_subarrays(nums: Vec<i32>) -> i64 {
38        
39    }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_3101() {
50    }
51}
52


Back
© 2025 bowen.ge All Rights Reserved.