|
| 1 | +Given an integer array nums, return the number of subarrays of length 3 such that the sum of the first and third numbers equals exactly half of the second number. |
| 2 | + |
| 3 | + |
| 4 | + class Solution: |
| 5 | + def countSubarrays(self, nums: List[int]) -> int: |
| 6 | + res = 0 |
| 7 | + for i in range(len(nums)-2): |
| 8 | + |
| 9 | + sub3 = nums[i:i+3] |
| 10 | + |
| 11 | + a,b,c = sub3 |
| 12 | + if 2*(a+c) == b: |
| 13 | + res+=1 |
| 14 | + return res |
| 15 | + |
| 16 | +-------------------------------------------------------------------------- |
| 17 | + |
| 18 | +class Solution: |
| 19 | + def countSubarrays(self, nums: List[int]) -> int: |
| 20 | + return sum(2*(nums[i-1]+nums[i+1])==nums[i] for i in range(1, len(nums)-1)) |
| 21 | + |
0 commit comments