티스토리 뷰
https://school.programmers.co.kr/learn/courses/11132/lessons/71148
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
## 문제
## 코드
package cos_pro_java;
import java.util.*;
import java.util.ArrayList;
class Solution {
/** stack의 첫번째 값을 빼서 리턴 method */
Integer func_a(ArrayList<Integer> stack) {
Integer item = stack.remove(stack.size() - 1);
return item;
}
/** stack1이 빌 때 까지 빼서 stack2로 옮기는 메소드 */
void func_b(ArrayList<Integer> stack1, ArrayList<Integer> stack2) {
while(!func_c(stack1)) {
Integer item = func_a(stack1);
stack2.add(item);
}
}
/** stack이 비었는지 chk하는 method */
boolean func_c(ArrayList<Integer> stack) {
return (stack.size() == 0);
}
/** 문제의 기능을 수행 method */
public int solution(ArrayList<Integer> stack1, ArrayList<Integer> stack2) {
if(func_c(stack2)) {
func_b(stack1, stack2);
}
Integer answer = (int)func_a(stack2);
return answer;
}
}
public class Programmers_1_stack {
// 아래는 테스트케이스 출력을 해보기 위한 main 메소드입니다.
public static void main(String[] args) {
Solution sol = new Solution();
ArrayList<Integer> stack1_1 = new ArrayList<Integer>();
ArrayList<Integer> stack2_1 = new ArrayList<Integer>();
stack1_1.add(1);
stack1_1.add(2);
stack2_1.add(3);
stack2_1.add(4);
int ret1 = sol.solution(stack1_1, stack2_1);
System.out.println("solution 메소드의 반환 값은 " + ret1 + " 입니다.");
ArrayList<Integer> stack1_2 = new ArrayList<Integer>();
ArrayList<Integer> stack2_2 = new ArrayList<Integer>();
stack1_2.add(1);
stack1_2.add(2);
stack1_2.add(3);
int ret2 = sol.solution(stack1_2, stack2_2);
System.out.println("solution 메소드의 반환 값은 " + ret2 + " 입니다.");
}
}
'자격증 > 2023 cos pro java 1급' 카테고리의 다른 글
프로그래머스 cos pro java 모의고사 파트3 1번 (0) | 2023.03.13 |
---|---|
프로그래머스 cos pro java 모의고사 파트1 4번 (0) | 2023.03.13 |
프로그래머스 cos pro java 모의고사 파트1 3번 (0) | 2023.03.06 |
프로그래머스 cos pro java 모의고사 파트1 2번 (0) | 2023.03.06 |
시험 접수 및 준비 (0) | 2023.03.06 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 정처기
- 권오흠
- 난이도C
- 맥세팅
- 리눅스마스터2급실기
- 정보처리기사실기
- 2장요구사항확인
- Mac cpu모델명
- 정보처리기사
- Mac cpu
- Java
- mac세팅
- //축
- 2021정처기실기1회대비
- 2020정처기실기1회
- 백준
- 프로그래머스
- 2021정처기
- 정처기실기
- 난이도B
- 인프런
- 알고리즘
- K번째수
- 맥 크롬 다운로드
- 맥 크롬설치
- 리눅스마스터2급2차
- mac크롬
- 자료구조
- 정렬
- 스택
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함