码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 牛客网刷题笔记231112 最小k位数+二叉树层序遍历+SQL异常邮件概率


    算法题牛客网NC119 最小的k个数

    题目:

    在这里插入图片描述

    用了一下python列表的便利,不知道在面试时允许用不。当然最简单的方法其实是直接sort()一下取前k位数即可。本次写的思路如下:

    用一个最大容量为k的列表存储结果,遍历n个元素,当列表超过k位时,弹出最大值。

    代码:

    class Solution:
        def GetLeastNumbers_Solution(self , input: List[int], k: int) -> List[int]:
            # write code here
            if k == 0:
                return []
            if k >= len(input):
                return input
            ## 直接sort()
            # input.sort()
            # return input[:k]
            
            ## 堆排序
            max_output = input[0]
            output = [max_output]
            for i in input[1:]:
                if len(output) < k:
                    output.append(i)
                    if i > max_output:
                        max_output = i
                else:
                    if i < max_output:
                        output.remove(max_output)
                        output.append(i)
                        max_output = max(output)
            return output
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    算法题牛客网NC15 二叉树层序遍历

    题目:

    在这里插入图片描述

    这个题目其实还挺普遍的,简单点说就是要做一次遍历,最直接的想法是用队列的思想。不过自己比较习惯用列表,所以是用列表实现队列的想法。
    在这里插入图片描述
    代码:

    class Solution:
        def levelOrder(self , root: TreeNode) -> List[List[int]]:
            # write code here
            if not root:
                return []
            if not root.left and not root.right:
                return [[root.val]]
            
            nodeList = [root] # 作为队列记录节点
            result = []
            while len(nodeList)>0:
                curRow = [] # 记录当前遍历行的结点
                n = len(nodeList)
                for i in range(n):
                    cur = nodeList.pop(0) ## 弹出节点
                    curRow.append(cur.val)
                    ## 加入子节点
                    if cur.left:
                        nodeList.append(cur.left)
                    if cur.right:
                        nodeList.append(cur.right)
                result.append(curRow)
            return result
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    SQL牛客网259 异常的邮件概率

    依旧写了两个,题库太少了没什么难度。题目太长放链接

    异常的邮件概率

    代码:

    select date, format(count(case when type = 'no_completed' then 1 else null end)/count(1), 3) as p
    from (
        select *
        from email
        where send_id in (select id from user where is_blacklist = 0) 
        and receive_id in (select id from user where is_blacklist = 0)
    ) a
    group by date
    order by date asc
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    vue3父组件调用子组件的方法
    Java SqlSessionFactoryBean.setMapperLocations方法代码示例
    SharePoint Password Change and Expiration
    MyBatis-XML映射文件
    postgresql|数据库|恢复备份的时候报错:pg_restore: implied data-only restore的处理方案
    新势力 | 赛宁工控防火墙 筑牢关基设施防护围墙
    PHP 脚本,其中包含一个函数,该函数会从给定的字符串列表中随机选择一个字符串并返回
    小白学习Java第四十天
    [springboot专栏]RedisLockRegistry实现分布式锁
    Jquery版轮播图超详细
  • 原文地址:https://blog.csdn.net/qq_39051660/article/details/134362725
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号