π 곡λΆνλ μ§μ§μνμΉ΄λ μ²μμ΄μ§?
[μ΄κ²μ΄ μ½λ© ν μ€νΈλ€ with Python]_16_μ€νκ³Ό ν λ³Έλ¬Έ
[μ΄κ²μ΄ μ½λ© ν μ€νΈλ€ with Python]_16_μ€νκ³Ό ν
μ§μ§μνμΉ΄ 2022. 2. 2. 00:24220202 μμ±
<λ³Έ λΈλ‘κ·Έλ γμ΄κ²μ΄ μ·¨μ μ μν μ½λ© ν μ€νΈλ€γ μ youtubeλ₯Ό μ°Έκ³ ν΄μ 곡λΆνλ©° μμ±νμμ΅λλ€>
https://www.youtube.com/watch?v=7iLoLcna7Hw&list=PLVsNizTWUw7H9_of5YCB0FmsSc-K44y81&index=16
1. νμ
: λ§μ μμ λ°μ΄ν° μ€μμ μνλ λ°μ΄ν°λ₯Ό μ°Ύλ κ³Όμ
ex) DFS, BFS
2. μ€ν
: λ¨Όμ λ€μ΄ μ¨ λ°μ΄ν°κ° λμ€μ λκ°λ νμ (μ μ νμΆ)
: μ ꡬμ μΆκ΅¬κ° λμΌν νν
- python
stack = []
stack.append(5)
stack.append(3)
stack.append(2)
stack.append(1)
stack.pop()
stack.append(4)
stack.append(7)
stack.pop()
print(stack[::-1]) # μ΅μλ¨ λΆν°
print(stack) # μ΅νλ¨ λΆν°
# result
# [4, 2, 3, 5]
# [5, 3, 2, 4]
=> <μ΅ν, μ²μ> 5, 3, 2, 1(μμ ), 4, 7(μμ ) <μ΅μ, λμ€>
- c++
#include <bits/stdc++.h>
using namespace std;
stack<int> s;
int main(void) {
s.push(3);
s.push(2);
s.push(1);
s.push(5);
s.pop();
s.push(3);
s.push(1);
s.pop();
while (!s.empty()) {
cout << s.top() << ' '; # top : μ΅μλ¨λΆν°
s.pop();
}
}
- java
import java.util.*;
public class Main {
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
s.push(4);
s.push(5);
s.push(2);
s.push(1);
s.pop();
s.push(7);
s.push(8);
s.pop();
while (!s.empty()) {
System.out.print(s.peek() + " "); # peek : μ΅μλ¨λΆν°
s.pop();
}
}
}
3. ν
: λ¨Όμ λ€μ΄ μ¨ λ°μ΄ν°κ° λ¨Όμ λκ°λ νμ (μ μ μ μΆ)
: μ ꡬμ μΆκ΅¬κ° λͺ¨λ λ«λ € μλ ν°λ
- python
from collections import deque
queue = deque()
queue.append(5)
queue.append(4)
queue.append(1)
queue.append(2)
queue.popleft()
queue.append(2)
queue.append(3)
queue.popleft()
print(queue) # λ¨Όμ λ€μ΄μ¨ μ λΆν°
queue.reverse()
print(queue) # λμ€μ λ€μ΄μ¨ μ λΆν°
# result
# deque([1, 2, 2, 3])
# deque([3, 2, 2, 1])
=> <μ΅ν, μ²μ> 5(μμ 1), 4(μμ 2), 1, 2, 2, 3 <μ΅μ, λμ€>
- c++
#include <bits/stdc++.h>
using namespace std;
queue<int> q;
int main(void) {
q.push(3);
q.push(2);
q.push(3);
q.push(4);
q.pop();
q.push(1);
q.push(2);
q.pop();
while (!q.empty()) {
cout << q.front() << ' ';
q.pop()
}
}
- java
import java.util.*;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.offer(2);
q.offer(5);
q.offer(6);
q.offer(3);
q.poll();
q.offer(8);
q.offer(3);
q.poll();
while (!q.empty()) {
System.out.print(q.poll() + " ");
}
}
}
'π¦₯ μ½ν > μ΄κ²μ΄ μ½λ© ν μ€νΈλ€ with python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μ΄κ²μ΄ μ½λ© ν μ€νΈλ€ with Python]_18_DFS (0) | 2022.02.02 |
---|---|
[μ΄κ²μ΄ μ½λ© ν μ€νΈλ€ with Python]_17_μ¬κ· ν¨μ (0) | 2022.02.02 |
[μ΄κ²μ΄ μ½λ© ν μ€νΈλ€ with Python]_15_ꡬν λ¬Έμ νμ΄ (0) | 2022.01.31 |
[μ΄κ²μ΄ μ½λ© ν μ€νΈλ€ with Python]_14_ꡬν (0) | 2022.01.31 |
[μ΄κ²μ΄ μ½λ© ν μ€νΈλ€ with Python]_13_그리λ μκ³ λ¦¬μ¦ μ ν λ¬Έμ (0) | 2022.01.31 |