Open enoch012 opened 11 months ago
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.HashSet;
class Solution {
public int solution(int a, int b, int c, int d) {
int answer = 0;
List<Integer> diceList = new ArrayList<>(Arrays.asList(a, b, c, d));
// Set 으로 변환, 크기가 4가 아니라면 중복이 있다는 뜻
Set<Integer> diceSet = new HashSet<>(diceList);
int diceSetSize = diceSet.size();
switch(diceSetSize){
case 4 : // 숫자가 모두 다름
answer = case4(diceList); break;
case 3 : // 2개 숫자가 중복, 나머지 두개는 서로 다름
answer = case3(diceList, diceSet); break;
case 2 : // 숫자가 각 2개씩 중복, 중복이 2:2인 경우와 3:1인 경우 분리
answer = case2(diceList, diceSet); break;
case 1 : // 숫자가 모두 동일함
answer = diceList.get(0) * 1111; break;
}
return answer;
}
int case4(List<Integer> diceList){
Collections.sort(diceList); // 오름차순으로 정렬
return diceList.get(0); // 최소값 리턴
}
int case3(List<Integer> diceList, Set<Integer> diceSet){
int dupliNum = 0;
for(int num : diceSet){
if(Collections.frequency(diceList, num) == 2){
dupliNum = num; // 중복 값 알아내기
}
}
// 중복 값이 없을 때까지 삭제 (실제론 2번)
while(diceList.contains(dupliNum)) diceList.remove(new Integer(dupliNum));
return diceList.get(0) * diceList.get(1); // 나머지 두 수 곱하기
}
int case2(List<Integer> diceList, Set<Integer> diceSet){
int answer = 0;
int numFirst = diceList.get(0); // 리스트의 첫번째 숫자
int dupliNum = Collections.frequency(diceList, numFirst); // 첫 숫자의 중복 횟수
List<Integer> diceList2 = new ArrayList<>(diceSet); // 중복 제거한 set을 다시 list로
int num1 = diceList2.get(0);
int num2 = diceList2.get(1);
switch(dupliNum){
case 1 :
if(num1 == numFirst){
// 같다면 나머지 3개가 중복인 것
answer = (int) Math.pow(((10 * num2) + num1), 2);
} else {
// 아니라면 중복수인 것
answer = (int) Math.pow(((10 * num1) + num2), 2);
} break;
case 2 : // 중복이 2:2인 경우
answer = (num1 + num2) * Math.abs(num1 - num2); break;
case 3 :
if(num1 == numFirst){
// 같다면 중복수인 것
answer = (int) Math.pow(((10 * num1) + num2), 2);
} else {
// 아니라면 나머지 다른 하나의 수
answer = (int) Math.pow(((10 * num2) + num1), 2);
} break;
}
return answer;
}
}
Arrays.asList()
앞에 new ArrayList<>()
로 감싸주고 안 감싸주고 여부에 따라 수정 가능 여부가 정해진다는 걸 알았다. 참고quiz
의 길이≤ 10import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
class Solution {
public String[] solution(String[] quizList) {
List<String> answerList = new ArrayList<>(); // "O", "X"를 담을 리스트
for(String quiz : quizList){
String[] quizSplit = quiz.split(" "); // 공백 기준으로 문자열을 잘라서 배열로 만들기
int num1 = Integer.parseInt(quizSplit[0]);
int num2 = Integer.parseInt(quizSplit[2]);
int quizAnswer = Integer.parseInt(quizSplit[4]);
int trueAnswer = quizSplit[1].equals("+") ? num1 + num2 : num1 - num2;
if(quizAnswer == trueAnswer) answerList.add("O");
else answerList.add("X");
}
String[] answer = answerList.toArray(new String[0]);
return answer;
}
}
옹알이 (1)
0단계 문제 중에 가장 정답률이 가장 낮길래 풀어보았다.
문제
제한사항
price
는 10원 단위로(1의 자리가 0) 주어집니다. 소수점 이하를 버린 정수를 return합니다.babbling
의 길이≤ 100babbling[i]
의 길이≤ 15입출력 예
babbling | result -- | -- ["aya", "yee", "u", "maa", "wyeoo"] | 1 ["ayaye", "uuuma", "ye", "yemawoo", "ayaa"] | 3유의사항
문제풀이
회고