반응형
https://leetcode.com/problems/find-greatest-common-divisor-of-array/description/
Find Greatest Common Divisor of Array - LeetCode
Can you solve this real interview question? Find Greatest Common Divisor of Array - Given an integer array nums, return the greatest common divisor of the smallest number and largest number in nums. The greatest common divisor of two numbers is the largest
leetcode.com
주어진 정수 배열에서 최소, 최대값 구하고
최소, 최대값의 최대공약수를 구하는 문제
class Solution {
fun findGCD(nums: IntArray): Int {
fun gcd(a: Int, b:Int): Int = if(b != 0) gcd(b, a % b) else a
// 배열 정렬하고
val sorted = nums.sorted()
// 최소 최대값 구하고
val minInt = sorted[0]
val maxInt = sorted[nums.size -1]
// 최대공약수 리턴
return gcd(minInt, maxInt)
}
}
https://codechacha.com/ko/kotlin-sorting-list/
Kotlin - 리스트 정렬 방법 (sort, sortBy, sortWith)
리스트를 정렬하는 방법을 소개합니다. Immutable 리스트 정렬, Mutable 리스트 정렬, 역순으로 정렬, sortedWith(), sortWith(), sortedBy(), sortBy() sorted()는 데이터 변경이 안되는 리스트(Immutable list)의 정렬된
codechacha.com
#코틀린정렬
반응형
'IT > 알고리즘' 카테고리의 다른 글
79] 프로그래머스 a와 b 출력하기 kotlin (0) | 2023.10.05 |
---|---|
78] 1672. Richest Customer Wealth Kotlin (0) | 2023.04.19 |
76] ★ Leetcode 1071. Greatest Common Divisor of Strings Kotlin (0) | 2023.04.11 |
75] Leetcode 2413. Smallest Even Multiple Kotlin (0) | 2023.04.10 |
74] Leetcode 2574. Left and Right Sum Differences Kotlin (0) | 2023.04.06 |
댓글