알고리즘 풀이 방법입니다.
문제(Problem) -> 생각(Think) -> 해결책(Solution) -> 리뷰(Review) 를 통해서 정리해서 작성합니다.
Problem📄
https://school.programmers.co.kr/learn/courses/30/lessons/76501
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
Think🤔
false이면 마이너스 , true이면 플러스
반복문 돌리면서 삼항연산자 이용해서 true이면 더하고 아니면 마이너스
for( sing[i] == true ? sum += absolutes[i] : sum -= absolutes[i];)
Solution✍
class Solution {
public int solution(int[] absolutes, boolean[] signs) {
int answer = 0;
for(int i=0; i<absolutes.length; i++){
answer += (signs[i] == true) ? absolutes[i] : absolutes[i] * -1;
}
return answer;
}
}
Review🤩
음수를 * -1로 이용
'Algorithm' 카테고리의 다른 글
[프로그래머스] 크레인 인형뽑기 게임 (1) | 2022.09.23 |
---|---|
[프로그래머스] 키패드 누르기 (0) | 2022.08.10 |
[프로그래머스] 없는 숫자 더하기 (0) | 2022.08.08 |
[프로그래머스] 내적 (0) | 2022.08.08 |
[프로그래머스] 실패율 (0) | 2022.08.08 |