Class Solution
-
- All Implemented Interfaces:
public final class Solution
2762 - Continuous Subarrays\.
Medium
You are given a 0-indexed integer array
nums
. A subarray ofnums
is called continuous if:Let
i
,i + 1
, ...,j
be the indices in the subarray. Then, for each pair of indices <code>i <= i<sub>1</sub>, i<sub>2</sub><= j</code>, <code>0 <= |numsi<sub>1</sub> - numsi<sub>2</sub>| <= 2</code>.
Return the total number of continuous subarrays.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = 5,4,2,4
Output: 8
Explanation:
Continuous subarray of size 1: 5, 4, 2, 4.
Continuous subarray of size 2: 5,4, 4,2, 2,4.
Continuous subarray of size 3: 4,2,4.
Thereare no subarrys of size 4.
Total continuous subarrays = 4 + 3 + 1 = 8.
It can be shown that there are no more continuous subarrays.
Example 2:
Input: nums = 1,2,3
Output: 6
Explanation:
Continuous subarray of size 1: 1, 2, 3.
Continuous subarray of size 2: 1,2, 2,3.
Continuous subarray of size 3: 1,2,3.
Total continuous subarrays = 3 + 2 + 1 = 6.
Constraints:
<code>1 <= nums.length <= 10<sup>5</sup></code>
<code>1 <= numsi<= 10<sup>9</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final Long
continuousSubarrays(IntArray nums)
-
-
Method Detail
-
continuousSubarrays
final Long continuousSubarrays(IntArray nums)
-
-
-
-