- jdk 클래스 : Stack
import array.MyArray;
public class MyArrayStack {
int top;
MyArray arrayStack;
public MyArrayStack()
{
top = 0;
arrayStack = new MyArray();
}
public MyArrayStack(int size)
{
arrayStack = new MyArray(size);
}
//데이터 넣기
public void push(int data)
{
if(isFull()){
System.out.println("stack is full");
return;
}
arrayStack.addElement(data);
top++;
}
//데이터 빼기
public int pop()
{
if (top == 0){
System.out.println("stack is empty");
return MyArray.ERROR_NUM;
}
return arrayStack.removeElement(--top);
}
//맨 위 데이터 확인하기
public int peek()
{
if (top == 0){
System.out.println("stack is empty");
return MyArray.ERROR_NUM;
}
return arrayStack.getElement(top-1);
}
//스택 사이즈 확인하기
public int getSize()
{
return top;
}
public boolean isFull()
{
if(top == arrayStack.ARRAY_SIZE){
return true;
}
else return false;
}
//스택이 비었는지 아닌지 살펴보기
public boolean isEmpty()
{
if (top == 0){
return true;
}
else return false;
}
//스택 전부 출력하기 (pop아님)
public void printAll()
{
arrayStack.printAll();
}
}
스택문제
LV2.주식가격
class Solution {
public int[] solution(int[] prices) {
int len = prices.length;
int[] answer = new int[len];
int i, j;
for (i = 0; i < len; i++) {
for (j = i + 1; j < len; j++) {
answer[i]++;
if (prices[i] > prices[j])
break;
}
}
return answer;
}
}