본문 바로가기
Study/Kotlin

프로그래머스 - 다음에 올 숫자 [Kotlin]

by 고체물리학 2025. 2. 6.

나의 풀이

class Solution {
    fun solution(common: IntArray): Int {
        val temp = IntArray(common.size - 1)
        var answer: Int = 0
        for (i in 0 until common.size - 1) {
            temp[i] = common[i + 1] - common[i]
        }
        if (temp.all { it == temp[0] }){
            answer = common.last() + temp[0]
        }else{
            val div = common[1]/common[0]
            answer = common[common.size-1] * div
        }
        return answer
    }
}

 

나는 배열에 있는 모든 값의 차를 temp에 담아서 체크했는데

다른사람풀이 보니까 common[1]-common[0] 이랑  common[2]-common[1] 인덱스 두개만 비교해서 체크하시는 분들도 많았다

간단하게 생각하자..

 

반응형

'Study > Kotlin' 카테고리의 다른 글

[Kotlin] Enum 값으로 객체 Enum 객체 찾기  (0) 2022.10.12
백준 1834 나머지와 몫이 같은 수  (0) 2022.10.04
백준 1264 모음의 개수  (0) 2022.09.29
백준 1259 팰린드롬수  (0) 2022.09.29
백준 1152 단어의 개수  (0) 2022.09.29

댓글