Skip to content

Commit 9da49d1

Browse files
committed
feat: add solutions to lc problem: No.1250
No.1250.Check If It Is a Good Array
1 parent c62e79e commit 9da49d1

File tree

6 files changed

+148
-2
lines changed

6 files changed

+148
-2
lines changed

solution/1200-1299/1250.Check If It Is a Good Array/README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,80 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
**方法一:数学(裴蜀定理)**
51+
52+
我们可以考虑选取两个数的情况,若选取的数是 $a$ 和 $b$,那么根据题目的要求,我们需要满足 $a \times x + b \times y = 1$,其中 $x$ 和 $y$ 是任意整数。
53+
54+
而根据裴蜀定理,可以得知,如果 $a$ 和 $b$ 互质,那么上述等式一定有解。
55+
56+
因此,我们只需要判断在数组 `nums` 中是否存在两个互质的数即可。两个数互质的充要条件是它们的最大公约数为 $1$。如果数组 `nums` 存在 $a$ 和 $b$ 的最大公约数为 $1$,那么数组 `nums` 中的所有数的最大公约数也为 $1$。所以我们将题目转化为:判断数组 `nums` 中的所有数的最大公约数是否为 $1$ 即可。
57+
58+
时间复杂度 $O(n + log m)$,空间复杂度 $O(1)$,其中 $n$ 是数组 `nums` 的长度,而 $m$ 是数组 `nums` 中的最大值。
59+
5060
<!-- tabs:start -->
5161

5262
### **Python3**
5363

5464
<!-- 这里可写当前语言的特殊实现逻辑 -->
5565

5666
```python
57-
67+
class Solution:
68+
def isGoodArray(self, nums: List[int]) -> bool:
69+
return reduce(gcd, nums) == 1
5870
```
5971

6072
### **Java**
6173

6274
<!-- 这里可写当前语言的特殊实现逻辑 -->
6375

6476
```java
77+
class Solution {
78+
public boolean isGoodArray(int[] nums) {
79+
int g = 0;
80+
for (int x : nums) {
81+
g = gcd(x, g);
82+
}
83+
return g == 1;
84+
}
85+
86+
private int gcd(int a, int b) {
87+
return b == 0 ? a : gcd(b, a % b);
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
bool isGoodArray(vector<int>& nums) {
98+
int g = 0;
99+
for (int x : nums) {
100+
g = gcd(x, g);
101+
}
102+
return g == 1;
103+
}
104+
};
105+
```
65106
107+
### **Go**
108+
109+
```go
110+
func isGoodArray(nums []int) bool {
111+
g := 0
112+
for _, x := range nums {
113+
g = gcd(x, g)
114+
}
115+
return g == 1
116+
}
117+
118+
func gcd(a, b int) int {
119+
if b == 0 {
120+
return a
121+
}
122+
return gcd(b, a%b)
123+
}
66124
```
67125

68126
### **...**

solution/1200-1299/1250.Check If It Is a Good Array/README_EN.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,61 @@
4949
### **Python3**
5050

5151
```python
52-
52+
class Solution:
53+
def isGoodArray(self, nums: List[int]) -> bool:
54+
return reduce(gcd, nums) == 1
5355
```
5456

5557
### **Java**
5658

5759
```java
60+
class Solution {
61+
public boolean isGoodArray(int[] nums) {
62+
int g = 0;
63+
for (int x : nums) {
64+
g = gcd(x, g);
65+
}
66+
return g == 1;
67+
}
68+
69+
private int gcd(int a, int b) {
70+
return b == 0 ? a : gcd(b, a % b);
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
bool isGoodArray(vector<int>& nums) {
81+
int g = 0;
82+
for (int x : nums) {
83+
g = gcd(x, g);
84+
}
85+
return g == 1;
86+
}
87+
};
88+
```
5889
90+
### **Go**
91+
92+
```go
93+
func isGoodArray(nums []int) bool {
94+
g := 0
95+
for _, x := range nums {
96+
g = gcd(x, g)
97+
}
98+
return g == 1
99+
}
100+
101+
func gcd(a, b int) int {
102+
if b == 0 {
103+
return a
104+
}
105+
return gcd(b, a%b)
106+
}
59107
```
60108

61109
### **...**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
bool isGoodArray(vector<int>& nums) {
4+
int g = 0;
5+
for (int x : nums) {
6+
g = gcd(x, g);
7+
}
8+
return g == 1;
9+
}
10+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func isGoodArray(nums []int) bool {
2+
g := 0
3+
for _, x := range nums {
4+
g = gcd(x, g)
5+
}
6+
return g == 1
7+
}
8+
9+
func gcd(a, b int) int {
10+
if b == 0 {
11+
return a
12+
}
13+
return gcd(b, a%b)
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public boolean isGoodArray(int[] nums) {
3+
int g = 0;
4+
for (int x : nums) {
5+
g = gcd(x, g);
6+
}
7+
return g == 1;
8+
}
9+
10+
private int gcd(int a, int b) {
11+
return b == 0 ? a : gcd(b, a % b);
12+
}
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def isGoodArray(self, nums: List[int]) -> bool:
3+
return reduce(gcd, nums) == 1

0 commit comments

Comments
 (0)