https://school.programmers.co.kr/learn/courses/30/lessons/12906
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
- 알고리즘 분류 : 스택, 자료구조
- 레벨 : Lv1
스택 문제니까 스택 메서드에 집착해서 풀었다.. ㅎㅎ
import java.util.*;
public class Solution {
public int[] solution(int []arr) {
int[] answer = {};
Stack<Integer> numStack = new Stack<Integer>();
for(int element : arr){
if(numStack.isEmpty()){
numStack.push(element);
}
else if(numStack.peek()!=element){
numStack.push(element);
}
}
answer = numStack.stream().mapToInt(i->i).toArray();
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;
}
}

간결하고 효율 좋은 코드를 언제나 고려하자..
반응형
'코테 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 Lv2] 순위 검색 (0) | 2023.11.23 |
---|---|
[프로그래머스 Lv2] 행렬 테두리 회전하기 (1) | 2023.11.22 |
[자바 Lv0] 전국 대회 선발 고사 (0) | 2023.07.30 |
[프로그래머스 Lv0] 자바 안전지대 (0) | 2023.07.29 |
[프로그래머스 Lv.1] 개인정보 수집 유효기간 (0) | 2023.06.13 |
https://school.programmers.co.kr/learn/courses/30/lessons/12906
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
- 알고리즘 분류 : 스택, 자료구조
- 레벨 : Lv1
스택 문제니까 스택 메서드에 집착해서 풀었다.. ㅎㅎ
import java.util.*;
public class Solution {
public int[] solution(int []arr) {
int[] answer = {};
Stack<Integer> numStack = new Stack<Integer>();
for(int element : arr){
if(numStack.isEmpty()){
numStack.push(element);
}
else if(numStack.peek()!=element){
numStack.push(element);
}
}
answer = numStack.stream().mapToInt(i->i).toArray();
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;
}
}

간결하고 효율 좋은 코드를 언제나 고려하자..
반응형
'코테 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 Lv2] 순위 검색 (0) | 2023.11.23 |
---|---|
[프로그래머스 Lv2] 행렬 테두리 회전하기 (1) | 2023.11.22 |
[자바 Lv0] 전국 대회 선발 고사 (0) | 2023.07.30 |
[프로그래머스 Lv0] 자바 안전지대 (0) | 2023.07.29 |
[프로그래머스 Lv.1] 개인정보 수집 유효기간 (0) | 2023.06.13 |