[Java] 카드 뭉치 - Lv1 프로그래머스코딩테스트/프로그래머스2023. 3. 4. 13:09
Table of Contents
728x90
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/159994
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
goal을 컬렉션 List에 담아 맨 앞 값들이 같을 때 삭제처리했다.
import java.util.ArrayList;
import java.util.List;
class Solution {
public String solution(String[] cards1, String[] cards2, String[] goal) {
List<String> c1 = new ArrayList<>();
List<String> c2 = new ArrayList<>();
List<String> g = new ArrayList<>();
for(String s : cards1) c1.add(s);
for(String s : cards2) c2.add(s);
for(String s : goal) g.add(s);
boolean bl = true;
while(bl) {
if(g.size() == 0) {
return "Yes";
}
if(c1.size() > 0 && g.get(0).equals(c1.get(0))){
g.remove(0);
c1.remove(0);
}
else if(c2.size() > 0 && g.get(0).equals(c2.get(0))) {
g.remove(0);
c2.remove(0);
}
else {
bl = false;
}
}
return "No";
}
}
피드백
풀고 나서 생각해보니 cards1와 cards2의 idx값을 변수로 설정 해 두고 goals[i]와 cards1[idx]이나 cards2[idx] 가 같을 때 idx를 ++해주고 둘다 해당되지 않을 때 No를 리턴하는 것이 더 수월할 것 같다는 생각이 들었다.
728x90
300x250
@mag1c :: 꾸준히 재밌게
2023.04 ~ 백엔드 개발자의 기록
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!