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

Appearance settings
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 5ff4530

Browse filesBrowse files
tanjunchenlowzj
authored andcommitted
pkg/algorithm/algorithm.go:improve algorithm
Signed-off-by: tanjunchen <tanjunchen20@gmail.com>
1 parent 0ba788d commit 5ff4530
Copy full SHA for 5ff4530

File tree

Expand file treeCollapse file tree

1 file changed

+8
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+8
-2
lines changed
Open diff view settings
Collapse file

‎pkg/algorithm/algorithm.go‎

Copy file name to clipboardExpand all lines: pkg/algorithm/algorithm.go
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ func GCDSlice(s []int) int {
2727
if length == 1 {
2828
return s[0]
2929
}
30-
31-
return GCD(s[length-1], GCDSlice(s[:length-1]))
30+
commonDivisor := s[0]
31+
for i := 1; i < length; i++ {
32+
if commonDivisor == 1 {
33+
return commonDivisor
34+
}
35+
commonDivisor = GCD(commonDivisor, s[i])
36+
}
37+
return commonDivisor
3238
}
3339

3440
// GCD returns the greatest common divisor of x and y.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.