본문 바로가기
Algorithm/Programmers

[프로그래머스/JAVA] LV1 - 음양 더하기

by 김비누! 2022. 3. 11.

프로그래머스 - 음양더하기

문제

레벨 1 문제라서 그런지 간단하다.
if문 대신 삼항 연산자로 풀었다.

코드

class Solution {
    public int solution(int[] absolutes, boolean[] signs) {
        int answer = 0;

        for(int i = 0; i < absolutes.length; i++){
            answer += signs[i] ? absolutes[i] : -absolutes[i];
        }

        return answer;
    }
}

댓글