본문 바로가기

Stack

Decode String (Stack) LeetCode의 Queue, Stack 튜토리얼의 마지막 섹션의 세 번째 문제인 Decode String이다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 일정한 규칙으로 주어진 문자열을 해독하는 것이 목적인데 문제의 규칙은 다음과 같다. 문자열에 k[encoded_string] 포맷이 있다면 encoded_string을 k번 반복해서 이어 붙인 .. 더보기
Implement Stack using Queues LeetCode의 Queue, Stack 튜토리얼에서 마지막 섹션의 두 번째 문제인 Implement Stack using Queues다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 이번 문제에서는 지난 문제(스택을 이용한 큐 구현)처럼 큐를 이용하여 스택을 구현하는 것을 목표로 하고 있다. 역시 큐 자료구조에서 사용할 수 있는 연산(push, p.. 더보기
Implement Queue using Stacks 이번 문제는 LeetCode의 Queue, Stack 튜토리얼에서 마지막 섹션의 첫 번째 문제인 Implement Queue using Stacks다. 이전에 학습할 때 큐와 스택의 가장 큰 차이점은 선입선출이냐 선입후출이냐의 차이였다. 즉 먼저 삽입된 요소가 먼저 삭제되거나 가장 나중에 삭제되는 완전 반대의 역할을 수행하고 있는데 스택을 이용하여 반대의 역할을 하는 큐를 구현할 수 있을까? 결론적으로는 가능하다. 하지만 추가적인 메모리 공간이 필요한데 일단 문제에서 요구하는 것은 다음과 같다. 구현된 큐는 push, pop, peek, empty 네 가지 연산을 수행할 수 있어야 한다. 큐를 구현할 때는 Stack 자료구조에서 지원하는 연산(push, pop, size, empty 등)만 사용해야 한다.. 더보기
Binary Tree Inorder Traversal (Stack) LeetCode의 Queue, Stack 튜토리얼에서 Stack과 DFS 섹션의 마지막 문제인 Binary Tree Inorder Traversal다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 이전까지는 주로 재귀를 이용하여 DFS를 구현했다면 이번에는 명확하게 Stack 자료구조를 활용하여 문제를 풀어보라고 하고 있다. 그럼에도 불구하고 역시 .. 더보기