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 db5683c

Browse files
feat: add typescript solution to lc problem: No.2447
No.2447.Number of Subarrays With GCD Equal to K
1 parent 1fc0cbd commit db5683c

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

‎solution/2400-2499/2447.Number of Subarrays With GCD Equal to K/README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,24 @@ func gcd(a, b int) int {
145145
### **TypeScript**
146146

147147
```ts
148+
function subarrayGCD(nums: number[], k: number): number {
149+
const n = nums.length;
150+
let ans = 0;
151+
for (let i = 0; i < n; i++) {
152+
let x = nums[i];
153+
for (let j = i; j < n; j++) {
154+
x = gcd(nums[j], x);
155+
if (x == k) ans += 1;
156+
}
157+
}
158+
return ans;
159+
};
148160

161+
function gcd(a: number, b: number): number {
162+
if (a > b) [a, b] = [b, a];
163+
if (a == 0) return b;
164+
return gcd(b % a, a);
165+
}
149166
```
150167

151168
### **...**

‎solution/2400-2499/2447.Number of Subarrays With GCD Equal to K/README_EN.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,24 @@ func gcd(a, b int) int {
131131
### **TypeScript**
132132

133133
```ts
134+
function subarrayGCD(nums: number[], k: number): number {
135+
const n = nums.length;
136+
let ans = 0;
137+
for (let i = 0; i < n; i++) {
138+
let x = nums[i];
139+
for (let j = i; j < n; j++) {
140+
x = gcd(nums[j], x);
141+
if (x == k) ans += 1;
142+
}
143+
}
144+
return ans;
145+
};
134146

147+
function gcd(a: number, b: number): number {
148+
if (a > b) [a, b] = [b, a];
149+
if (a == 0) return b;
150+
return gcd(b % a, a);
151+
}
135152
```
136153

137154
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function subarrayGCD(nums: number[], k: number): number {
2+
const n = nums.length;
3+
let ans = 0;
4+
for (let i = 0; i < n; i++) {
5+
let x = nums[i];
6+
for (let j = i; j < n; j++) {
7+
x = gcd(nums[j], x);
8+
if (x == k) ans += 1;
9+
}
10+
}
11+
return ans;
12+
};
13+
14+
function gcd(a: number, b: number): number {
15+
if (a > b) [a, b] = [b, a];
16+
if (a == 0) return b;
17+
return gcd(b % a, a);
18+
}

0 commit comments

Comments
(0)

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