반응형
1. Stack
- 선언
stack <char> s1;
- push
s1.push('c');
- pop
s1.pop();
- 스택 가장 위의 값
s1.top();
- 예제
#include<string>
#include <iostream>
#include <stack>
using namespace std;
bool solution(string s)
{
bool answer = true;
stack <char> s1;
for(int i=0; i<s.length(); i++){
if(s[i] == '('){
s1.push(s[i]);
} else if(s[i] == ')'){
if(s1.empty() || s1.top() == ')'){
answer = false;
break;
}
s1.pop();
}
}
if(!s1.empty()){
answer = false;
}
return answer;
}
2. Queue
- 선언
Queue<int> q;
- push
q.push(1);
- pop
q.pop();
- 큐 맨 앞의 값
q.front();
- 큐 맨 뒤의 값
q.back();
- 비었는지 체크
q.empty();
- 큐에 몇 개의 값이 들었는지
q.size();
반응형
'Programming > Coding Test' 카테고리의 다른 글
7. 순열과 조합 정리 (0) | 2021.01.01 |
---|---|
6. DFS - 부분집합 (0) | 2020.12.31 |
[C++] 백준 2573 빙산 - 테스트 케이스 (0) | 2020.11.15 |
런타임에러 1 - return 1 return 0 return -1 의미 (0) | 2020.11.14 |
4. BFS와 DFS의 장단점, 차이 (0) | 2020.11.04 |