🗒 문제
📝 나의 문제풀이
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]
반응형
'코딩테스트' 카테고리의 다른 글
[프로그래머스] 1로 만들기 (0) | 2025.01.19 |
---|---|
[프로그래머스] 조건에 맞게 수열 변환하기 2 (0) | 2025.01.19 |
[프로그래머스] 수열과 구간 쿼리 1 (0) | 2025.01.17 |
[프로그래머스] n보다 커질 때까지 더하기 (0) | 2025.01.17 |
[프로그래머스] 할 일 목록 (0) | 2025.01.17 |