새소식

알고리즘/문제

[BOJ/백준 - 10845] 큐

  • -

문제 출처: https://www.acmicpc.net/problem/10845

 

10845번: 큐

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net


public class BOJ_10845 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int len = Integer.parseInt(br.readLine());
ArrayList<Integer> al = new ArrayList<>();
for(int i=0; i<len; i++){
st = new StringTokenizer(br.readLine());
String temp = st.nextToken();
if("pop".equals(temp)){
if(al.isEmpty()){
System.out.println(-1);
}else{
System.out.println(al.remove(0));
}
}else if("size".equals(temp)){
System.out.println(al.size());
}else if("empty".equals(temp)){
if(al.isEmpty()){
System.out.println(1);
}else{
System.out.println(0);
}
}else if("front".equals(temp)){
if(al.isEmpty()){
System.out.println(-1);
}else{
System.out.println(al.get(0));
}
}else if("back".equals(temp)){
if(al.isEmpty()){
System.out.println(-1);
}else{
System.out.println(al.get(al.size()-1));
}
}else{ // push X
int num = Integer.parseInt(st.nextToken());
al.add(num);
}
}
}
}
view raw BOJ_10845.java hosted with ❤ by GitHub

그냥 편하게 어레이리스트 썼다 빨리 풀고 지나가고 싶었음

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.