讨论社区

测试用例执行结果和本地调试结果不一致

题目:https://leetcode-cn.com/problems/design-skiplist/submissions/


输入:["Skiplist","add","add","add","add","search","erase","search","search","search"]
[[],[0],[5],[2],[1],[0],[5],[2],[3],[2]]

输出

[null,null,null,null,null,true,true,true,true,true]

预期结果

[null,null,null,null,null,true,true,true,false,true]


本地运算结果符合预期,和在线提交的输出结果不一致


var (

    level int = 3

)


// Skiplist ..

type Skiplist struct {

    val  int

    next []*Skiplist

}


var l []*Skiplist = make([]*Skiplist, level)


// Constructor ...

func Constructor() Skiplist {

    return Skiplist{

        next: make([]*Skiplist, level),

    }

}

// Search ...

func (this *Skiplist) Search(target int) bool {

    i := level - 1

    for {

        currentDot := l[i]

        if currentDot == nil {

            i--

            continue

        }

        nextDot := currentDot.next[i]

        for { // 同层级循环遍历

            // 找到

            if currentDot.val == target {

                return true

            }

            if currentDot.next[i] == nil {

                i--

                goto goon

            }

            if currentDot.val < target {

                // 后移

                currentDot = nextDot

                nextDot = nextDot.next[i]

            } else {

                goto goon

            }

        }

    goon:

        i--

        if i < 0 {

            break

        }

    }

    return false

}


// Add ...

func (this *Skiplist) Add(num int) {

    randomLevel := num % level

    v := &Skiplist{

        val:  num,

        next: make([]*Skiplist, randomLevel+1),

    }

    for i := randomLevel; i >= 0; i-- {

        dot := l[i]

        var pre *Skiplist

        for {

            if dot == nil && pre == nil {

                l[i] = v

                dot = l[i]

                goto goon

            }

            if dot != nil && dot.val >= num {

                v.next[i] = dot

                if pre == nil {

                    l[i] = v

                    goto goon

                }

                pre.next[i] = v

                goto goon

            } else if dot == nil {

                pre.next[i] = v

                goto goon

            } else {

                pre = dot

                dot = dot.next[i]

            }

        }

    goon:

    }

}


// Erase ...

func (this *Skiplist) Erase(target int) bool {

    i := level - 1

    for {

        currentDot := l[i]

        if currentDot == nil {

            i--

            continue

        }

        nextDot := currentDot.next[i]

        var preDot *Skiplist

        for { 

            if currentDot.val == target {

                if preDot == nil {

                    l[i] = currentDot.next[i]

                } else {

                    preDot.next[i] = currentDot.next[i]

                }

                return true

            }

            if currentDot.next[i] == nil {

                i--

                goto goon

            }

            if currentDot.val < target {

                preDot = currentDot

                currentDot = nextDot

                nextDot = nextDot.next[i]

            } else {

                goto goon

            }

        }

    goon:

        i--

        if i < 0 {

            break

        }

    }

    return false

}


0 人关注了该问题 关注

0

力扣 (LeetCode) • 1周前

您好,推荐阅读 这篇文章,后续刷题学习中遇到困难,可至 力扣圈子 进行题目交流,以获得更好的支持哟~

0 个讨论

您需要登录后才可回复
您需要登录后才可以回复