File tree Expand file tree Collapse file tree 6 files changed +200
-3
lines changed
solution/1900-1999/1979.Find Greatest Common Divisor of Array Expand file tree Collapse file tree 6 files changed +200
-3
lines changed Original file line number Diff line number Diff line change @@ -51,27 +51,100 @@ nums 中最大的数是 3
51
51
<li><code>1 <= nums[i] <= 1000</code></li>
52
52
</ul >
53
53
54
-
55
54
## 解法
56
55
57
56
<!-- 这里可写通用的实现逻辑 -->
58
57
58
+ 最大公约数算法:
59
+
60
+ ``` java
61
+ int gcd(int a, int b) {
62
+ return b > 0 ? gcd(b, a % b) : a;
63
+ }
64
+ ```
65
+
59
66
<!-- tabs:start -->
60
67
61
68
### ** Python3**
62
69
63
70
<!-- 这里可写当前语言的特殊实现逻辑 -->
64
71
65
72
``` python
66
-
73
+ class Solution :
74
+ def findGCD (self , nums : List[int ]) -> int :
75
+ return gcd(max (nums), min (nums))
67
76
```
68
77
69
78
### ** Java**
70
79
71
80
<!-- 这里可写当前语言的特殊实现逻辑 -->
72
81
73
82
``` java
83
+ class Solution {
84
+ public int findGCD (int [] nums ) {
85
+ int a = 1 , b = 1000 ;
86
+ for (int num : nums) {
87
+ a = Math . max(a, num);
88
+ b = Math . min(b, num);
89
+ }
90
+ return gcd(a, b);
91
+ }
92
+
93
+ private int gcd (int a , int b ) {
94
+ return b > 0 ? gcd(b, a % b) : a;
95
+ }
96
+ }
97
+ ```
98
+
99
+ ### ** C++**
100
+
101
+ ``` cpp
102
+ class Solution {
103
+ public:
104
+ int findGCD(vector<int >& nums) {
105
+ int a = 0, b = 1000;
106
+ for (int num : nums)
107
+ {
108
+ a = max(a, num);
109
+ b = min(b, num);
110
+ }
111
+ return gcd(a, b);
112
+ }
113
+ };
114
+ ```
74
115
116
+ ### **Go**
117
+
118
+ ```go
119
+ func findGCD(nums []int) int {
120
+ a, b := 0, 1000
121
+ for _, num := range nums {
122
+ a = max(a, num)
123
+ b = min(b, num)
124
+ }
125
+ return gcd(a, b)
126
+ }
127
+
128
+ func gcd(a, b int) int {
129
+ if b > 0 {
130
+ return gcd(b, a%b)
131
+ }
132
+ return a
133
+ }
134
+
135
+ func max(a, b int) int {
136
+ if a > b {
137
+ return a
138
+ }
139
+ return b
140
+ }
141
+
142
+ func min(a, b int) int {
143
+ if a < b {
144
+ return a
145
+ }
146
+ return b
147
+ }
75
148
```
76
149
77
150
### ** ...**
Original file line number Diff line number Diff line change @@ -58,13 +58,79 @@ The greatest common divisor of 3 and 3 is 3.
58
58
### ** Python3**
59
59
60
60
``` python
61
-
61
+ class Solution :
62
+ def findGCD (self , nums : List[int ]) -> int :
63
+ return gcd(max (nums), min (nums))
62
64
```
63
65
64
66
### ** Java**
65
67
66
68
``` java
69
+ class Solution {
70
+ public int findGCD (int [] nums ) {
71
+ int a = 1 , b = 1000 ;
72
+ for (int num : nums) {
73
+ a = Math . max(a, num);
74
+ b = Math . min(b, num);
75
+ }
76
+ return gcd(a, b);
77
+ }
78
+
79
+ private int gcd (int a , int b ) {
80
+ return b > 0 ? gcd(b, a % b) : a;
81
+ }
82
+ }
83
+ ```
84
+
85
+ ### ** C++**
86
+
87
+ ``` cpp
88
+ class Solution {
89
+ public:
90
+ int findGCD(vector<int >& nums) {
91
+ int a = 0, b = 1000;
92
+ for (int num : nums)
93
+ {
94
+ a = max(a, num);
95
+ b = min(b, num);
96
+ }
97
+ return gcd(a, b);
98
+ }
99
+ };
100
+ ```
67
101
102
+ ### **Go**
103
+
104
+ ```go
105
+ func findGCD(nums []int) int {
106
+ a, b := 0, 1000
107
+ for _, num := range nums {
108
+ a = max(a, num)
109
+ b = min(b, num)
110
+ }
111
+ return gcd(a, b)
112
+ }
113
+
114
+ func gcd(a, b int) int {
115
+ if b > 0 {
116
+ return gcd(b, a%b)
117
+ }
118
+ return a
119
+ }
120
+
121
+ func max(a, b int) int {
122
+ if a > b {
123
+ return a
124
+ }
125
+ return b
126
+ }
127
+
128
+ func min(a, b int) int {
129
+ if a < b {
130
+ return a
131
+ }
132
+ return b
133
+ }
68
134
```
69
135
70
136
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int findGCD (vector<int >& nums) {
4
+ int a = 0 , b = 1000 ;
5
+ for (int num : nums)
6
+ {
7
+ a = max (a, num);
8
+ b = min (b, num);
9
+ }
10
+ return gcd (a, b);
11
+ }
12
+ };
Original file line number Diff line number Diff line change
1
+ func findGCD (nums []int ) int {
2
+ a , b := 0 , 1000
3
+ for _ , num := range nums {
4
+ a = max (a , num )
5
+ b = min (b , num )
6
+ }
7
+ return gcd (a , b )
8
+ }
9
+
10
+ func gcd (a , b int ) int {
11
+ if b > 0 {
12
+ return gcd (b , a % b )
13
+ }
14
+ return a
15
+ }
16
+
17
+ func max (a , b int ) int {
18
+ if a > b {
19
+ return a
20
+ }
21
+ return b
22
+ }
23
+
24
+ func min (a , b int ) int {
25
+ if a < b {
26
+ return a
27
+ }
28
+ return b
29
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int findGCD (int [] nums ) {
3
+ int a = 1 , b = 1000 ;
4
+ for (int num : nums ) {
5
+ a = Math .max (a , num );
6
+ b = Math .min (b , num );
7
+ }
8
+ return gcd (a , b );
9
+ }
10
+
11
+ private int gcd (int a , int b ) {
12
+ return b > 0 ? gcd (b , a % b ) : a ;
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def findGCD (self , nums : List [int ]) -> int :
3
+ return gcd (max (nums ), min (nums ))
You can’t perform that action at this time.
0 commit comments