Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 64c2f24

Browse files
authored
Create Count Subarrays of Length Three With a Condition.py
1 parent 4010372 commit 64c2f24

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /