본문 바로가기

프로그래머스/LEVEL 1

[프로그래머스/LEVEL1] 음양 더하기

문제

 

https://programmers.co.kr/learn/courses/30/lessons/76501

 

코딩테스트 연습 - 음양 더하기

어떤 정수들이 있습니다. 이 정수들의 절댓값을 차례대로 담은 정수 배열 absolutes와 이 정수들의 부호를 차례대로 담은 불리언 배열 signs가 매개변수로 주어집니다. 실제 정수들의 합을 구하여 re

programmers.co.kr

 

코드

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

		// signs 값이 기준이 되는 sum
		int index = 0;
		for (boolean sign : signs) {
			result += sign == true ? absolutes[index] : -absolutes[index];
			index++;
		}

		return result;
    }
}