정답 1 - mine
이 문제를 처음 딱 봤을때 들었던 생각은 int 배열에 목과 나머지를 계산해서 때려 넣자! 였다.
그리하여 아래와 같이 작성해보았다.
class Solution {
public int[] solution(int money) {
int priceOfCoffee = 5500;
int affortAmount = money / priceOfCoffee;
int change = money % priceOfCoffee;
int[] answer = {affortAmount, change};
return answer;
}
}
결과는 작동 됐다.
하지만 코드가 너무 길어보였다. 그래서 고민끝에 아래 정답 2 와같이 리펙토링을 해보았다.
정답 2 - mine
class Solution {
public int[] solution(int money) {
int[] answer = {money / 5500, money % 5500};
return answer;
}
}
훨씬 간결하고 깔끔해졌다.
이후 다른사람들은 어떻게 했을지가 궁금했다.
정답 3
class Solution {
public int[] solution(int money) {
return new int[] { money / 5500, money % 5500 };
}
}
더 간결하고 깔끔한 답이 있었다. 저렇게 int 배열을 바로 초기화 해버리는 방법도 있었다.
'프로그래머스 - 코딩테스트' 카테고리의 다른 글
[Java][프로그래머스] - LVL. 0 문자열 뒤집기 (1) | 2023.01.22 |
---|---|
[Java][프로그래머스] - LVL. 0 배열원소의 길이 (0) | 2023.01.22 |
[Java][프로그래머스] - LVL.0 피자나눠먹기 (0) | 2023.01.22 |
[Java][프로그래머스] - LVL.0 배열의 평균값 (0) | 2023.01.21 |
[Java][프로그래머스] - (LVL.0) 두수의 나눗셈 (0) | 2023.01.21 |