Movatterモバイル変換


[0]ホーム

URL:


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

Commit0d50f59

Browse files
committed
Add solution #2447
1 parent25cf456 commit0d50f59

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#1,954 LeetCode solutions in JavaScript
1+
#1,955 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1847,6 +1847,7 @@
18471847
2443|[Sum of Number and Its Reverse](./solutions/2443-sum-of-number-and-its-reverse.js)|Medium|
18481848
2444|[Count Subarrays With Fixed Bounds](./solutions/2444-count-subarrays-with-fixed-bounds.js)|Hard|
18491849
2446|[Determine if Two Events Have Conflict](./solutions/2446-determine-if-two-events-have-conflict.js)|Easy|
1850+
2447|[Number of Subarrays With GCD Equal to K](./solutions/2447-number-of-subarrays-with-gcd-equal-to-k.js)|Medium|
18501851
2460|[Apply Operations to an Array](./solutions/2460-apply-operations-to-an-array.js)|Easy|
18511852
2462|[Total Cost to Hire K Workers](./solutions/2462-total-cost-to-hire-k-workers.js)|Medium|
18521853
2467|[Most Profitable Path in a Tree](./solutions/2467-most-profitable-path-in-a-tree.js)|Medium|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 2447. Number of Subarrays With GCD Equal to K
3+
* https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums and an integer k, return the number of subarrays of nums where
7+
* the greatest common divisor of the subarray's elements is k.
8+
*
9+
* A subarray is a contiguous non-empty sequence of elements within an array.
10+
*
11+
* The greatest common divisor of an array is the largest integer that evenly divides all the
12+
* array elements.
13+
*/
14+
15+
/**
16+
*@param {number[]} nums
17+
*@param {number} k
18+
*@return {number}
19+
*/
20+
varsubarrayGCD=function(nums,k){
21+
letresult=0;
22+
for(letstart=0;start<nums.length;start++){
23+
letcurrentGCD=nums[start];
24+
for(letend=start;end<nums.length;end++){
25+
currentGCD=gcd(currentGCD,nums[end]);
26+
if(currentGCD===k){
27+
result++;
28+
}
29+
}
30+
}
31+
32+
returnresult;
33+
34+
functiongcd(a,b){
35+
while(b){
36+
a%=b;
37+
[a,b]=[b,a];
38+
}
39+
returna;
40+
}
41+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp