HR30 Day 18: Queues and Stacks

Jmnote (토론 | 기여)님의 2018년 8월 24일 (금) 23:41 판 (→‎PHP)

1 개요

HR30 Day 18: Queues and Stacks
해커랭크 30 Days of Code
문제 풀이
10-19 Day e
HR30 Day 10: Binary Numbers

HR30 Day 11: 2D Arrays

HR30 Day 12: Inheritance

HR30 Day 13: Abstract Classes

HR30 Day 14: Scope

HR30 Day 15: Linked List

HR30 Day 16: Exceptions - String to Integer

HR30 Day 17: More Exceptions

HR30 Day 18: Queues and Stacks

HR30 Day 19: Interfaces

2 Java

public class Solution {
    // Write your code here.
    private Stack<Character> stack = new Stack<Character>();
    private Queue<Character> queue = new LinkedList<Character>();
    void pushCharacter(char ch) {
        stack.push(ch);
    }
    void enqueueCharacter(char ch) {
        queue.add(ch);
    }
    char popCharacter() {
        return stack.pop();
    }
    char dequeueCharacter() {
        return queue.remove();
    }

3 PHP

class Solution {
    // Write your code here
    private $stack = [];
    private $queue = [];
    function pushCharacter($ch) {
        array_push($this->stack, $ch);
    }
    function enqueueCharacter($ch) {
        array_push($this->queue, $ch);
    }
    function popCharacter() {
        return array_pop($this->stack);
    }
    function dequeueCharacter() {
        return array_shift($this->queue);
    }
}
class Solution {
    // Write your code here
    private $stack;
    private $queue;
    function __construct() {
        $this->stack = new SplStack();
        $this->queue = new SplQueue(); 
    }
    function pushCharacter($ch) {
        $this->stack->push($ch);
    }
    function enqueueCharacter($ch) {
        $this->queue->enqueue($ch);
    }
    function popCharacter() {
        return $this->stack->pop();
    }
    function dequeueCharacter() {
        return $this->queue->dequeue();
    }
}

4 Python

class Solution(object):
    # Write your code here
    def __init__(self):
        self.stack = []
        self.queue = []
    def pushCharacter(self, ch):
        self.stack.append(ch)
    def enqueueCharacter(self, ch):
        self.queue.insert(0, ch)
    def popCharacter(self):
        return self.stack.pop()
    def dequeueCharacter(self):
        return self.queue.pop()
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}