算法面试通关札记 03-栈和队列 - 极客时间 【系列】

330 阅读2分钟

算法与数据结构是程序员基本功,但是入门着实不易,在极客时间找了一门非常适合新手人们入门的课程,一遍学习一遍记录,方便日后查看。编程语言选用Swift,并针对技术点丰富了一些感想和随笔。

Github的算法Repo包括了所有的系列札记,同时还有本人刷leetcode、《剑指offer》的历程。如果你感兴趣,可以去看看。Go to Github

堆栈和队列

Stack,先入后出,Access和Search O(n),Insertion和Deletion O(1) 复杂度。 实现:Array or LinkedList

Queue,先入先出,Access和Search O(n),Insertion和Deletion O(1) 复杂度。 实现:Array or LinkedList

Leetcode真题

20-有效的括号

class Solution {
    func isValid(_ s: String) -> Bool {
        var stack:[String] = []
        var initialParentheses:[String: String] = [")":"(","]":"[","}":"{"]

        for c in s {
            if !initialParentheses.keys .contains(String(c)) {
                stack.append(String(c));
            } else if stack.count > 0 && initialParentheses[String(c)] != stack.removeLast() {
                return false
            }
        }

        if stack.count == 0 {
            return true;
        } else {
            return false
        }
    }
}

class Solution {
    func isValid(_ s: String) -> Bool {
        var stack = [Character]()
        let map: [Character: Character] = [")":"(","]":"[","}":"{"]
        for c in s {
            if map.values.contains(c) {
                stack.append(c)
            } else if stack.isEmpty || map[c] != stack.popLast() {
                return false
            }
        }
        return stack.isEmpty
    }
}

232-用栈实现队列 思路:两个栈A和B,弹出操作的时候把元素从A挪到B,从B中依次弹出。直到B中清楚干净,再把A中的重新挪到B中。

class MyQueue {

    /** Initialize your data structure here. */
     var inputStack = [Int]()
     var outputStack = [Int]()
    init() {

    }

    /** Push element x onto stack. */
    func push(_ x: Int) {
        self.inputStack.append(x)
    }

    /** Removes the element on top of the stack and returns that element. */
    func pop() -> Int {


        if self.outputStack.count > 0 {
            return self.outputStack.removeLast()
        } else {
            while self.inputStack.count > 0 {
                self.outputStack.append(self.inputStack.removeLast())
            }
            return self.outputStack.removeLast()
        }
    }

    /** Get the top element. */
    func peek() -> Int {

        if self.outputStack.count > 0 {
            return self.outputStack.last ?? 0
        } else {
            while self.inputStack.count > 0 {
                self.outputStack.append(self.inputStack.removeLast())
            }
            return self.outputStack.last ?? 0
        }
    }

    /** Returns whether the stack is empty. */
    func empty() -> Bool {
        if self.outputStack.count > 0 {
            return false
        } else if (self.inputStack.count > 0) {
            return false
        } else {
            return true
        }
    }
}

class MyQueue {
    var input = [Int]()
    var output = [Int]()
    /** Initialize your data structure here. */
    init() {

    }
    /** Push element x to the back of queue. */
    func push(_ x: Int) {
        input.append(x)
    }
    /** Removes the element from in front of queue and returns that element. */
    func pop() -> Int {
        if !empty() {
            peek()
            return output.removeLast()
        }
        return 0
    }
    /** Get the front element. */
    func peek() -> Int {
        if output.isEmpty {
            while !input.isEmpty {
                output.append(input.removeLast())
            }
        }
        return output.last ?? 0
    }
    /** Returns whether the queue is empty. */
    func empty() -> Bool {
        return input.isEmpty && output.isEmpty
    }
}

225-用队列实现栈

思路

  • 两个队列queue1和queue2,其他操作简单,直接操作即可。
  • pop和top操作,先把queue1中除了第一个元素的其他元素放入queue2中,弹出(或查看)第一个元素,再把确queue2中的元素挪会queue1中。

本系列仅是个人学习笔记,并未包括所有教程内容,如有侵权请联系删除。 极客时间链接