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

세상을 바꾸는 개발자·2023년 4월 30일
0

[문제링크 - 프로그래머스 - 조건에 맞게 수열 변환하기 3] https://school.programmers.co.kr/learn/courses/30/lessons/181835#

풀이 코드

class Solution {
    public int[] solution(int[] arr, int k) {
        int[] answer = new int[arr.length];
        
        if(k % 2 != 0) {
            for(int i=0; i<arr.length; i++){
                answer[i] = arr[i] * k;
            }
        } else {
            for(int i=0; i<arr.length; i++){
                answer[i] = arr[i] + k;
            }
        }
        return answer;
    }
}



다른 사람 풀이

import java.util.stream.IntStream;

class Solution {
    public int[] solution(int[] arr, int k) {
        if(k%2==0) {
            return IntStream.of(arr).map(i->i+k).toArray();
        }

        return IntStream.of(arr).map(i->i*k).toArray();
    }
}



효율성 테스트

내 풀이

다른 사람 풀이

profile
초심 잃지 않기

0개의 댓글

관련 채용 정보