본문 바로가기
코딩테스트

[프로그래머스] 조건에 맞게 수열 변환하기 1

by liz_devel 2025. 1. 19.

🗒 문제


📝 나의 문제풀이

class Solution {
    fun solution(arr: IntArray): IntArray {
          return  arr.map{
                if(it >= 50 && it % 2 == 0){
                    it / 2   
                }else if(it < 50 && it % 2 != 0){
                    it * 2
                }else{
                    it
                }
            }.toIntArray()
    }
}

 


📝 다른 사람의 문제 풀이

class Solution {
    fun solution(arr: IntArray): List<Int> {
        return arr.map { if (it >= 50 && it % 2 == 0) it / 2 else if (it < 50 && it % 2 == 1) it * 2 else it }
    }
}

🖊 문제 풀이 시 알면 좋을 것

map

  • 설명: 컬렉션의 각 요소에 주어진 변환 함수를 적용해 새로운 List를 반환.
val list = listOf(1, 2, 3)
val result = list.map { it * 2 } // [2, 4, 6]

 

반응형