Skip to content

Commit 21c029e

Browse files
committed
check-if-it-is-a-good-array
1 parent 4ca721a commit 21c029e

File tree

7 files changed

+67
-0
lines changed

7 files changed

+67
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ Step 2. Add the dependency
4949

5050
<summary>展开查看</summary>
5151

52+
https://leetcode.cn/problems/insufficient-nodes-in-root-to-leaf-paths
53+
54+
https://leetcode.cn/problems/check-if-it-is-a-good-array
55+
5256
https://leetcode.cn/problems/maximum-number-of-groups-getting-fresh-donuts/
5357

5458
https://leetcode.cn/problems/check-if-point-is-reachable/
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"style": "WebKit",
3+
"filter": [
4+
"*.c",
5+
"*.cc",
6+
"*.cpp",
7+
"*.h",
8+
"*.hh",
9+
"*.hpp"
10+
],
11+
"dirs": [
12+
"./"
13+
]
14+
}

check-if-it-is-a-good-array/index.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// +build ignore
2+
3+
#pragma once
4+
#include <vector>
5+
6+
using namespace std;
7+
template <class InputIt, class T, class BinaryOperation>
8+
constexpr // since C++20
9+
T
10+
accumulate2(InputIt first, InputIt last, T init, BinaryOperation op)
11+
{
12+
for (; first != last; ++first)
13+
init = op(std::move(init), *first); // std::move since C++11
14+
15+
return init;
16+
};
17+
int gcd2(int num1, int num2)
18+
{
19+
while (num2 != 0) {
20+
int temp = num1;
21+
num1 = num2;
22+
num2 = temp % num2;
23+
}
24+
return num1;
25+
};
26+
27+
class Solution {
28+
public:
29+
bool isGoodArray(vector<int>& nums)
30+
{
31+
int divisor = accumulate2(nums.begin(), nums.end(), nums[0], gcd2);
32+
return divisor == 1;
33+
}
34+
};

check-if-it-is-a-good-array/test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// +build ignore

format.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"style": "WebKit",
3+
"filter": [
4+
"*.c",
5+
"*.cc",
6+
"*.cpp",
7+
"*.h",
8+
"*.hh",
9+
"*.hpp"
10+
],
11+
"dirs": [
12+
"./"
13+
]
14+
}

insufficient-nodes-in-root-to-leaf-paths/index.hpp

Whitespace-only changes.

insufficient-nodes-in-root-to-leaf-paths/test.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)