inblog logo
|
silver
    알고리즘문제풀기

    [알고리즘문제풀기] 조건에 맞게 수열 변환하기 1

    silver's avatar
    silver
    Aug 19, 2025
    [알고리즘문제풀기] 조건에 맞게 수열 변환하기 1
    Contents
    문제내가 작성한 정답다른 사람들의 정답

    문제

    school.programmers.co.kr
    https://school.programmers.co.kr/learn/courses/30/lessons/181882

    내가 작성한 정답

    class Solution { public int[] solution(int[] arr) { for(int i=0; i<arr.length; i++){ int a = arr[i]; if(a>=50 && a%2==0) arr[i] = a/2; else if(a<50 && a%2==1) arr[i] = a*2; } return arr; } }

    다른 사람들의 정답

    import java.util.*; class Solution { public int[] solution(int[] arr) { // 1. arr 배열을 스트림(IntStream)으로 변환 return Arrays.stream(arr) // 2. map() 메서드: 스트림의 각 요소를 조건에 따라 변환 .map(operand -> // 조건 1: operand가 50 이상이고 짝수인 경우 operand를 2로 나눔 operand >= 50 && operand % 2 == 0 ? operand / 2 : // 조건 2: operand가 50 미만이고 홀수인 경우 operand를 2배로 만듦 operand < 50 && operand % 2 == 1 ? operand * 2 : // 나머지 경우에는 operand를 그대로 반환 operand ) // 3. 변환된 스트림을 다시 배열(int[])로 수집 후 반환 .toArray(); } }
    Share article

    silver

    RSS·Powered by Inblog