求大佬解惑,腾讯笔试

菜鸡一枚
第1题,实现一个队列的操作,用python写的,实在想不明白哪儿错了

def myQueue():
    T = input()
    T = int(T)
    res=[]
    myqueue = []
    for i in range(T):
        Q = int(input())
        for j in range(Q):
            line = input()
            line = line.split()
            if line[0]=='PUSH':
                myqueue.insert(0,int(line[1]))
            if line[0]=='TOP':
                if not myqueue:
                    #print(-1)
                    res.append(-1)
                else:
                    res.append(myqueue[-1])
            if line[0]=='SIZE':
                res.append(len(myqueue))
            if line[0]=='CLEAR':
                myqueue = []
            if line[0]=='POP':
                if not myqueue:
                    res.append(-1)
                else:
                    myqueue.pop()
    for result in res:
        print(result)


第四题 两个栈实现队列,同样想不明白
def myQueue2():
    stack1 = []
    stack2 = []
    res=[]
    n = int(input())
    for i in range(n):
        line = input()
        line = line.split()
        if line[0]=='add':
            stack1.append(int(line[1]))
        if line[0]=='poll':
            while stack1:
                tmp = stack1.pop()
                stack2.append(tmp)
            stack2.pop()
        if line[0]=='peek':
            while stack1:
                tmp = stack1.pop()
                stack2.append(tmp)
            res.append(stack2[-1])
    print(stack2,stack1)
    for k in res:
        print(k)


第五题,寻找k层祖父节点 哎~为啥都是case 0 啊~

def findAn():
    n = input()
    n = int(n)
    res = []
    def level(k):
        index = 0
        while k != 0:
            k = k//2
            index += 1
        return index
    for i in range(n):
        line = input()
        line = line.split()
        cur_level = level(int(line[0]))
        count = cur_level - int(line[1])
        number = int(line[0])
        if cur_level==int(line[1]):
            res.append(-1)
        else:
            while count > 0:
                count -= 1
                number = number//2
            res.append(number)
    for k in res:
        print(k)



#腾讯笔试##笔试题目##笔经#
全部评论
第五题,编号范围10^8,用int会越界的
点赞 回复 分享
发布于 2020-04-26 22:58

相关推荐

叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
04-29 22:35
门头沟学院 Java
牛友说改了名字能收到offer:旧图新发查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务