상세 컨텐츠

본문 제목

[Kotlin] 음양 더하기 // 두 정수 사이의 합

프로그래밍

by 독서와 여행 2021. 4. 18. 17:25

본문

class Solution {
    fun solution(absolutes: IntArray, signs: BooleanArray): Int {
        
        for ((index,sign) in signs.withIndex()){
            if(sign) absolutes[index] =  absolutes[index]
            else absolutes[index] = - absolutes[index]
        }
        
        var answer: Int = absolutes.sum()
        
        return answer
    }
}

좋아

 

class Solution {
    fun solution(a: Int, b: Int): Long {
        var answer: Long = 0
        
        if (a <= b) for (i in a..b) answer += i
        else for (i in b..a) answer += i
        return answer
    }
}

'프로그래밍' 카테고리의 다른 글

[Kotlin] 실패율  (0) 2021.04.18
[Python] 크레인 인형뽑기  (0) 2021.04.13
[Kotlin] 정규식  (0) 2021.04.10
[Kotlin] 시퀀스를 이용한 피보나치 수열  (0) 2021.04.09
[Kotlin] forEach / for EachIndexed  (0) 2021.04.08

관련글 더보기

댓글 영역