목록스택 (22)
N

스택(Stack) Last In First Out(LIFO) 개념을 가진 선형 자료구조 프링글스 과자를 연상하자! 가장 먼저 들어간 과자는 바닥에 위치하며, 가장 마지막에 들어간 과자는 맨 위에 있다. 그렇기 때문에 가장 마지막에 들어간 과자부터 차례대로 먹을 수 있다! 스택은 배열이나 연결 리스트로 구현 가능하다. JS의 배열은 기본적으로 스택으로 구현된다.(push(), pop() 존재) 데이터 추가 push는 스택에 데이터를 추가한다. 배열의 가장 마지막에 데이터를 저장한다. 데이터 삭제 pop은 스택의 가장 마지막 데이터를 삭제한다. 코드 const stack = []; stack.push(1); stack.push(2); stack.push(3); console.log(stack); stack...
232. Implement Queue using Stacks Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: void push(int x) Pushes element x to the back of the queue. int pop() Removes the element from the front of the queue and returns it. int peek() Returns the element ..
225. Implement Stack using Queues Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the MyStack class: void push(int x) Pushes element x to the top of the stack. int pop() Removes the element on the top of the stack and returns it. int top() Returns the element on the t..
20. Valid Parentheses Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Example 1: Input: s = "()" Output: true Example 2: Input: s = "()[]{}" Output: true Example 3: Input: s = "(]" Outpu..
https://programmers.co.kr/learn/courses/30/lessons/12909?language=javascript 코딩테스트 연습 - 올바른 괄호 괄호가 바르게 짝지어졌다는 것은 '(' 문자로 열렸으면 반드시 짝지어서 ')' 문자로 닫혀야 한다는 뜻입니다. 예를 들어 "()()" 또는 "(())()" 는 올바른 괄호입니다. ")()(" 또는 "(()(" 는 올바르지 않은 programmers.co.kr function solution(s){ const stack = []; for(let i = 0; i < s.length; i++){ if(s[i] === "("){ stack.push(s[i]); } else{ if(stack[stack.length - 1] == "("){ sta..
https://programmers.co.kr/learn/courses/30/lessons/60058?language=javascript 코딩테스트 연습 - 괄호 변환 카카오에 신입 개발자로 입사한 "콘"은 선배 개발자로부터 개발역량 강화를 위해 다른 개발자가 작성한 소스 코드를 분석하여 문제점을 발견하고 수정하라는 업무 과제를 받았습니다. 소스를 programmers.co.kr 스택과 재귀를 활용. 문제의 예시처럼 순서대로 코딩하면 된다. isBalanced(string) 함수 설명: 인자로 받은 string 배열이 균형잡힌 괄호인지 확인하는 함수. 스택을 이용해 괄호를 검사한다. stack이 비어있으면 string 배열은 균형잡힌 괄호라는 뜻으로 true를 반환한다. reverse(string) 함수..
programmers.co.kr/learn/courses/30/lessons/76502?language=javascript 코딩테스트 연습 - 괄호 회전하기 programmers.co.kr 스택 알고리즘. s를 s.length만큼 회전하면서 s가 올바른 괄호인지 확인한다. 바깥 for문은 s를 회전하는 반복문. 안쪽 for문은 회전한 s가 올바른 괄호인지 스택을 활용해 확인하는 반복문. stack이 비어있으면 올바른 괄호, 비어있지 않으면 올바르지 않은 괄호를 나타낸다. function solution(s) { let answer = 0; for(let i = 0; i < s.length; i++){ const stack = []; for(let j = 0; j < s.length; j++){ if(st..
programmers.co.kr/learn/courses/30/lessons/76502?language=cpp 코딩테스트 연습 - 괄호 회전하기 programmers.co.kr 스택을 이용한 문제 풀이. s를 왼쪽으로 s.size() 만큼 회전하면서 올바른 괄호인지 확인한다. 바깥 for문은 회전을 위한 for문. 안쪽 for문은 s의 괄호가 올바른 괄호인지 스택을 이용해 확인하는 for문이다. 스택이 비어져있으면 answer++, 비어있지 않으면 올바른 괄호가 아니기 때문에 아무런 행동도 하지 않는다. #include #include #include #include using namespace std; int solution(string s) { int answer = 0; queue q; int si..