GO算法-栈队列转换之互通有无

栈:先进后出

时间复杂度:入出O(1),查找O(n)

使用场景:10进制转2进制

23 %2 = 1

11 %2 = 1

5 %2 = 1

2 %2 = 0

1 %2 = 1

tack = [1, 1, 1, 0, 1]

res  = 1 0 1 1 1

复习下2进制转10进制:

1 0 1 1 1有5位

1 * 2^4 + 0 * 2^3 + 1*2^2 + 1*2^1 + 1*2^0

= 16 + 0 + 4 + 2 + 1

= 23

一:有效括号(20)

给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
 

示例 1:

输入:s = "()"
输出:true
示例 2:

输入:s = "()[]{}"
输出:true
示例 3:

输入:s = "(]"
输出:false
示例 4:

输入:s = "([)]"
输出:false
示例 5:

输入:s = "{[]}"
输出:true

func isValid(s string) bool {
    /**
    思路分析:
    左右闭合必然是偶数,单数直接返回
    从左遍历,如果是左边的括号,就放入栈中,当匹配右边的第一个括号,必然是和栈顶的是一套的,怎么判断是一套那,我们可以直接使用hash实现,利用map

    **/

    //开始
    n:= len(s)
    if n%2 == 1 {
        return false
    }

    //利用ascii码,注意这里key一定要是有括号,因为下面判断是从最左边开始,所以存在有括号才是需要和栈顶数据对比,写反了下面逻辑也要修改
    var memory = map[byte]byte{
        ')':'(',
        '}':'{',
        ']':'[',
    }

    stack := []byte{}
    for i:=0; i<n; i++ {
        //>0说明存在右括号,这时从栈顶取出做括号
        if memory[s[i]] > 0{
            if len(stack) == 0 || stack[len(stack)-1] != memory[s[i]] {
                return false
            }

            //取出栈顶的数据
            stack = stack[:len(stack) - 1]
        } else {
            //是做括号放到栈顶
            stack = append(stack, s[i])
        }
    }

    //栈没有数据说明匹配完毕
    if len(stack) == 0 {
        return true
    } else {
        return false
    }
}

二:用栈实现队列(232)

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):

实现 MyQueue 类:

void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
 

说明:

你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
 

进阶:

你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。
 

示例:

输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]

解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

type MyQueue struct {
    queue1, queue2 []int
}


func Constructor() (this MyQueue) {
    return
}


func (this *MyQueue) Push(x int)  {
    //栈先进后出,队列先进先出
    //写入前先把栈的数据放到临时中,然后写入到新的栈,如果检测到临时的栈有数据,就把数据放到当前栈中,比如按照栈输入的是2,3,4,5.栈输出是5,4,3,2.所以再把这个按照顺序保存到另一个栈就是,5,4,3,2输出就变成了2,3,4,5。因为栈是先输出最后一个也就是 queue(len(queue)-1)

    //原本234现在2345
    this.queue2 = append(this.queue2, x)
    //开始保存到queue1,每次插入清空queue1,重新同步queue2
    this.queue1 = []int{}
    for i:=0; i< len(this.queue2); i++ {
        this.queue1 = append(this.queue1, this.queue2[len(this.queue2) - i - 1])
    }
}


func (this *MyQueue) Pop() int {
    v := this.queue1[len(this.queue1) - 1]
    //1
    //这里一定要注意。。因为1和2是反转的,所以1移出最后一个,2就应该移出第一个
    //1:1234,2:4321
    this.queue1 = this.queue1[:len(this.queue1) - 1]
    this.queue2 = this.queue2[1:]

    return v
}


func (this *MyQueue) Peek() int {
    //栈只能从栈顶出数据,但是这个是队列的第一个出的
    return this.queue1[len(this.queue1) - 1]
}


func (this *MyQueue) Empty() bool {
    return len(this.queue1) == 0
}


/**
 * Your MyQueue object will be instantiated and called as such:
 * obj := Constructor();
 * obj.Push(x);
 * param_2 := obj.Pop();
 * param_3 := obj.Peek();
 * param_4 := obj.Empty();
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值