- 오늘의 문제
https://school.programmers.co.kr/learn/courses/30/lessons/12906
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
- 나의 풀이
import java.util.*;
public class Solution {
public int[] solution(int []arr) {
List<Integer> list = new ArrayList<Integer>();
list.add(arr[0]);
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[i - 1])
list.add(arr[i]);
}
int[] answer = new int[list.size()];
for (int i = 0; i < list.size(); i++)
answer[i] = list.get(i);
return answer;
}
}
🎯 다른사람 풀이
import java.util.*;
public class Solution {
public int[] solution(int []arr) {
ArrayList<Integer> tempList = new ArrayList<Integer>();
int preNum = 10;
for(int num : arr) {
if(preNum != num)
tempList.add(num);
preNum = num;
}
int[] answer = new int[tempList.size()];
for(int i=0; i<answer.length; i++) {
answer[i] = tempList.get(i).intValue();
}
return answer;
}
}
import java.util.*;
public class Solution {
public Stack<Integer> solution(int []arr) {
Stack<Integer> stack = new Stack<>();
for(int num : arr){
if(stack.size() == 0 || stack.peek() != num){
stack.push(num);
}
}
return stack;
}
}
- 알게 된 점
단순히 LinkedHashSet 등을 사용해서 풀려고 했는데 폭망.. 다른 분들 풀이 보면서 풀었다...
stack을 사용한 풀이도 있고, ArrayList를 이용한 풀이도 많았다.
풀이법은 다양하게 익혀놓자!
- 학습할 것
- stack
- ArrayList
'코딩테스트 TIL' 카테고리의 다른 글
[프로그래머스 86491] 99클럽 코테 스터디 6일차 TIL + 완전탐색 (0) | 2024.05.28 |
---|---|
[리트코드 2824] 99클럽 코테 스터디 5일차 TIL + 리스트 (0) | 2024.05.27 |
[리트코드 Valid Parentheses] 99클럽 코테 스터디 4일차 TIL + Stack (0) | 2024.05.23 |
[프로그래머스 42576] 99클럽 코테 스터디 2일차 TIL + Hash (0) | 2024.05.21 |
[프로그래머스 1845] 99클럽 코테 스터디 1일차 TIL + Hash (0) | 2024.05.20 |