Sliding window approach, easy to understand.
Intuition\n Describe your first thoughts on how to solve this problem. \nwhen it comes to subarrays problem, i usually think of sliding window.\n# Approach\n De
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwhen it comes to subarrays problem, i usually think of sliding window.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nwe definitely have nums.size() into our result first.\nif (nums[r] == nums[r-1]) then we have r - l subarray satisfy.\nelse we move the left pointer to the current index of right pointer\n\n# Complexity\n- Time complexity:\n$$O(n)$$ \n- Space complexity:\n$$O(1)$$ \n<!-- Add your space complexity here, e.g. $O(n)$$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long countAlternatingSubarrays(vector<int>& nums) {\n        int l = 0, r = 1;\n        long long res = nums.size();\n        for (r; r < nums.size(); r++) {\n            if (nums[r] == nums[r-1]) \n                l = r;\n            else \n                res += r - l;\n        }\n        return res;\n    }\n};\n```
['Two Pointers', 'Sliding Window', 'C++']