·解题思路来源https://github.com/azl3979858...

package main

import "container/list"
import "fmt"

func main(){
    s := "{{}}"
    m := "{{[()}]}"
    l := "{[(())]}"

    fmt.Println(check(s))
    fmt.Println(check(m))
    fmt.Println(check(l))
}

func check(x string) bool {

    mapper := map[byte]byte{
        '{':'}',
        '(':')',
        '[':']',
    }

    stack := list.New() //初始化栈

    for _, v := range x {
        i := byte(v)
        if _, ok := mapper[i]; ok {
            //入栈
            stack.PushFront(i)
            fmt.Println(i)
        } else {
            if stack.Len() == 0 {
                return false
            }
            l := stack.Remove(stack.Front()).(byte)
            r, ok := mapper[l]
            if !ok || r != i {
                return false
            }
            fmt.Printf("%s:%s", "S", string(i))
            fmt.Printf("%s:%s", "L", string(l))
            fmt.Printf("%s:%s", "R", string(r))
            fmt.Println()
        }
    }
    if stack.Len() > 0 {
        return false
    }
    return true
}

氓浅
8 声望2 粉丝

« 上一篇
go并发编程