ARTS 第15周

2019-07-15  本文已影响0人  陈卧虫

ARTS 第15周分享

[TOC]

Algorithm

232. 用栈实现队列

难度简单

[思路]

通过两个栈来逆向取出第一个元素

[参考代码]

package main

import (
    "fmt"
    "github.com/pkg/errors"
)

type MyStack struct {
    myStack []int
    front   int
}

func NewMyStack() *MyStack {
    return &MyStack{myStack: make([]int, 0), front: -1}
}

func (s *MyStack) isEmpty() bool {
    return s.front == -1
}

func (s *MyStack) push(num int) {
    s.myStack = append(s.myStack, num)
    s.front++
}

func (s *MyStack) pop() (num int, err error) {
    if s.isEmpty() {
        return 0, errors.New("empty stack")
    }
    num = s.myStack[s.front]
    s.myStack = s.myStack[:s.front]
    s.front--
    return
}

type MyQueue struct {
    myStackIn  MyStack
    myStackOut MyStack
}

/** Initialize your data structure here. */
func Constructor() MyQueue {
    return MyQueue{myStackIn: *NewMyStack(), myStackOut: *NewMyStack()}
}

/** Push element x to the back of queue. */
func (this *MyQueue) Push(x int) {
    this.myStackIn.push(x)
}

/** Removes the element from in front of queue and returns that element. */
func (this *MyQueue) Pop() int {
    if this.myStackIn.isEmpty() {
        return -1
    }
    for !this.myStackIn.isEmpty() {
        tmp, _ := this.myStackIn.pop()
        this.myStackOut.push(tmp)
    }
    res, _ := this.myStackOut.pop()
    for !this.myStackOut.isEmpty() {
        tmp, _ := this.myStackOut.pop()
        this.myStackIn.push(tmp)
    }
    return res
}

/** Get the front element. */
func (this *MyQueue) Peek() int {
    if this.myStackIn.isEmpty() {
        return -1
    }
    for !this.myStackIn.isEmpty() {
        tmp, _ := this.myStackIn.pop()
        this.myStackOut.push(tmp)
    }
    res, _ := this.myStackOut.pop()
    this.myStackIn.push(res)
    for !this.myStackOut.isEmpty() {
        tmp, _ := this.myStackOut.pop()
        this.myStackIn.push(tmp)
    }
    return res
}

/** Returns whether the queue is empty. */
func (this *MyQueue) Empty() bool {
    return this.myStackIn.isEmpty()
}

Review

Channels in Go:https://go101.org/article/channel.html

<本文详细的讲述的 go中Channel的原理与内部结构和一些使用细节>

Tips

Git分支管理实践:https://mp.weixin.qq.com/s/k53LQi5UdQBcmT3YwoYKSQ

某位大佬的一篇介绍关于分支管理经验的总结,满满的都是干货!

个人笔记,只有部分(是本人觉得需要掌握的)

[其他]

因为最近负责公司阿里云服务器的管理,需要远程安装一个应用,可以不同的linux发行版有不同的安装命令,但尴尬的是不知道是哪个发行版,所以上网查了如何查看系统版本:

share

什么是Socket

进程管理工具:supervisord

适配器模式:

Adapter Pattern,通常被翻译成适配器模式,有时候也叫做包装模式(wrapper pattern)

本周阅读

第二周:1, 2, 3, 4, 5, 
如何打造一份优雅的简历?https://mp.weixin.qq.com/s/GJbGPQxpCPrfUFNE8ZZPnA
同步/异步/阻塞/非阻塞/BIO/NIO/AIO https://mp.weixin.qq.com/s/e-HPyBtQ1zlcKXnFu8ADog
什么是适配器模式? https://mp.weixin.qq.com/s/25oyWPGZAasJUePNX9sEgA

我的程序员蜕变之路: https://mp.weixin.qq.com/s/VgPR828g_vJJs1REUMQUQg
代码重构!你敢吗?https://mp.weixin.qq.com/s/2HK7t8GJUfF99HUDUYqzcA
Channels in Go https://go101.org/article/channel.html

golang实现set集合,变相实现切片去重  https://studygolang.com/articles/3291
为什么“剩男”大多因为穷,“剩女”却什么类型都有?https://mp.weixin.qq.com/s/Ol1Zb6GIiBrnpjNTJSG78Q
解决 Git 在 windows 下中文乱码的问题:https://gist.github.com/nightire/5069597

supervisor 管理进程简明教程: https://www.jianshu.com/p/bf2b3f4dec73
Git分支管理实践: https://mp.weixin.qq.com/s/k53LQi5UdQBcmT3YwoYKSQ   // good

常用查看Linux系统信息命令: https://blog.51cto.com/liguxk/152912
FFrequently Asked Questions:  https://www.johnvansickle.com/ffmpeg/faq/
开发 7 年,我学到了什么?https://mp.weixin.qq.com/s/kt4ZPSWTc8gtWoI_Qc_bHA
Channels in Go:https://go101.org/article/channel.html

上一篇下一篇

猜你喜欢

热点阅读