LeetCode_Study_Plan/Programming Skills

232. Implement Queue using Stacks java

개발하는루루 2022. 9. 24. 15:38

https://leetcode.com/problems/implement-queue-using-stacks/

 

Implement Queue using Stacks - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class MyQueue {

    ArrayList<Integer> arr = new ArrayList<Integer>();

    public MyQueue() {
    }

    public void push(int x) {
        this.arr.add(x);
    }

    public int pop() {
        int tmp = this.arr.get(0);
        this.arr.remove(0);
        return tmp;
    }

    public int peek() {
        return this.arr.get(0);
    }

    public boolean empty() {
        if (this.arr.size() == 0) return true;
        return false;
    }
}

더 좋은 예제는 있을 거라고 보지만 나는 ArrayList를 활용했다.

참고 : https://coding-factory.tistory.com/551

 

[Java] 자바 ArrayList 사용법 & 예제 총정리

ArrayList란? ArrayList는 List 인터페이스를 상속받은 클래스로 크기가 가변적으로 변하는 선형리스트입니다. 일반적인 배열과 같은 순차리스트이며 인덱스로 내부의 객체를 관리한다는점등이 유

coding-factory.tistory.com

파이썬을 먼저 사용했어서 배열의 크기를 선언해서 시작하는 방식이 낯설다. 코딩테스트용으로는

ArrayList랑 친해져야겠다는 생각이 들었고, 문법이 기존 []방식과 조금씪 다른 면이 있어 사용에 익숙해져야겠다.