9.4 B站笔试编程题

第一题

    def GetMaxConsecutiveOnes(self, arr, k):
        # write code here
        zero = 0
        left = 0
        res = 0
        for right in range(len(arr)):
            if arr[right] == 0:
                zero += 1
            while zero > k:
                if arr[left] == 0:
                    zero -= 1
                left += 1
            res = max(res, right - left + 1)
        return res

第二题

    def SpiralMatrix(self, matrix):
        # write code here
        direction = [[0, 1], [1, 0], [0, -1], [-1, 0]]
        res = []
        x, y = 0, 0
        direct = 0
        for _ in range(len(matrix) * len(matrix[0]) - 1):
            res.append(matrix[x][y])
            matrix[x][y] = 'a'
            while not (0 <= x + direction[direct][0] < len(matrix) and
                       len(matrix[0]) > y + direction[direct][1] >= 0 and
                       matrix[x + direction[direct][0]][y + direction[direct][1]] != 'a'):
                direct = (direct + 1) % 4
            x += direction[direct][0]
            y += direction[direct][1]
        res.append(matrix[x][y])
        return res

第三题

    def GetFragment(self, str):
        slide = 1
        for i in range(1, len(str)):
            if str[i] != str[i - 1]:
                slide += 1

        return len(str) // slide


#笔试题目##哔哩哔哩#
全部评论

相关推荐

牛客20485985...:抱抱😘,首先你还有春招,然后就算这时候没上岸也没关系,大部分人都是这样,毕业了再找也成,最后工作只是生活的一小部分,找到工作也不是一个必须的事情。不要气馁不要焦虑你只是陷入了短暂的低谷,你也一直有退路
点赞 评论 收藏
分享
03-23 22:04
江南大学 Java
程序员小白条:28届原因,这才研一,而且项目比较经典,东西也写的很简单,自我评价没啥用,应该写的是技术栈
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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