Skip to content

Commit a203120

Browse files
authored
Create 2197.Replace-Non-Coprime-Numbers-in-Array.cpp
1 parent ead81c9 commit a203120

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<int> replaceNonCoprimes(vector<int>& nums)
4+
{
5+
vector<int>rets;
6+
for (int i=0; i<nums.size(); i++)
7+
{
8+
int x = nums[i];
9+
while (!rets.empty() && gcd(rets.back(), x)>1)
10+
{
11+
x = lcm(rets.back(), x);
12+
rets.pop_back();
13+
}
14+
rets.push_back(x);
15+
}
16+
return rets;
17+
}
18+
19+
long long lcm(long long a, long long b)
20+
{
21+
return (long long)a*(long long)b/gcd(a,b);
22+
}
23+
};

0 commit comments

Comments
 (0)