
class Solution {
public int solution(int[] absolutes, boolean[] signs) {
int answer = 123456789;
int result = 0;
for(int i=0;i<signs.length;i++){ // signs, absolutes의 배열의 크기는 같음
if(signs[i] == true){ // boolean형의 signs배열의 [i]값이 true라면 result의 값을 더하기
result += absolutes[i];
} else { // false라면 result값에서 false값 빼기
result-=absolutes[i];
}
}
answer = result; //answer에 result값 덮어쓰기
return answer;
}
}
이 문제는 간단한 원리만 알고있다면 편하게 풀 수 있는 문제였다.
다른 사람의 문제풀이를 보니
class Solution {
public int solution(int[] absolutes, boolean[] signs) {
int answer = 0;
for (int i=0; i<signs.length; i++)
answer += absolutes[i] * (signs[i]? 1: -1);
return answer;
}
}
이런식으로 푼 사람도 있었다. 이 코드가 정말 간결하게 작성을 잘한 것 같다.
'프로그래머스 문제 풀이' 카테고리의 다른 글
| Lv1 - 핸드폰 번호 가리기 (0) | 2025.02.21 |
|---|---|
| Lv1 - 두 개 뽑아서 더하기 (0) | 2025.02.19 |
| Lv1 - 숫자 문자열과 영단어 (0) | 2025.02.19 |
| Lv1 - 로또의 최고 순위와 최저 순위 (0) | 2025.02.19 |
| Lv1 - 부족한 금액 계산하기 (0) | 2025.02.19 |