牛客网刷题笔记231112 最小k位数+二叉树层序遍历+SQL异常邮件概率

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

题目:

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

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

代码:

python 复制代码
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

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

题目:

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

代码:

python 复制代码
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

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

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

异常的邮件概率

代码:

sql 复制代码
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
相关推荐
Cyanto3 小时前
深入MyBatis:CRUD操作与高级查询实战
java·数据库·mybatis
茫忙然3 小时前
【WEB】Polar靶场 Day7 详细笔记
笔记
datascome3 小时前
文章发布易优CMS(Eyoucms)网站技巧
数据库·经验分享·爬虫·数据采集·eyoucms·易优cms
天上掉下来个程小白3 小时前
MybatisPlus-06.核心功能-自定义SQL
java·spring boot·后端·sql·微服务·mybatisplus
有想法的py工程师4 小时前
PostgreSQL 锁等待监控,查找等待中的锁
数据库
学不会就看4 小时前
Django--02模型和管理站点
数据库·oracle·django
今天背单词了吗9804 小时前
算法学习笔记:17.蒙特卡洛算法 ——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·笔记·考研·算法·蒙特卡洛算法
逐花归海.5 小时前
『 C++ 入门到放弃 』- 多态
开发语言·c++·笔记·程序人生
←か淡定☆ ヾ5 小时前
SQL Server 2008R2 到 2012 数据库迁移完整指南
数据库·sql server
瀚高PG实验室5 小时前
Arcgis连接HGDB报错
数据库·arcgis·瀚高数据库