Programmers/Lv. 0 (完)

[Kotlin] Programmers 코딩테스트 입문 Day 25 시뮬레이션, 조건문, 수학

chattymin 2023. 6. 28. 13:52
728x90
반응형

문자열 밀기

class Solution {
    fun solution(A: String, B: String): Int {
        for(i in 0 until A.length){
            if (B.contains(A.slice(0 until A.length - i)) && B.startsWith(A.slice(A.length - i until A.length)))
                return i
        }
        return -1
    }
}

 

 

 

 

종이 자르기

class Solution {
    fun solution(M: Int, N: Int): Int = M * N -1
}

 

 

연속된 수의 합

class Solution {
    fun solution(num: Int, total: Int): IntArray {
        if (num%2 == 0) return (((total / num) - num / 2 + 1) .. ((total / num) + num / 2)).toList().toIntArray()
        return (((total / num) - num / 2) .. ((total / num) + num / 2)).toList().toIntArray()
    }
}

 

 

 

다음에 올 숫자

class Solution {
    fun solution(common: IntArray): Int =
    if (common[1] - common[0] == common[2] - common[1]) common.last() + common[1] - common[0]
    else common.last() * (common[1] / common[0])
}
728x90
반응형