├── .gitignore ├── LICENSE ├── README.md ├── README_en.md ├── source ├── All_Solutions │ ├── 0001.两数之和 │ │ └── 0001-两数之和.py │ ├── 0002.两数相加 │ │ └── 0002-两数相加.py │ ├── 0003.无重复字符的最长子串 │ │ └── 0003-无重复字符的最长子串.py │ ├── 0004.寻找两个有序数组的中位数 │ │ └── 0004-寻找两个有序数组的中位数.py │ ├── 0005.最长回文子串 │ │ └── 0005-最长回文子串.py │ ├── 0007.整数反转 │ │ ├── 0007-整数反转.cpp │ │ └── 0007-整数反转.py │ ├── 0008.字符串转换整数(atoi) │ │ └── 0008-字符串转换整数(atoi).py │ ├── 0010.正则表达式匹配 │ │ └── 0010-正则表达式匹配.py │ ├── 0011.盛最多水的容器 │ │ └── 0011-盛最多水的容器.cpp │ ├── 0012.整数转罗马数字 │ │ └── 0012-整数转罗马数字.py │ ├── 0013.罗马数字转整数 │ │ └── 0013-罗马数字转整数.py │ ├── 0042.接雨水 │ │ └── 0042-接雨水.cpp │ ├── 0055.跳跃游戏 │ │ └── 0055-跳跃游戏.cpp │ ├── 0131.分割回文串 │ │ └── 0131-分割回文串.py │ ├── 0146.LRU缓存机制 │ │ └── 0146-LRU缓存机制.py │ ├── 0170.两数之和III-数据结构设计 │ │ └── 0170-两数之和III-数据结构设计.py │ ├── 0176.第二高的薪水 │ │ └── 0176-第二高的薪水.sql │ ├── 0177.第N高的薪水 │ │ └── 0177-第N高的薪水.sql │ ├── 0180.连续出现的数字 │ │ └── 0180-连续出现的数字.sql │ ├── 0181.超过经理收入的员工 │ │ └── 0181-超过经理收入的员工.sql │ ├── 0182.查找重复的电子邮箱 │ │ └── 0182-查找重复的电子邮箱.sql │ ├── 0183.从不订购的客户 │ │ └── 0183-从不订购的客户.sql │ ├── 0184.部门工资最高的员工 │ │ └── 0184-部门工资最高的员工.sql │ ├── 0196.删除重复的电子邮箱 │ │ └── 0196-删除重复的电子邮箱.sql │ ├── 0197.上升的温度 │ │ └── 0197-上升的温度.sql │ ├── 0223.矩形面积 │ │ └── 0223-矩形面积.py │ ├── 0225.用队列实现栈 │ │ └── 0225-用队列实现栈.py │ ├── 0240.搜索二维矩阵II │ │ └── 0240-搜索二维矩阵II.py │ ├── 0262.行程和用户 │ │ └── 0262-行程和用户.sql │ ├── 0304.二维区域和检索-矩阵不可变 │ │ └── 0304-二维区域和检索-矩阵不可变.py │ ├── 0307.区域和检索-数组可修改 │ │ └── 0307-区域和检索-数组可修改.py │ ├── 0308.二维区域和检索-可变 │ │ └── 0308-二维区域和检索-可变.py │ ├── 0348.判定井字棋胜负 │ │ └── 0348-判定井字棋胜负.py │ ├── 0353.贪吃蛇 │ │ └── 0353-贪吃蛇.py │ ├── 0355.设计推特 │ │ └── 0355-设计推特.py │ ├── 0370.区间加法 │ │ └── 0370-区间加法.py │ ├── 0379.电话目录管理系统 │ │ └── 0379-电话目录管理系统.py │ ├── 0406.根据身高重建队列 │ │ └── 0406-根据身高重建队列.py │ ├── 0416.分割等和子集 │ │ └── 0416-分割等和子集.py │ ├── 0425.单词方块 │ │ └── 0425-单词方块.py │ ├── 0426.将二叉搜索树转化为排序的双向链表 │ │ └── 0426-将二叉搜索树转化为排序的双向链表.py │ ├── 0428.序列化和反序列化N叉树 │ │ └── 0428-序列化和反序列化N叉树.py │ ├── 0431.将N叉树编码为二叉树 │ │ └── 0431-将N叉树编码为二叉树.py │ ├── 0437.路径总和III │ │ └── 0437-路径总和III.py │ ├── 0438.找到字符串中所有字母异位词 │ │ └── 0438-找到字符串中所有字母异位词.py │ ├── 0439.三元表达式解析器 │ │ └── 0439-三元表达式解析器.py │ ├── 0465.最优账单平衡 │ │ └── 0465-最优账单平衡.py │ ├── 0466.统计重复个数 │ │ └── 0466-统计重复个数.py │ ├── 0467.环绕字符串中唯一的子字符串 │ │ └── 0467-环绕字符串中唯一的子字符串.py │ ├── 0468.验证IP地址 │ │ └── 0468-验证IP地址.py │ ├── 0469.凸多边形 │ │ └── 0469-凸多边形.py │ ├── 0470.用Rand7()实现Rand10() │ │ └── 0470-用Rand7()实现Rand10().py │ ├── 0471.编码最短长度的字符串 │ │ └── 0471-编码最短长度的字符串.py │ ├── 0472.连接词 │ │ └── 0472-连接词.py │ ├── 0473.火柴拼正方形 │ │ └── 0473-火柴拼正方形.py │ ├── 0474.一和零 │ │ └── 0474-一和零.py │ ├── 0475.供暖器 │ │ └── 0475-供暖器.py │ ├── 0477.汉明距离总和 │ │ └── 0477-汉明距离总和.py │ ├── 0478.在圆内随机生成点 │ │ └── 0478-在圆内随机生成点.py │ ├── 0479.最大回文数乘积 │ │ └── 0479-最大回文数乘积.py │ ├── 0480.滑动窗口中位数 │ │ └── 0480-滑动窗口中位数.py │ ├── 0481.神奇字符串 │ │ └── 0481-神奇字符串.py │ ├── 0482.密钥格式化 │ │ └── 0482-密钥格式化.py │ ├── 0483.最小好进制 │ │ └── 0483-最小好进制.py │ ├── 0484.寻找排列 │ │ └── 0484-寻找排列.py │ ├── 0486.预测赢家 │ │ └── 0486-预测赢家.py │ ├── 0487.最大连续1的个数II │ │ └── 0487-最大连续1的个数II.java │ ├── 0488.祖玛游戏 │ │ └── 0488-祖玛游戏.py │ ├── 0489.扫地机器人 │ │ └── 0489-扫地机器人.py │ ├── 0490.迷宫 │ │ └── 0490-迷宫.py │ ├── 0491.递增子序列 │ │ └── 0491-递增子序列.py │ ├── 0492.构造矩形 │ │ └── 0492-构造矩形.py │ ├── 0493.翻转对 │ │ └── 0493-翻转对.py │ ├── 0494.目标和 │ │ └── 0494-目标和.py │ ├── 0495.提莫攻击 │ │ └── 0495-提莫攻击.py │ ├── 0496.下一个更大元素I │ │ └── 0496-下一个更大元素I.py │ ├── 0497.非重叠矩形中的随机点 │ │ └── 0497-非重叠矩形中的随机点.py │ ├── 0499.迷宫III │ │ └── 0499-迷宫III.py │ ├── 0501.二叉搜索树中的众数 │ │ └── 0501-二叉搜索树中的众数.py │ ├── 0502.IPO │ │ └── 0502-IPO.py │ ├── 0503.下一个更大元素II │ │ └── 0503-下一个更大元素II.py │ ├── 0505.迷宫II │ │ └── 0505-迷宫II.py │ ├── 0508.出现次数最多的子树元素和 │ │ └── 0508-出现次数最多的子树元素和.py │ ├── 0510.二叉搜索树中的中序后继II │ │ └── 0510-二叉搜索树中的中序后继II.py │ ├── 0513.找树左下角的值 │ │ └── 0513-找树左下角的值.py │ ├── 0514.自由之路 │ │ └── 0514-自由之路.py │ ├── 0538.把二叉搜索树转换为累加树 │ │ └── 0538-把二叉搜索树转换为累加树.py │ ├── 0543.二叉树的直径 │ │ └── 0543-二叉树的直径.py │ ├── 0560.和为K的子数组 │ │ └── 0560-和为K的子数组.py │ ├── 0567.字符串的排列 │ │ └── 0567-字符串的排列.py │ ├── 0569.员工薪水中位数 │ │ └── 0569-员工薪水中位数.sql │ ├── 0570.至少有5名直接下属的经理 │ │ └── 0570-至少有5名直接下属的经理.sql │ ├── 0571.给定数字的频率查询中位数 │ │ └── 0571-给定数字的频率查询中位数.sql │ ├── 0574.当选者 │ │ └── 0574-当选者.sql │ ├── 0577.员工奖金 │ │ └── 0577-员工奖金.sql │ ├── 0578.查询回答率最高的问题 │ │ └── 0578-查询回答率最高的问题.sql │ ├── 0579.查询员工的累计薪水 │ │ └── 0579-查询员工的累计薪水.sql │ ├── 0580.统计各专业学生人数 │ │ └── 0580-统计各专业学生人数.sql │ ├── 0584.寻找用户推荐人 │ │ └── 0584-寻找用户推荐人.sql │ ├── 0585.2016年的投资 │ │ └── 0585-2016年的投资.sql │ ├── 0586.订单最多的客户 │ │ └── 0586-订单最多的客户.sql │ ├── 0587.安装栅栏 │ │ └── 0587-安装栅栏.py │ ├── 0588.设计内存文件系统 │ │ └── 0588-设计内存文件系统.py │ ├── 0591.标签验证器 │ │ └── 0591-标签验证器.py │ ├── 0592.分数加减运算 │ │ └── 0592-分数加减运算.py │ ├── 0593.有效的正方形 │ │ └── 0593-有效的正方形.py │ ├── 0595.大的国家 │ │ └── 0595-大的国家.sql │ ├── 0596.超过5名学生的课 │ │ └── 0596-超过5名学生的课.sql │ ├── 0597.好友申请I:总体通过率 │ │ └── 0597-好友申请I:总体通过率.sql │ ├── 0598.范围求和II │ │ └── 0598-范围求和II.py │ ├── 0600.不含连续1的非负整数 │ │ └── 0600-不含连续1的非负整数.py │ ├── 0601.体育馆的人流量 │ │ └── 0601-体育馆的人流量.sql │ ├── 0602.好友申请II:谁有最多的好友 │ │ └── 0602-好友申请II:谁有最多的好友.sql │ ├── 0603.连续空余座位 │ │ └── 0603-连续空余座位.sql │ ├── 0604.迭代压缩字符串 │ │ └── 0604-迭代压缩字符串.py │ ├── 0606.根据二叉树创建字符串 │ │ └── 0606-根据二叉树创建字符串.py │ ├── 0607.销售员 │ │ └── 0607-销售员.sql │ ├── 0608.树节点 │ │ └── 0608-树节点.sql │ ├── 0609.在系统中查找重复文件 │ │ └── 0609-在系统中查找重复文件.py │ ├── 0610.判断三角形 │ │ └── 0610-判断三角形.sql │ ├── 0611.有效三角形的个数 │ │ └── 0611-有效三角形的个数.py │ ├── 0612.平面上的最近距离 │ │ └── 0612-平面上的最近距离.sql │ ├── 0613.直线上的最近距离 │ │ └── 0613-直线上的最近距离.sql │ ├── 0616.给字符串添加加粗标签 │ │ └── 0616-给字符串添加加粗标签.py │ ├── 0617.合并二叉树 │ │ └── 0617-合并二叉树.py │ ├── 0623.在二叉树中增加一行 │ │ └── 0623-在二叉树中增加一行.py │ ├── 0625.最小因式分解 │ │ └── 0625-最小因式分解.py │ ├── 0627.交换工资 │ │ └── 0627-交换工资.sql │ ├── 0629.K个逆序对数组 │ │ └── 0629-K个逆序对数组.py │ ├── 0630.课程表III │ │ └── 0630-课程表III.py │ ├── 0631.设计Excel求和公式 │ │ └── 0631-设计Excel求和公式.py │ ├── 0632.最小区间 │ │ └── 0632-最小区间.java │ ├── 0634.寻找数组的错位排列 │ │ └── 0634-寻找数组的错位排列.java │ ├── 0635.设计日志存储系统 │ │ └── 0635-设计日志存储系统.java │ ├── 0636.函数的独占时间 │ │ └── 0636-函数的独占时间.py │ ├── 0637.二叉树的层平均值 │ │ └── 0637-二叉树的层平均值.py │ ├── 0638.大礼包 │ │ └── 0638-大礼包.py │ ├── 0639.解码方法2 │ │ └── 0639-解码方法2.py │ ├── 0640.求解方程 │ │ └── 0640-求解方程.py │ ├── 0641.设计循环双端队列 │ │ └── 0641-设计循环双端队列.py │ ├── 0642.设计搜索自动补全系统 │ │ └── 0642-设计搜索自动补全系统.py │ ├── 0644.最大平均子段和II │ │ └── 0644-最大平均子段和II.java │ ├── 0645.错误的集合 │ │ └── 0645-错误的集合.py │ ├── 0646.最长数对链 │ │ └── 0646-最长数对链.py │ ├── 0647.回文子串 │ │ └── 0647-回文子串.py │ ├── 0648.单词替换 │ │ └── 0648-单词替换.py │ ├── 0649.Dota2参议院 │ │ └── 0649-Dota2参议院.py │ ├── 0650.只有两个键的键盘 │ │ └── 0650-只有两个键的键盘.py │ ├── 0651.4键键盘 │ │ └── 0651-4键键盘.py │ ├── 0653.两数之和IV-输入BST │ │ └── 0653-两数之和IV-输入BST.py │ ├── 0654.最大二叉树 │ │ └── 0654-最大二叉树.py │ ├── 0655.输出二叉树 │ │ └── 0655-输出二叉树.py │ ├── 0656.金币路径 │ │ └── 0656-金币路径.py │ ├── 0657.机器人能否返回原点 │ │ └── 0657-机器人能否返回原点.py │ ├── 0659.分割数组为连续子序列 │ │ └── 0659-分割数组为连续子序列.py │ ├── 0660.移除9 │ │ └── 0660-移除9.py │ ├── 0682.棒球比赛 │ │ └── 0682-棒球比赛.py │ ├── 0683.K个空花盆 │ │ └── 0683-K个空花盆.py │ ├── 0684.冗余连接 │ │ └── 0684-冗余连接.py │ ├── 0685.冗余连接II │ │ └── 0685-冗余连接II.py │ ├── 0688.“马”在棋盘上的概率 │ │ └── 0688-“马”在棋盘上的概率.py │ ├── 0689.三个无重叠子数组的最大和 │ │ └── 0689-三个无重叠子数组的最大和.py │ ├── 0690.员工的重要性 │ │ └── 0690-员工的重要性.py │ ├── 0691.贴纸拼词 │ │ └── 0691-贴纸拼词.py │ ├── 0692.前K个高频单词 │ │ └── 0692-前K个高频单词.py │ ├── 0693.交替位二进制数 │ │ └── 0693-交替位二进制数.py │ ├── 0694.不同岛屿的数量 │ │ └── 0694-不同岛屿的数量.py │ ├── 0695.岛屿的最大面积 │ │ └── 0695-岛屿的最大面积.py │ ├── 0696.计数二进制子串 │ │ └── 0696-计数二进制子串.py │ ├── 0698.划分为k个相等的子集 │ │ └── 0698-划分为k个相等的子集.py │ ├── 0699.掉落的方块 │ │ └── 0699-掉落的方块.py │ ├── 0702.搜索长度未知的有序数组 │ │ └── 0702-搜索长度未知的有序数组.py │ ├── 0708.循环有序列表的插入 │ │ └── 0708-循环有序列表的插入.py │ ├── 0710.黑名单中的随机数 │ │ └── 0710-黑名单中的随机数.py │ ├── 0711.不同岛屿的数量II │ │ └── 0711-不同岛屿的数量II.py │ ├── 0712.两个字符串的最小ASCII删除和 │ │ └── 0712-两个字符串的最小ASCII删除和.py │ ├── 0713.乘积小于K的子数组 │ │ └── 0713-乘积小于K的子数组.py │ ├── 0714.买卖股票的最佳时机含手续费 │ │ └── 0714-买卖股票的最佳时机含手续费.py │ ├── 0715.Range模块 │ │ └── 0715-Range模块.py │ ├── 0716.最大栈 │ │ └── 0716-最大栈.py │ ├── 0717.1比特与2比特字符 │ │ └── 0717-1比特与2比特字符.py │ ├── 0718.最长重复子数组 │ │ └── 0718-最长重复子数组.py │ ├── 0720.词典中最长的单词 │ │ └── 0720-词典中最长的单词.py │ ├── 0721.账户合并 │ │ └── 0721-账户合并.py │ ├── 0722.删除注释 │ │ └── 0722-删除注释.py │ ├── 0723.粉碎糖果 │ │ └── 0723-粉碎糖果.py │ ├── 0725.分隔链表 │ │ └── 0725-分隔链表.py │ ├── 0765.情侣牵手 │ │ └── 0765-情侣牵手.py │ ├── 0766.托普利茨矩阵 │ │ └── 0766-托普利茨矩阵.py │ ├── 0767.重构字符串 │ │ └── 0767-重构字符串.py │ ├── 0768.最多能完成排序的块II │ │ └── 0768-最多能完成排序的块II.py │ ├── 0769.最多能完成排序的块 │ │ └── 0769-最多能完成排序的块.py │ ├── 0770.基本计算器IV │ │ └── 0770-基本计算器IV.py │ ├── 0772.基本计算器III │ │ └── 0772-基本计算器III.py │ ├── 0773.滑动谜题 │ │ └── 0773-滑动谜题.py │ ├── 0775.全局倒置与局部倒置 │ │ └── 0775-全局倒置与局部倒置.py │ ├── 0776.拆分二叉搜索树 │ │ └── 0776-拆分二叉搜索树.py │ ├── 0777.在LR字符串中交换相邻字符 │ │ └── 0777-在LR字符串中交换相邻字符.py │ ├── 0778.水位上升的泳池中游泳 │ │ └── 0778-水位上升的泳池中游泳.py │ ├── 0780.到达终点 │ │ └── 0780-到达终点.py │ ├── 0781.森林中的兔子 │ │ └── 0781-森林中的兔子.py │ ├── 0782.变为棋盘 │ │ └── 0782-变为棋盘.py │ ├── 0783.二叉搜索树结点最小距离 │ │ └── 0783-二叉搜索树结点最小距离.py │ ├── 0784.字母大小写全排列 │ │ └── 0784-字母大小写全排列.py │ ├── 0785.判断二分图 │ │ └── 0785-判断二分图.py │ ├── 0786.第K个最小的素数分数 │ │ └── 0786-第K个最小的素数分数.py │ ├── 0787.K站中转内最便宜的航班 │ │ └── 0787-K站中转内最便宜的航班.py │ ├── 0788.旋转数字 │ │ └── 0788-旋转数字.py │ ├── 0789.逃脱阻碍者 │ │ └── 0789-逃脱阻碍者.py │ ├── 0790.多米诺和托米诺平铺 │ │ └── 0790-多米诺和托米诺平铺.py │ ├── 0791.自定义字符串排序 │ │ └── 0791-自定义字符串排序.py │ ├── 0792.匹配子序列的单词数 │ │ └── 0792-匹配子序列的单词数.py │ ├── 0793.阶乘函数后K个零 │ │ └── 0793-阶乘函数后K个零.py │ ├── 0794.有效的井字游戏 │ │ └── 0794-有效的井字游戏.py │ ├── 0795.区间子数组个数 │ │ └── 0795-区间子数组个数.py │ ├── 0796.旋转字符串 │ │ └── 0796-旋转字符串.py │ ├── 0797.所有可能的路径 │ │ └── 0797-所有可能的路径.py │ ├── 0798.得分最高的最小轮调 │ │ └── 0798-得分最高的最小轮调.py │ ├── 0799.香槟塔 │ │ └── 0799-香槟塔.py │ ├── 0800.相似RGB颜色 │ │ └── 0800-相似RGB颜色.py │ ├── 0801.使序列递增的最小交换次数 │ │ └── 0801-使序列递增的最小交换次数.py │ ├── 0802.找到最终的安全状态 │ │ └── 0802-找到最终的安全状态.py │ ├── 0803.打砖块 │ │ └── 0803-打砖块.py │ ├── 0804.唯一摩尔斯密码词 │ │ └── 0804-唯一摩尔斯密码词.py │ ├── 0805.数组的均值分割 │ │ └── 0805-数组的均值分割.py │ ├── 0806.写字符串需要的行数 │ │ └── 0806-写字符串需要的行数.py │ ├── 0807.保持城市天际线 │ │ └── 0807-保持城市天际线.py │ ├── 0808.分汤 │ │ └── 0808-分汤.py │ ├── 0809.情感丰富的文字 │ │ └── 0809-情感丰富的文字.py │ ├── 0810.黑板异或游戏 │ │ └── 0810-黑板异或游戏.py │ ├── 0811.子域名访问计数 │ │ └── 0811-子域名访问计数.py │ ├── 0850.矩形面积II │ │ └── 0850-矩形面积II.py │ ├── 0899.有序队列 │ │ └── 0899-有序队列.py │ ├── 0900.RLE迭代器 │ │ └── 0900-RLE迭代器.py │ ├── 0901.股票价格跨度 │ │ └── 0901-股票价格跨度.py │ ├── 0902.最大为N的数字组合 │ │ └── 0902-最大为N的数字组合.py │ ├── 0903.DI序列的有效排列 │ │ └── 0903-DI序列的有效排列.py │ ├── 0904.水果成篮 │ │ └── 0904-水果成篮.py │ ├── 0906.超级回文数 │ │ └── 0906-超级回文数.py │ ├── 0907.子数组的最小值之和 │ │ └── 0907-子数组的最小值之和.py │ ├── 0908.最小差值I │ │ └── 0908-最小差值I.py │ ├── 0909.蛇梯棋 │ │ └── 0909-蛇梯棋.py │ ├── 0910.最小差值II │ │ └── 0910-最小差值II.py │ ├── 0911.在线选举 │ │ └── 0911-在线选举.py │ ├── 0913.猫和老鼠 │ │ └── 0913-猫和老鼠.py │ ├── 0915.分割数组 │ │ └── 0915-分割数组.py │ ├── 0916.单词子集 │ │ └── 0916-单词子集.py │ ├── 0918.环形子数组的最大和 │ │ └── 0918-环形子数组的最大和.py │ ├── 0954.二倍数对数组 │ │ └── 0954-二倍数对数组.py │ ├── 0955.删列造序II │ │ └── 0955-删列造序II.py │ ├── 0956.最高的广告牌 │ │ └── 0956-最高的广告牌.py │ ├── 0966.元音拼写检查器 │ │ └── 0966-元音拼写检查器.py │ ├── 0967.连续差相同的数字 │ │ └── 0967-连续差相同的数字.py │ ├── 0968.监控二叉树 │ │ └── 0968-监控二叉树.py │ ├── 0969.煎饼排序 │ │ └── 0969-煎饼排序.py │ ├── 0970.强整数 │ │ └── 0970-强整数.py │ ├── 0971.翻转二叉树以匹配先序遍历 │ │ └── 0971-翻转二叉树以匹配先序遍历.py │ ├── 0972.相等的有理数 │ │ └── 0972-相等的有理数.py │ ├── 0973.最接近原点的K个点 │ │ └── 0973-最接近原点的K个点.py │ ├── 0974.和可被K整除的子数组 │ │ └── 0974-和可被K整除的子数组.py │ ├── 0975.奇偶跳 │ │ └── 0975-奇偶跳.py │ ├── 0978.最长湍流子数组 │ │ └── 0978-最长湍流子数组.py │ ├── 0979.在二叉树中分配硬币 │ │ └── 0979-在二叉树中分配硬币.py │ ├── 0980.不同路径III │ │ └── 0980-不同路径III.py │ ├── 0981.基于时间的键值存储 │ │ └── 0981-基于时间的键值存储.py │ ├── 0982.按位与为零的三元组 │ │ └── 0982-按位与为零的三元组.py │ ├── 0983.最低票价 │ │ └── 0983-最低票价.py │ ├── 0984.不含AAA或BBB的字符串 │ │ └── 0984-不含AAA或BBB的字符串.py │ ├── 0985.查询后的偶数和 │ │ └── 0985-查询后的偶数和.py │ ├── 0986.区间列表的交集 │ │ └── 0986-区间列表的交集.py │ ├── 0987.二叉树的垂序遍历 │ │ └── 0987-二叉树的垂序遍历.py │ ├── 0988.从叶结点开始的最小字符串 │ │ └── 0988-从叶结点开始的最小字符串.py │ ├── 0990.等式方程的可满足性 │ │ └── 0990-等式方程的可满足性.py │ ├── 0991.坏了的计算器 │ │ └── 0991-坏了的计算器.py │ ├── 0992.K个不同整数的子数组 │ │ └── 0992-K个不同整数的子数组.py │ ├── 0993.二叉树的堂兄弟节点 │ │ └── 0993-二叉树的堂兄弟节点.py │ ├── 0994.腐烂的橘子 │ │ └── 0994-腐烂的橘子.py │ ├── 0995.K连续位的最小翻转次数 │ │ └── 0995-K连续位的最小翻转次数.py │ ├── 0996.正方形数组的数目 │ │ └── 0996-正方形数组的数目.py │ ├── 0997.找到小镇的法官 │ │ └── 0997-找到小镇的法官.py │ ├── 0998.最大二叉树II │ │ └── 0998-最大二叉树II.py │ ├── 1016.子串能表示从1到N数字的二进制串 │ │ └── 1016-子串能表示从1到N数字的二进制串.py │ ├── 1017.负二进制转换 │ │ └── 1017-负二进制转换.py │ ├── 1019.链表中的下一个更大节点 │ │ └── 1019-链表中的下一个更大节点.py │ ├── 1020.飞地的数量 │ │ └── 1020-飞地的数量.py │ ├── 1022.从根到叶的二进制数之和 │ │ └── 1022-从根到叶的二进制数之和.py │ ├── 1023.驼峰式匹配 │ │ └── 1023-驼峰式匹配.py │ ├── 1024.视频拼接 │ │ └── 1024-视频拼接.py │ ├── 1026.节点与其祖先之间的最大差值 │ │ └── 1026-节点与其祖先之间的最大差值.py │ ├── 1027.最长等差数列 │ │ └── 1027-最长等差数列.py │ ├── 1028.从先序遍历还原二叉树 │ │ └── 1028-从先序遍历还原二叉树.py │ ├── 1029.两地调度 │ │ └── 1029-两地调度.py │ ├── 1030.距离顺序排列矩阵单元格 │ │ └── 1030-距离顺序排列矩阵单元格.py │ ├── 1031.两个非重叠子数组的最大和 │ │ └── 1031-两个非重叠子数组的最大和.py │ ├── 1032.字符流 │ │ └── 1032-字符流.py │ ├── 1034.边框着色 │ │ └── 1034-边框着色.py │ ├── 1035.不相交的线 │ │ └── 1035-不相交的线.py │ ├── 1036.逃离大迷宫 │ │ └── 1036-逃离大迷宫.py │ ├── 1037.有效的回旋镖 │ │ └── 1037-有效的回旋镖.py │ ├── 1038.从二叉搜索树到更大和树 │ │ └── 1038-从二叉搜索树到更大和树.py │ ├── 1039.多边形三角剖分的最低得分 │ │ └── 1039-多边形三角剖分的最低得分.py │ ├── 1040.移动石子直到连续II │ │ └── 1040-移动石子直到连续II.py │ ├── 1041.困于环中的机器人 │ │ └── 1041-困于环中的机器人.py │ ├── 1042.不邻接植花 │ │ └── 1042-不邻接植花.py │ ├── 1043.分隔数组以得到最大和 │ │ └── 1043-分隔数组以得到最大和.py │ ├── 1046.最后一块石头的重量 │ │ └── 1046-最后一块石头的重量.py │ ├── 1047.删除字符串中的所有相邻重复项 │ │ └── 1047-删除字符串中的所有相邻重复项.py │ ├── 1048.最长字符串链 │ │ └── 1048-最长字符串链.py │ ├── 1049.最后一块石头的重量II │ │ └── 1049-最后一块石头的重量II.py │ ├── 1051.高度检查器 │ │ └── 1051-高度检查器.py │ ├── 1052.爱生气的书店老板 │ │ └── 1052-爱生气的书店老板.py │ ├── 1053.交换一次的先前排列 │ │ └── 1053-交换一次的先前排列.py │ ├── 1054.距离相等的条形码 │ │ └── 1054-距离相等的条形码.py │ ├── 1055.形成字符串的最短路径 │ │ └── 1055-形成字符串的最短路径.py │ ├── 1056.易混淆数 │ │ └── 1056-易混淆数.py │ ├── 1057.校园自行车分配 │ │ └── 1057-校园自行车分配.py │ ├── 1058.最小化舍入误差以满足目标 │ │ └── 1058-最小化舍入误差以满足目标.py │ ├── 1059.从始点到终点的所有路径 │ │ └── 1059-从始点到终点的所有路径.py │ ├── 1060.有序数组中的缺失元素 │ │ └── 1060-有序数组中的缺失元素.py │ ├── 1061.按字典序排列最小的等效字符串 │ │ └── 1061-按字典序排列最小的等效字符串.py │ ├── 1062.最长重复子串 │ │ └── 1062-最长重复子串.py │ ├── 1110.删点成林 │ │ └── 1110-删点成林.py │ ├── 1111.有效括号的嵌套深度 │ │ └── 1111-有效括号的嵌套深度.py │ ├── 1114.按序打印 │ │ └── 1114-按序打印.py │ ├── 1115.交替打印FooBar │ │ └── 1115-交替打印FooBar.py │ ├── 1116.打印零与奇偶数 │ │ └── 1116-打印零与奇偶数.py │ ├── 1117.H2O生成 │ │ └── 1117-H2O生成.py │ ├── 1120.子树的最大平均值 │ │ └── 1120-子树的最大平均值.py │ ├── 1121.将数组分成几个递增序列 │ │ └── 1121-将数组分成几个递增序列.py │ ├── 1123.最深叶节点的最近公共祖先 │ │ └── 1123-最深叶节点的最近公共祖先.py │ ├── 1124.表现良好的最长时间段 │ │ └── 1124-表现良好的最长时间段.py │ ├── 1125.最小的必要团队 │ │ └── 1125-最小的必要团队.py │ ├── 1128.等价多米诺骨牌对的数量 │ │ └── 1128-等价多米诺骨牌对的数量.py │ ├── 1129.颜色交替的最短路径 │ │ └── 1129-颜色交替的最短路径.py │ ├── 1130.叶值的最小代价生成树 │ │ └── 1130-叶值的最小代价生成树.py │ ├── 1131.绝对值表达式的最大值 │ │ └── 1131-绝对值表达式的最大值.py │ ├── 1133.最大唯一数 │ │ └── 1133-最大唯一数.py │ ├── 1134.阿姆斯特朗数 │ │ └── 1134-阿姆斯特朗数.py │ ├── 1135.最低成本联通所有城市 │ │ └── 1135-最低成本联通所有城市.py │ ├── 1148.文章浏览I │ │ └── 1148-文章浏览I.sql │ ├── 1175.质数排列 │ │ └── 1175-质数排列.py │ ├── 1176.健身计划评估 │ │ └── 1176-健身计划评估.py │ ├── 1177.构建回文串检测 │ │ └── 1177-构建回文串检测.py │ ├── 1178.猜字谜 │ │ └── 1178-猜字谜.py │ ├── 1179.重新格式化部门表 │ │ └── 1179-重新格式化部门表.sql │ ├── 1180.统计只含单一字母的子串 │ │ └── 1180-统计只含单一字母的子串.py │ ├── 1181.前后拼接 │ │ └── 1181-前后拼接.py │ ├── 1182.与目标颜色间的最短距离 │ │ └── 1182-与目标颜色间的最短距离.py │ ├── 1184.公交站间的距离 │ │ └── 1184-公交站间的距离.py │ ├── 1186.删除一次得到子数组最大和 │ │ └── 1186-删除一次得到子数组最大和.py │ ├── 1187.使数组严格递增 │ │ └── 1187-使数组严格递增.py │ ├── 1188.设计有限阻塞队列 │ │ └── 1188-设计有限阻塞队列.py │ ├── 1190.反转每对括号间的子串 │ │ └── 1190-反转每对括号间的子串.py │ ├── 1192.查找集群内的「关键连接」 │ │ └── 1192-查找集群内的「关键连接」.py │ ├── 1193.每月交易I │ │ └── 1193-每月交易I.sql │ ├── 1194.锦标赛优胜者 │ │ └── 1194-锦标赛优胜者.sql │ ├── 1195.交替打印字符串 │ │ └── 1195-交替打印字符串.py │ ├── 1196.最多可以买到的苹果数量 │ │ └── 1196-最多可以买到的苹果数量.py │ ├── 1197.进击的骑士 │ │ └── 1197-进击的骑士.py │ ├── 1198.找出所有行中最小公共元素 │ │ └── 1198-找出所有行中最小公共元素.py │ ├── 1199.建造街区的最短时间 │ │ └── 1199-建造街区的最短时间.py │ ├── 1200.最小绝对差 │ │ └── 1200-最小绝对差.py │ ├── 1201.丑数III │ │ └── 1201-丑数III.py │ ├── 1202.交换字符串中的元素 │ │ └── 1202-交换字符串中的元素.py │ ├── 1203.项目管理 │ │ └── 1203-项目管理.py │ ├── 1205.每月交易II │ │ └── 1205-每月交易II.sql │ ├── 1206.设计跳表 │ │ └── 1206-设计跳表.py │ ├── 1207.独一无二的出现次数 │ │ └── 1207-独一无二的出现次数.py │ ├── 1208.尽可能使字符串相等 │ │ └── 1208-尽可能使字符串相等.py │ ├── 1209.删除字符串中的所有相邻重复项II │ │ └── 1209-删除字符串中的所有相邻重复项II.py │ ├── 1210.穿过迷宫的最少移动次数 │ │ └── 1210-穿过迷宫的最少移动次数.py │ ├── 1214.查找两棵二叉搜索树之和 │ │ └── 1214-查找两棵二叉搜索树之和.py │ ├── 1215.步进数 │ │ └── 1215-步进数.java │ ├── 1216.验证回文字符串III │ │ └── 1216-验证回文字符串III.py │ ├── 1225.报告系统状态的连续日期 │ │ └── 1225-报告系统状态的连续日期.sql │ ├── 1241.每个帖子的评论数 │ │ └── 1241-每个帖子的评论数.sql │ ├── 1260.二维网格迁移 │ │ └── 1260-二维网格迁移.py │ ├── 1261.在受污染的二叉树中查找元素 │ │ └── 1261-在受污染的二叉树中查找元素.py │ ├── 1262.可被三整除的最大和 │ │ └── 1262-可被三整除的最大和.py │ ├── 1263.推箱子 │ │ └── 1263-推箱子.py │ ├── 1264.页面推荐 │ │ └── 1264-页面推荐.sql │ ├── 1265.逆序打印不可变链表 │ │ └── 1265-逆序打印不可变链表.py │ ├── 1266.访问所有点的最小时间 │ │ └── 1266-访问所有点的最小时间.py │ ├── 1267.统计参与通信的服务器 │ │ └── 1267-统计参与通信的服务器.py │ ├── 1268.搜索推荐系统 │ │ └── 1268-搜索推荐系统.py │ ├── 1269.停在原地的方案数 │ │ └── 1269-停在原地的方案数.py │ ├── 1270.向公司CEO汇报工作的所有人 │ │ └── 1270-向公司CEO汇报工作的所有人.sql │ ├── 1272.删除区间 │ │ └── 1272-删除区间.py │ ├── 1273.删除树节点 │ │ └── 1273-删除树节点.py │ ├── 1274.矩形内船只的数目 │ │ └── 1274-矩形内船只的数目.py │ ├── 1275.找出井字棋的获胜者 │ │ └── 1275-找出井字棋的获胜者.py │ ├── 1276.不浪费原料的汉堡制作方案 │ │ └── 1276-不浪费原料的汉堡制作方案.py │ ├── 1278.分割回文串III │ │ └── 1278-分割回文串III.py │ ├── 1279.红绿灯路口 │ │ └── 1279-红绿灯路口.java │ ├── 1280.学生们参加各科测试的次数 │ │ └── 1280-学生们参加各科测试的次数.sql │ ├── 1281.整数的各位积和之差 │ │ └── 1281-整数的各位积和之差.py │ ├── 1282.用户分组 │ │ └── 1282-用户分组.py │ ├── 1283.使结果不超过阈值的最小除数 │ │ └── 1283-使结果不超过阈值的最小除数.py │ ├── 1284.转化为全零矩阵的最少反转次数 │ │ └── 1284-转化为全零矩阵的最少反转次数.py │ ├── 1285.找到连续区间的开始和结束数字 │ │ └── 1285-找到连续区间的开始和结束数字.sql │ ├── 1286.字母组合迭代器 │ │ └── 1286-字母组合迭代器.py │ ├── 1287.有序数组中出现次数超过25%的元素 │ │ └── 1287-有序数组中出现次数超过25%的元素.py │ ├── 1288.删除被覆盖区间 │ │ └── 1288-删除被覆盖区间.py │ ├── 1289.下降路径最小和II │ │ └── 1289-下降路径最小和II.py │ ├── 1292.元素和小于等于阈值的正方形的最大边长 │ │ └── 1292-元素和小于等于阈值的正方形的最大边长.py │ ├── 1293.网格中的最短路径 │ │ └── 1293-网格中的最短路径.py │ ├── 1294.不同国家的天气类型 │ │ └── 1294-不同国家的天气类型.sql │ ├── 1296.划分数组为连续数字的集合 │ │ └── 1296-划分数组为连续数字的集合.py │ ├── 1297.子串的最大出现次数 │ │ └── 1297-子串的最大出现次数.py │ ├── 1298.你能从盒子里获得的最大糖果数 │ │ └── 1298-你能从盒子里获得的最大糖果数.py │ ├── 1299.将每个元素替换为右侧最大元素 │ │ └── 1299-将每个元素替换为右侧最大元素.py │ ├── 1300.转变数组后最接近目标值的数组和 │ │ └── 1300-转变数组后最接近目标值的数组和.py │ ├── 1301.最大得分的路径数目 │ │ └── 1301-最大得分的路径数目.py │ ├── 1302.层数最深叶子节点的和 │ │ └── 1302-层数最深叶子节点的和.py │ ├── 1303.求团队人数 │ │ └── 1303-求团队人数.sql │ ├── 1304.和为零的N个唯一整数 │ │ └── 1304-和为零的N个唯一整数.py │ ├── 1306.跳跃游戏III │ │ └── 1306-跳跃游戏III.py │ ├── 1307.口算难题 │ │ └── 1307-口算难题.py │ ├── 1310.子数组异或查询 │ │ └── 1310-子数组异或查询.py │ ├── 1311.获取你好友已观看的视频 │ │ └── 1311-获取你好友已观看的视频.py │ ├── 1312.让字符串成为回文串的最少插入次数 │ │ └── 1312-让字符串成为回文串的最少插入次数.py │ ├── 1314.矩阵区域和 │ │ └── 1314-矩阵区域和.py │ ├── 1315.祖父节点值为偶数的节点和 │ │ └── 1315-祖父节点值为偶数的节点和.py │ ├── 1316.不同的循环子字符串 │ │ └── 1316-不同的循环子字符串.py │ ├── 1324.竖直打印单词 │ │ └── 1324-竖直打印单词.py │ ├── 1325.删除给定值的叶子节点 │ │ └── 1325-删除给定值的叶子节点.py │ └── 1367.二叉树中的列表 │ │ ├── 1367-二叉树中的列表.cpp │ │ └── 1367-二叉树中的列表.py ├── Clarification │ ├── Array │ │ ├── 100.相同的树.py │ │ ├── 1002.查找常用字符.py │ │ ├── 1010.总持续时间可被60整除的歌曲.py │ │ ├── 1013.将数组分成和相等的三部分.py │ │ ├── 1018.可被5整除的二进制前缀.py │ │ ├── 1033.移动石子直到连续.py │ │ ├── 1071.字符串的最大因子.py │ │ ├── 1085.最小元素各数位之和.py │ │ ├── 1089.复写零.py │ │ ├── 1119.删去字符串中的元音.py │ │ ├── 1122.数组的相对排序.py │ │ ├── 1137.第N个泰波纳契数.py │ │ ├── 1150.检查一个数是否再数组中占绝大多数.py │ │ ├── 1221.分割平衡字符串.py │ │ ├── 1295.统计位数为偶数的数字.py │ │ ├── 134.加油站.py │ │ ├── 157.用Read读取N个字符.py │ │ ├── 158.用Read4读取N个字符2.py │ │ ├── 161.相隔为1的编辑距离.py │ │ ├── 163.缺失的区间.py │ │ ├── 165.比较版本号.py │ │ ├── 169.多数元素.py │ │ ├── 179.最大数.py │ │ ├── 186.翻转字符串里的单词Ⅱ.py │ │ ├── 214.最短回文串.py │ │ ├── 228.汇总区间.py │ │ ├── 229.求众数Ⅱ.py │ │ ├── 243.最短单词距离.py │ │ ├── 246.中心对称树.py │ │ ├── 253.会议室Ⅱ.py │ │ ├── 271.字符串的编码与解码.py │ │ ├── 273.整数转换英文表示.py │ │ ├── 274.H指数.py │ │ ├── 275.H指数Ⅱ.java │ │ ├── 277.搜寻名人.py │ │ ├── 280.摆动排序.py │ │ ├── 289.生命游戏.py │ │ ├── 293.翻转游戏.py │ │ ├── 301.删除无效的括号.py │ │ ├── 31.下一个排列.java │ │ ├── 316.去除重复字母.py │ │ ├── 32.最长有效括号.java │ │ ├── 321.拼接最大数.py │ │ ├── 327.区间和的个数.py │ │ ├── 35.搜索插入位置.py │ │ ├── 352.将数据流变为多个不相交区间.py │ │ ├── 358.K距离间隔重排字符串.py │ │ ├── 383.赎金信.py │ │ ├── 389.找不同.py │ │ ├── 392.判断子序列.py │ │ ├── 408.有效单词缩写.py │ │ ├── 41.缺失的第一个正数.py │ │ ├── 414.第三大的数.py │ │ ├── 415.字符串相加.py │ │ ├── 422.有效的单词方块.py │ │ ├── 43.字符串相乘.java │ │ ├── 434.字符串中的单词数.py │ │ ├── 44.通配符匹配.py │ │ ├── 448.找到所有数组中消失的数字.py │ │ ├── 459.重复的子字符串.py │ │ ├── 485.最大连续1的个数.py │ │ ├── 504.七进制数.py │ │ ├── 57.插入区间.py │ │ ├── 59.螺旋矩阵2.py │ │ ├── 628.三个数的最大乘积.py │ │ ├── 643.子数组最大平均数Ⅰ.py │ │ ├── 65.有效数字.py │ │ ├── 651.数组拆分.py │ │ ├── 665.非递减数列.py │ │ ├── 674.最长连续递增序列.py │ │ ├── 68.文本左右对齐.py │ │ ├── 680.验证回文字符串.py │ │ ├── 686.重复叠加字符串匹配.py │ │ ├── 697.数组的度.py │ │ ├── 709.转换成小写字母.py │ │ ├── 821.字符的最短距离.py │ │ ├── 867.转置矩阵.py │ │ ├── 896.单调数列.py │ │ ├── 917.仅仅反转字母.py │ │ ├── 938.二叉搜索树的范围和.py │ │ ├── 977.有序数组的平方.py │ │ ├── 989.数组形式的整数加法.py │ │ ├── z字形变换.py │ │ ├── 一周中的第几天.py │ │ ├── 两句话种的不常见单词.py │ │ ├── 乘积最大子序列.py │ │ ├── 卡牌分组.py │ │ ├── 反转字符串中的单词Ⅲ.py │ │ ├── 按奇偶排序数组.py │ │ ├── 数组列表中的最大距离.py │ │ ├── 整数转罗马数字.py │ │ ├── 最长回文子串.py │ │ ├── 正则表达式匹配.py │ │ ├── 缺失数字.py │ │ ├── 翻转字符串里的单词.py │ │ ├── 翻转游戏Ⅱ.py │ │ ├── 递增的三元子序列.py │ │ └── 除自身以外数组的乘积.py │ ├── Backtracking │ │ ├── 131.分割回文串.py │ │ ├── 216.组合总和3.py │ │ ├── 254.因子的组合.py │ │ ├── 267.回文排列2.java │ │ ├── 282.给表达式添加运算符.py │ │ ├── 291.单词规律2.py │ │ ├── 320.列举单词的全部缩写.py │ │ ├── 351.安卓系统手势解锁.py │ │ ├── 37.解数独.py │ │ ├── 39.组合总和.py │ │ ├── 40.组合总和Ⅱ.py │ │ ├── 47.全排列Ⅱ.py │ │ ├── 51.N皇后.py │ │ ├── 52.N皇后2.py │ │ ├── 60.第k个排列.py │ │ ├── 90.子集2.py │ │ ├── 93.复原IP地址.py │ │ ├── 全排列.py │ │ ├── 单词搜索.py │ │ ├── 子集.py │ │ ├── 生成括号.py │ │ └── 电话号码的字母组合.py │ ├── Binary Search │ │ ├── 74.搜索二维矩阵.py │ │ ├── Pow(x,n).py │ │ ├── x的平方根.py │ │ ├── 二分查找.py │ │ ├── 分割数组的最大值.py │ │ ├── 在排序数组中查找元素的第一个和最后一个元素.py │ │ ├── 寻找两个有序数组的中位数.py │ │ ├── 寻找峰值.py │ │ ├── 寻找旋转排序数组中的最小值.py │ │ ├── 寻找旋转排序数组中的最小值Ⅱ.py │ │ ├── 寻找比目标字母大的最小字母.py │ │ ├── 寻找重复数.py │ │ ├── 找出第k小的距离对.py │ │ ├── 找到k个最接近的元素.py │ │ ├── 搜索旋转排序数组.py │ │ ├── 有效的完全平方数.py │ │ └── 猜数字大小.py │ ├── Bit_Manipulation │ │ ├── 260.只出现一次的数字3.py │ │ ├── 318.最大单词长度乘积.py │ │ ├── 338.比特位计数.py │ │ └── 393.UTF-8编码验证.py │ ├── DFS_BFS │ │ ├── 126.单词接龙2.py │ │ ├── 127.单词接龙.py │ │ ├── 130.被围绕的区域.py │ │ ├── 339.嵌套链表权重和.py │ │ ├── 364.加权嵌套序列和2.py │ │ ├── 51.N皇后.py │ │ └── 52.N皇后2.py │ ├── Design │ │ ├── 146.LRU缓存机制.py │ │ ├── 170.两数之和 III - 数据结构设计.py │ │ ├── 251.展开二维向量.py │ │ ├── 281.锯齿迭代器.py │ │ ├── 284.顶端迭代器.py │ │ ├── 288.单词的唯一缩写.py │ │ ├── 304.二维区域和检索 - 矩阵不可变.py │ │ ├── 307.区域和检索 - 数组可修改.py │ │ ├── 308.二维区域和检索 - 可变.py │ │ ├── 348.判定井字棋胜负.py │ │ ├── 353.贪吃蛇.py │ │ ├── 355.设计推特.py │ │ ├── 359.日志速率限制器.py │ │ ├── 362.敲击计数器.py │ │ └── 379.电话目录管理系统.py │ ├── Dynamic_Programming │ │ ├── 115.不同的子序列.py │ │ ├── 120.三角形最小路径和.py │ │ ├── 123.买卖股票的最佳时机Ⅲ.py │ │ ├── 132.分割回文串2.py │ │ ├── 139.单词拆分.py │ │ ├── 140.单词拆分2.py │ │ ├── 174.地下城游戏.py │ │ ├── 188.买卖股票的最佳时机Ⅳ.py │ │ ├── 199.二叉树的右视图.py │ │ ├── 213.打家劫舍2.py │ │ ├── 221.最大正方形.py │ │ ├── 239.滑动窗口的最大值.py │ │ ├── 256.粉刷房子.py │ │ ├── 265.粉刷房子2.py │ │ ├── 276.栅栏涂色.py │ │ ├── 309.最佳买卖股票时机含冷冻期.py │ │ ├── 312.戳气球.py │ │ ├── 337.打家劫舍3.py │ │ ├── 363. 矩形区域不超过 K 的最大数值和.py │ │ ├── 375.猜数字大小.py │ │ ├── 376.摆动序列.py │ │ ├── 377.组合总和4.py │ │ ├── 63.不同路径2.py │ │ ├── 64.最小路径和.py │ │ ├── 72.编辑距离.py │ │ ├── 746.使用最小花费爬楼梯.py │ │ ├── 85.最大矩形.py │ │ ├── 91.解码方法.py │ │ ├── 97.交错字符串.py │ │ ├── 不同路径.py │ │ ├── 最长上升子序列.py │ │ ├── 最长连续序列.py │ │ ├── 跳跃游戏.py │ │ └── 零钱兑换.py │ ├── Graph │ │ ├── 223.矩形面积.py │ │ ├── 310.最小高度树.py │ │ ├── 335.路径交叉.py │ │ ├── 356.直线镜像.py │ │ ├── 391.完美矩形.py │ │ ├── 399.除法求值.py │ │ ├── 中序遍历二叉树.py │ │ ├── 二叉搜索树中第k小的元素.py │ │ ├── 二叉树的前序遍历.py │ │ ├── 二叉树的后序遍历.py │ │ ├── 二叉树的层次遍历.py │ │ ├── 二叉树的最大深度.py │ │ ├── 二叉树的锯齿形层次遍历.py │ │ ├── 从前序与中序遍历序列构造二叉树.py │ │ ├── 填充每个节点的下一个右侧节点指针.py │ │ ├── 对称二叉树.py │ │ ├── 将有序数组转换为二叉搜索树.py │ │ ├── 岛屿数量.py │ │ └── 验证二叉搜索树.py │ ├── LinkedList │ │ ├── 143.重排链表.py │ │ ├── 147.对链表进行插入排序.py │ │ ├── 148.排序链表.py │ │ ├── 369.给单链表加一.py │ │ ├── 86.分割链表.py │ │ ├── 876.链表的中间结点.py │ │ ├── 92.反转链表2.py │ │ ├── 两数相加.py │ │ ├── 删除排序链表中的重复元素.py │ │ ├── 删除排序链表中的重复元素Ⅱ.py │ │ ├── 反转链表.py │ │ ├── 复制带随机指针的链表.py │ │ ├── 奇偶链表.py │ │ ├── 扁平化多级双向链表.py │ │ ├── 旋转链表.py │ │ ├── 环形链表2.py │ │ ├── 相交链表.py │ │ ├── 移除链表元素.py │ │ └── 设计链表.py │ ├── Math │ │ ├── 1118.一月有多少天.py │ │ ├── 168.Excel表列名称.py │ │ ├── 233.数字1的个数.py │ │ ├── 247.中心对称数Ⅱ.py │ │ ├── 248.中心对称数3.py │ │ ├── 258.各位相加.py │ │ ├── 259.较小的三数之和.py │ │ ├── 263.丑数.py │ │ ├── 264.丑数Ⅱ.py │ │ ├── 2的幂.py │ │ ├── 306.累加数.py │ │ ├── 319.灯泡开关.py │ │ ├── 343.整数拆分.py │ │ ├── 357.计算各个位数不同的数字个数.py │ │ ├── 360.有序转化数组.py │ │ ├── 365.水壶问题.py │ │ ├── 368.最大整除子集.py │ │ ├── 372.超级次方.py │ │ ├── 386.字典序排数.py │ │ ├── 390.消除游戏.py │ │ ├── 396.旋转函数.py │ │ ├── 397.整数替换.py │ │ ├── 3的幂.py │ │ ├── 4的幂.py │ │ ├── 507.完美数.py │ │ ├── 633.平方数之和.py │ │ ├── 812.最大三角形面积.py │ │ ├── 976.三角形的最大周长.py │ │ ├── excel表列序号.py │ │ ├── 两个日期之间的天数.py │ │ ├── 两数相除.py │ │ ├── 分数到小数.py │ │ ├── 回文数.py │ │ ├── 数字的补数.py │ │ └── 阶乘后的零.py │ ├── Matrix │ │ ├── 296.最佳的碰头地点.py │ │ ├── 302.包含全部黑色像素的最小矩形.py │ │ ├── 311.稀疏矩阵的乘法.py │ │ ├── 317.离建筑物最近的距离.py │ │ ├── 329.矩阵中的最长递增路径.py │ │ ├── 361.轰炸敌人.py │ │ ├── 370.区间加法.py │ │ └── 378.有序矩阵中第K小的元素.py │ ├── Random │ │ ├── 381.O(1) 时间插入、删除和获取随机元素 - 允许重复.py │ │ ├── 382.链表随机节点.py │ │ └── 398.随机数索引.py │ ├── Stack_PriorityQueue │ │ ├── 1021.删除最外层的括号.py │ │ ├── 218.天际线问题.py │ │ ├── 224.基本计算器.py │ │ ├── 227.基本计算器2.py │ │ ├── 295.数据流的中位数.py │ │ ├── 313.超级丑数.py │ │ ├── 332.重新安排形成.py │ │ ├── 341.扁平化嵌套列表迭代器.py │ │ ├── 373.查找和最小的K对数字.py │ │ ├── 385.迷你语法分析器.py │ │ ├── 388.文件的最长绝对路径.py │ │ ├── 42.接雨水.py │ │ ├── 克隆图.py │ │ ├── 图像渲染.py │ │ ├── 墙与门.py │ │ ├── 字符串解码.py │ │ ├── 完全平方数.py │ │ ├── 打开转盘锁.py │ │ ├── 数据流中的移动平均值.py │ │ ├── 每日温度.py │ │ ├── 用栈实现队列.py │ │ ├── 用队列实现栈.py │ │ └── 逆波兰表达式求值.py │ ├── Topological_Sort │ │ ├── 207.课程表.py │ │ ├── 210.课程表2.py │ │ └── 269.火星词典.py │ ├── Tree │ │ ├── 107.二叉树的层次遍历.py │ │ ├── 109.有序链表转换二叉搜素树.py │ │ ├── 113.路径总和2.py │ │ ├── 114.二叉树展开为链表.py │ │ ├── 124.二叉树中的最大路径和.py │ │ ├── 129.求根到叶子节点数字之和.py │ │ ├── 156.上下翻转二叉树.py │ │ ├── 199.二叉树的右视图.py │ │ ├── 222.完全二叉树的结点个数.py │ │ ├── 226.翻转二叉树.py │ │ ├── 250.统计同值子树.py │ │ ├── 255.验证前序遍历序列二叉搜索树.py │ │ ├── 270.最接近的二叉搜索数值.py │ │ ├── 285.二叉搜素树种的顺序后继.py │ │ ├── 298.二叉树最长连续序列.py │ │ ├── 314.二叉树的垂直遍历.py │ │ ├── 331.验证二叉树的前序序列化.py │ │ ├── 333.最大BST子树.py │ │ ├── 366.寻找完全二叉树的叶子节点.py │ │ ├── 404.左叶子之和.py │ │ ├── 96.不同的二叉搜索树.py │ │ ├── 99.恢复二叉树.py │ │ ├── N叉树的前序遍历.py │ │ ├── N叉树的后续遍历.py │ │ ├── N叉树的层序遍历.py │ │ ├── N叉树的最大深度.py │ │ ├── 二叉搜索树中的插入操作.py │ │ ├── 二叉搜索树中的搜索.py │ │ ├── 二叉搜索树的最近公共祖先.py │ │ ├── 二叉搜索树迭代器.py │ │ ├── 二叉树的序列化与反序列化.py │ │ ├── 二叉树的所有路径.py │ │ ├── 二叉树的最小深度.py │ │ ├── 二叉树的最近公共祖先.py │ │ ├── 从中序与后序遍历构造二叉树.py │ │ ├── 删除二叉搜索树中的结点.py │ │ ├── 叶子相似的树.py │ │ ├── 填充每个节点的下一个右侧节点指针2.py │ │ ├── 平衡二叉树.py │ │ ├── 数据流中的第k大于元素.py │ │ ├── 路径总和.py │ │ └── 验证二叉搜索树.py │ ├── Trie │ │ ├── 208.实现Trie(前缀树).py │ │ ├── 211.添加与搜索单词 - 数据结构设计.py │ │ └── 212.单词搜索2.py │ └── Union_Find │ │ ├── 261.以图判树.py │ │ ├── 305.岛屿数量.py │ │ └── 323.无向图中连通分量的数目.py └── weeks │ ├── 1000002.找出隐藏信息.java │ ├── 5151.破坏回文串.py │ ├── 5155.数组序号转换.py │ ├── 5307. 将整数转换为两个无零整数的和.py │ ├── 5308. 或运算的最小翻转次数.py │ ├── 5309. 连通网络的操作次数.py │ ├── 5310. 二指输入的的最小距离.java │ ├── 5319.删除回文子序列.py │ ├── 5320.餐厅过滤器.py │ ├── 5328.方针中战斗力最弱的k行.py │ ├── 5329.数组大小减半.py │ ├── 5330.分裂二叉树的最大乘积.py │ ├── 5331.跳跃游戏Ⅴ.py │ ├── 全排列.py │ └── 顺次数.py └── 如何使用 Github 进行多人协作.md /source/All_Solutions/0001.两数之和/0001-两数之和.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, nums: List[int], target: int) -> List[int]: 3 | hashmap = {} 4 | for idx, num in enumerate(nums): 5 | if target - num in hashmap: 6 | return [hashmap[target - num],idx] 7 | else: 8 | hashmap[num] = idx -------------------------------------------------------------------------------- /source/All_Solutions/0002.两数相加/0002-两数相加.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0002.两数相加/0002-两数相加.py -------------------------------------------------------------------------------- /source/All_Solutions/0003.无重复字符的最长子串/0003-无重复字符的最长子串.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0003.无重复字符的最长子串/0003-无重复字符的最长子串.py -------------------------------------------------------------------------------- /source/All_Solutions/0005.最长回文子串/0005-最长回文子串.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0005.最长回文子串/0005-最长回文子串.py -------------------------------------------------------------------------------- /source/All_Solutions/0007.整数反转/0007-整数反转.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reverse(int x) { 4 | int rev = 0; 5 | while(x != 0){ 6 | int pop = x % 10; 7 | x /= 10; 8 | if (rev > INT_MAX/10||(rev == INT_MAX/10&&pop>7)) return 0; 9 | if(rev < INT_MIN/10 || (rev == INT_MIN/10 &&pop < -8)) return 0; 10 | rev = rev * 10 + pop; 11 | 12 | } 13 | return rev; 14 | } 15 | }; 16 | 17 | 18 | -------------------------------------------------------------------------------- /source/All_Solutions/0007.整数反转/0007-整数反转.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0007.整数反转/0007-整数反转.py -------------------------------------------------------------------------------- /source/All_Solutions/0008.字符串转换整数(atoi)/0008-字符串转换整数(atoi).py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def myAtoi(self, s): 3 | """ 4 | :type str: str 5 | :rtype: int 6 | """ 7 | # if len(s) == 0 : return 0 8 | ls = list(s.strip()) 9 | if not ls: return 0 10 | sign = -1 if ls[0] == '-' else 1 11 | if ls[0] in ['-','+'] : del ls[0] 12 | ret, i = 0, 0 13 | while i < len(ls) and ls[i].isdigit() : 14 | ret = ret*10 + ord(ls[i]) - ord('0') 15 | i += 1 16 | return max(-2**31, min(sign * ret,2**31-1)) -------------------------------------------------------------------------------- /source/All_Solutions/0010.正则表达式匹配/0010-正则表达式匹配.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0010.正则表达式匹配/0010-正则表达式匹配.py -------------------------------------------------------------------------------- /source/All_Solutions/0011.盛最多水的容器/0011-盛最多水的容器.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | if(height.size() <= 1) return -1; 5 | int i = 0, j = height.size() - 1, res = 0; 6 | while(i < j){ 7 | int h = min(height[i], height[j]); 8 | res = max(res, h * (j - i)); 9 | if(height[i] < height[j]) ++i; 10 | else --j; 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /source/All_Solutions/0012.整数转罗马数字/0012-整数转罗马数字.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intToRoman(self, num: int) -> str: 3 | nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] 4 | roman = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'] 5 | 6 | index = 0 7 | res = '' 8 | while index < 13: 9 | while num >= nums[index]: 10 | num -= nums[index] 11 | res += roman[index] 12 | index += 1 13 | return res -------------------------------------------------------------------------------- /source/All_Solutions/0013.罗马数字转整数/0013-罗马数字转整数.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def romanToInt(self, s: str) -> int: 3 | Roman2Int = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} 4 | Int = 0 5 | 6 | for index in range(len(s) - 1): 7 | if Roman2Int[s[index]] < Roman2Int[s[index + 1]]: 8 | Int -= Roman2Int[s[index]] 9 | else: 10 | Int += Roman2Int[s[index]] 11 | 12 | return Int + Roman2Int[s[-1]] 13 | -------------------------------------------------------------------------------- /source/All_Solutions/0042.接雨水/0042-接雨水.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int ans = 0; 5 | int n = height.size(); 6 | int maxleft; 7 | int maxright; 8 | for (int i = 0;i < n;i++){ 9 | maxleft = maxright = 0; 10 | for (int j = 0;j < i;j++){ 11 | maxleft = max(maxleft,height[j]); 12 | } 13 | for (int k = i;k height[i]){ 17 | ans += min(maxleft,maxright) - height[i]; 18 | } 19 | 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /source/All_Solutions/0055.跳跃游戏/0055-跳跃游戏.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canJump(vector& nums) { 4 | int max_i = 0; 5 | for(int i = 0; i= i && i + nums[i] > max_i){ 7 | max_i = i + nums[i]; 8 | } 9 | } 10 | return max_i >= nums.size() - 1; 11 | } 12 | }; -------------------------------------------------------------------------------- /source/All_Solutions/0131.分割回文串/0131-分割回文串.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def partition(self, s: str) -> List[List[str]]: 3 | res = [] 4 | 5 | def helper(s,tmp): 6 | if not s: 7 | res.append(tmp) 8 | for i in range(1,len(s) + 1): 9 | if s[:i] == s[:i][::-1]: 10 | helper(s[i:], tmp + [s[:i]]) 11 | helper(s,[]) 12 | return res -------------------------------------------------------------------------------- /source/All_Solutions/0146.LRU缓存机制/0146-LRU缓存机制.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0146.LRU缓存机制/0146-LRU缓存机制.py -------------------------------------------------------------------------------- /source/All_Solutions/0176.第二高的薪水/0176-第二高的薪水.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT 3 | (SELECT DISTINCT 4 | Salary 5 | FROM 6 | Employee 7 | ORDER BY Salary DESC 8 | LIMIT 1 OFFSET 1) AS SecondHighestSalary 9 | -------------------------------------------------------------------------------- /source/All_Solutions/0177.第N高的薪水/0177-第N高的薪水.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0177.第N高的薪水/0177-第N高的薪水.sql -------------------------------------------------------------------------------- /source/All_Solutions/0180.连续出现的数字/0180-连续出现的数字.sql: -------------------------------------------------------------------------------- 1 | select distinct Num as ConsecutiveNums 2 | from ( 3 | select Num, 4 | case 5 | when @prev = Num then @count := @count + 1 6 | when (@prev := Num) is not null then @count := 1 7 | end as CNT 8 | from Logs, (select @prev := null,@count := null) as t 9 | ) as temp 10 | where temp.CNT >= 3 -------------------------------------------------------------------------------- /source/All_Solutions/0181.超过经理收入的员工/0181-超过经理收入的员工.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT 3 | Name Employee 4 | FROM 5 | Employee AS a 6 | WHERE 7 | Salary > (SELECT 8 | Salary 9 | FROM 10 | Employee 11 | WHERE 12 | Id = a.Managerid) -------------------------------------------------------------------------------- /source/All_Solutions/0182.查找重复的电子邮箱/0182-查找重复的电子邮箱.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select distinct 3 | a.email as Email 4 | from 5 | person a 6 | group by 7 | a.email 8 | having 9 | count(a.email)>1; -------------------------------------------------------------------------------- /source/All_Solutions/0183.从不订购的客户/0183-从不订购的客户.sql: -------------------------------------------------------------------------------- 1 | select c.Name as Customers from Customers c where c.Id not in (select distinct o.CustomerId from Orders o); -------------------------------------------------------------------------------- /source/All_Solutions/0184.部门工资最高的员工/0184-部门工资最高的员工.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select 3 | d.Name as Department, 4 | e.Name as Employee, 5 | e.Salary 6 | from 7 | Employee e,Department d 8 | where 9 | e.DepartmentId=d.id 10 | and 11 | (e.Salary,e.DepartmentId) in (select max(Salary),DepartmentId from Employee group by DepartmentId); -------------------------------------------------------------------------------- /source/All_Solutions/0196.删除重复的电子邮箱/0196-删除重复的电子邮箱.sql: -------------------------------------------------------------------------------- 1 | DELETE from Person 2 | Where Id not in ( 3 | Select Id 4 | From( 5 | Select MIN(Id) as id 6 | From Person 7 | Group by Email 8 | ) t 9 | ) -------------------------------------------------------------------------------- /source/All_Solutions/0197.上升的温度/0197-上升的温度.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select 3 | Id 4 | from 5 | (select w.*, 6 | @curd := w.RecordDate, 7 | @curt := w.Temperature, 8 | @isH := if(datediff(@curd,@pred) = 1 and @curt > @pret,1,0) as r, 9 | @pret := @curt, 10 | @pred := @curd 11 | from 12 | Weather w, 13 | (select 14 | @curd := null, 15 | @pred := null, 16 | @curt := 0, 17 | @pret := 0, 18 | @isH := 0 19 | ) init 20 | order by w.RecordDate 21 | ) t 22 | 23 | where 24 | t.r = 1 -------------------------------------------------------------------------------- /source/All_Solutions/0223.矩形面积/0223-矩形面积.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0223.矩形面积/0223-矩形面积.py -------------------------------------------------------------------------------- /source/All_Solutions/0240.搜索二维矩阵II/0240-搜索二维矩阵II.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0240.搜索二维矩阵II/0240-搜索二维矩阵II.py -------------------------------------------------------------------------------- /source/All_Solutions/0262.行程和用户/0262-行程和用户.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select 3 | t.request_at Day, 4 | ( 5 | round(count(if(status != 'completed', status, null)) / count(status), 2) 6 | ) as 'Cancellation Rate' 7 | from 8 | Users u inner join Trips t 9 | on 10 | u.Users_id = t.Client_Id 11 | and 12 | u.banned != 'Yes' 13 | where 14 | t.Request_at >= '2013-10-01' 15 | and 16 | t.Request_at <= '2013-10-03' 17 | group by 18 | t.Request_at 19 | -------------------------------------------------------------------------------- /source/All_Solutions/0304.二维区域和检索-矩阵不可变/0304-二维区域和检索-矩阵不可变.py: -------------------------------------------------------------------------------- 1 | class NumMatrix: 2 | 3 | def __init__(self, matrix: List[List[int]]): 4 | if(not matrix): 5 | return 6 | m=len(matrix) 7 | n=len(matrix[0]) 8 | self.dp=[[0]*(n+1) for _ in range(m+1)] 9 | for i in range(1,m+1): 10 | for j in range(1,n+1): 11 | self.dp[i][j]=self.dp[i-1][j]+self.dp[i][j-1]+matrix[i-1][j-1]-self.dp[i-1][j-1] 12 | 13 | 14 | def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int: 15 | return self.dp[row2+1][col2+1]-self.dp[row2+1][col1]-self.dp[row1][col2+1]+self.dp[row1][col1] 16 | 17 | -------------------------------------------------------------------------------- /source/All_Solutions/0308.二维区域和检索-可变/0308-二维区域和检索-可变.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0308.二维区域和检索-可变/0308-二维区域和检索-可变.py -------------------------------------------------------------------------------- /source/All_Solutions/0370.区间加法/0370-区间加法.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]: 3 | res = [0] * (length + 1) 4 | for i, j, x in updates: 5 | res[i] += x 6 | res[j+1] -= x 7 | for i in range(1,length): 8 | res[i] += res[i-1] 9 | return res[:-1] -------------------------------------------------------------------------------- /source/All_Solutions/0406.根据身高重建队列/0406-根据身高重建队列.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def reconstructQueue(self, people): 3 | """ 4 | :type people: List[List[int]] 5 | :rtype: List[List[int]] 6 | """ 7 | people.sort(key = lambda x:(-x[0], x[1])) 8 | output = [] 9 | for p in people: 10 | output.insert(p[1],p) 11 | return output 12 | -------------------------------------------------------------------------------- /source/All_Solutions/0416.分割等和子集/0416-分割等和子集.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def canPartition(self, nums): 3 | n=len(nums) 4 | target=sum(nums) 5 | if(target%2!=0): 6 | return False 7 | target//=2 8 | dp=[[False]*(target+1) for _ in range(n)] 9 | dp[0][0]=True 10 | for i in range(1,target+1): 11 | if(nums[0]==i): 12 | dp[0][i]=True 13 | break 14 | for i in range(1,n): 15 | for j in range(target+1): 16 | if(j>=nums[i]): 17 | dp[i][j]=dp[i-1][j] or (dp[i-1][j-nums[i]]) 18 | else: 19 | dp[i][j]=dp[i-1][j] 20 | return dp[-1][-1] 21 | 22 | 23 | -------------------------------------------------------------------------------- /source/All_Solutions/0437.路径总和III/0437-路径总和III.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0437.路径总和III/0437-路径总和III.py -------------------------------------------------------------------------------- /source/All_Solutions/0438.找到字符串中所有字母异位词/0438-找到字符串中所有字母异位词.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0438.找到字符串中所有字母异位词/0438-找到字符串中所有字母异位词.py -------------------------------------------------------------------------------- /source/All_Solutions/0465.最优账单平衡/0465-最优账单平衡.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0465.最优账单平衡/0465-最优账单平衡.py -------------------------------------------------------------------------------- /source/All_Solutions/0467.环绕字符串中唯一的子字符串/0467-环绕字符串中唯一的子字符串.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0467.环绕字符串中唯一的子字符串/0467-环绕字符串中唯一的子字符串.py -------------------------------------------------------------------------------- /source/All_Solutions/0469.凸多边形/0469-凸多边形.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isConvex(self, points: List[List[int]]) -> bool: 3 | n = len(points) 4 | zcrossproduct = None 5 | for i in range(-2, n-2): 6 | x = [ points[i][0], points[i+1][0], points[i+2][0] ] 7 | y = [ points[i][1], points[i+1][1], points[i+2][1] ] 8 | dx1 = x[1] - x[0] 9 | dy1 = y[1] - y[0] 10 | dx2 = x[2] - x[1] 11 | dy2 = y[2] - y[1] 12 | if not zcrossproduct: 13 | zcrossproduct = dx1 * dy2 - dy1 * dx2 14 | elif ( dx1 * dy2 - dy1 * dx2 ) * zcrossproduct < 0: 15 | return False 16 | return True 17 | 18 | -------------------------------------------------------------------------------- /source/All_Solutions/0470.用Rand7()实现Rand10()/0470-用Rand7()实现Rand10().py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rand10(self): 3 | """ 4 | :rtype: int 5 | """ 6 | n=rand7() 7 | while n>5: 8 | n=rand7() 9 | i=rand7() 10 | while i==4: 11 | i=rand7() 12 | if i<4: 13 | j=0 14 | else: 15 | j=5 16 | return n+j 17 | 18 | -------------------------------------------------------------------------------- /source/All_Solutions/0474.一和零/0474-一和零.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0474.一和零/0474-一和零.py -------------------------------------------------------------------------------- /source/All_Solutions/0475.供暖器/0475-供暖器.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0475.供暖器/0475-供暖器.py -------------------------------------------------------------------------------- /source/All_Solutions/0477.汉明距离总和/0477-汉明距离总和.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def totalHammingDistance(self, nums: List[int]) -> int: 3 | return sum((b.count('0') * b.count('1')) for b in zip(*map('{:032b}'.format, nums))) 4 | 5 | -------------------------------------------------------------------------------- /source/All_Solutions/0478.在圆内随机生成点/0478-在圆内随机生成点.py: -------------------------------------------------------------------------------- 1 | import random 2 | import math 3 | class Solution(object): 4 | def __init__(self, radius, x_center, y_center): 5 | """ 6 | :type radius: float 7 | :type x_center: float 8 | :type y_center: float 9 | """ 10 | self.radius = radius 11 | self.x = x_center 12 | self.y = y_center 13 | def randPoint(self): 14 | """ 15 | :rtype: List[float] 16 | """ 17 | r = (random.random() ** 0.5) * self.radius 18 | theta = random.uniform(0, 2 * math.pi) 19 | return [r * math.cos(theta) + self.x, r * math.sin(theta) + self.y] -------------------------------------------------------------------------------- /source/All_Solutions/0479.最大回文数乘积/0479-最大回文数乘积.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestPalindrome(self, n: int) -> int: 3 | ans = [9, 987, 123, 597, 677, 1218, 877, 475] 4 | return ans[n - 1] -------------------------------------------------------------------------------- /source/All_Solutions/0481.神奇字符串/0481-神奇字符串.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def magicalString(self, n: int) -> int: 3 | ans,i = [1],0 4 | for i in range(n): 5 | if ans[i]==2: 6 | ans.append(ans[-1]) 7 | ans.append(ans[-1]^3) 8 | return ans[:n].count(1) 9 | -------------------------------------------------------------------------------- /source/All_Solutions/0482.密钥格式化/0482-密钥格式化.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0482.密钥格式化/0482-密钥格式化.py -------------------------------------------------------------------------------- /source/All_Solutions/0483.最小好进制/0483-最小好进制.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestGoodBase(self, n: str) -> str: 3 | num = int(n) 4 | n_max = int(math.floor(math.log(num + 1) / math.log(2))) - 1 5 | for n in range(n_max, 1, -1): 6 | q = int(max(math.floor(math.pow(num, 1 / n)), 2)) 7 | s = (pow(q, n + 1) - 1) / (q - 1) 8 | if abs(num - s) < 0.001: 9 | return str(q) 10 | return str(num - 1) -------------------------------------------------------------------------------- /source/All_Solutions/0484.寻找排列/0484-寻找排列.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findPermutation(self, s: str) -> List[int]: 3 | nums = list(range(1, len(s) + 2)) 4 | i = 0 5 | while i < len(s): 6 | if s[i] == "D": 7 | start = i 8 | for j in range(i, len(s)): 9 | if s[j] == "D" and (j == len(s) - 1 or s[j+1] == "I"): 10 | end = j + 1 11 | i = end + 1 12 | break 13 | nums[start:end + 1] = list(reversed(nums[start:end + 1])) 14 | else: 15 | i += 1 16 | 17 | return nums -------------------------------------------------------------------------------- /source/All_Solutions/0486.预测赢家/0486-预测赢家.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def PredictTheWinner(self, nums: List[int]) -> bool: 3 | n = len(nums) 4 | if n % 2 == 0: 5 | return True 6 | dp = [[0]*n for _ in range(n)] 7 | for i in range(n): 8 | dp[i][i] = nums[i] 9 | for i in range(1, n): 10 | dp[i-1][i] = max(nums[i-1], nums[i]) 11 | for i in range(n-2,-1,-1): 12 | for j in range(i+2, n): 13 | dp[i][j] = max(nums[i] + min(dp[i+1][j-1], dp[i+2][j]), 14 | nums[j] + min(dp[i+1][j-1], dp[i][j-2])) 15 | return dp[0][n-1] >= sum(nums) - dp[0][n-1] -------------------------------------------------------------------------------- /source/All_Solutions/0487.最大连续1的个数II/0487-最大连续1的个数II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMaxConsecutiveOnes(int[] nums) { 3 | int filpPos=-1; 4 | int maxCount =0; 5 | int curCount =0; 6 | for(int i=0;i= 2: 9 | self.s.add(tuple(cur)) 10 | for i in range(st, len(self.nums), 1): 11 | if len(cur) > 0 and cur[-1] > self.nums[i]: 12 | continue 13 | cur.append(self.nums[i]) 14 | self.dfs(st=i + 1, cur=cur) 15 | cur.pop() 16 | 17 | def findSubsequences(self, nums: List[int]) -> List[List[int]]: 18 | self.nums = nums 19 | self.dfs(st=0, cur=[]) 20 | return [list(a) for a in self.s] 21 | 22 | -------------------------------------------------------------------------------- /source/All_Solutions/0492.构造矩形/0492-构造矩形.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def constructRectangle(self, area: int) -> List[int]: 3 | for i in range(int(area ** 0.5), 0, -1): 4 | if area % i == 0: 5 | return [area // i, i] -------------------------------------------------------------------------------- /source/All_Solutions/0493.翻转对/0493-翻转对.py: -------------------------------------------------------------------------------- 1 | import bisect 2 | 3 | class Solution: 4 | def reversePairs(self, nums: List[int]) -> int: 5 | ri = [] 6 | res = 0 7 | n = len(nums) 8 | for i in reversed(range(0, n)): 9 | res += bisect.bisect_left(ri, nums[i]) 10 | bisect.insort(ri, 2 * nums[i]) 11 | return res -------------------------------------------------------------------------------- /source/All_Solutions/0494.目标和/0494-目标和.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0494.目标和/0494-目标和.py -------------------------------------------------------------------------------- /source/All_Solutions/0495.提莫攻击/0495-提莫攻击.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int: 3 | n = len(timeSeries) 4 | if n == 0: 5 | return 0 6 | 7 | total = 0 8 | for i in range(n - 1): 9 | total += min(timeSeries[i + 1] - timeSeries[i], duration) 10 | return total + duration 11 | 12 | -------------------------------------------------------------------------------- /source/All_Solutions/0496.下一个更大元素I/0496-下一个更大元素I.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def nextGreaterElement(self, nums1, nums2): 3 | stack, hash = [], {} 4 | for n in nums2: 5 | while stack and stack[-1] < n: 6 | hash[stack.pop()] = n 7 | stack.append(n) 8 | 9 | return [hash.get(x, -1) for x in nums1] -------------------------------------------------------------------------------- /source/All_Solutions/0497.非重叠矩形中的随机点/0497-非重叠矩形中的随机点.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0497.非重叠矩形中的随机点/0497-非重叠矩形中的随机点.py -------------------------------------------------------------------------------- /source/All_Solutions/0499.迷宫III/0499-迷宫III.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0499.迷宫III/0499-迷宫III.py -------------------------------------------------------------------------------- /source/All_Solutions/0502.IPO/0502-IPO.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMaximizedCapital(self, k: int, W: int, Profits: List[int], Capital: List[int]) -> int: 3 | dp = [i for i in zip(Capital, Profits)] 4 | dp.sort() 5 | heap = [] 6 | n, i = len(dp), 0 7 | while k: 8 | while i < n and dp[i][0] <= W: 9 | heapq.heappush(heap, -dp[i][1]) 10 | i += 1 11 | if not heap: 12 | return W 13 | W += -heapq.heappop(heap) 14 | k -= 1 15 | return W -------------------------------------------------------------------------------- /source/All_Solutions/0503.下一个更大元素II/0503-下一个更大元素II.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def nextGreaterElements(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[int] 6 | """ 7 | nums_length = len(nums) 8 | res_list = [-1 for _ in range(nums_length)] 9 | stack = list() 10 | 11 | double_nums = nums + nums 12 | for index, num in enumerate(double_nums): 13 | while stack and nums[stack[-1]] < num: 14 | res_list[stack[-1]] = num 15 | stack.pop() 16 | if index < nums_length: 17 | stack.append(index) 18 | return res_list -------------------------------------------------------------------------------- /source/All_Solutions/0508.出现次数最多的子树元素和/0508-出现次数最多的子树元素和.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findFrequentTreeSum(self, root): 3 | """ 4 | :type root: TreeNode 5 | :rtype: List[int] 6 | """ 7 | self.d = {} 8 | def dfs(node): 9 | l = dfs(node.left) if node.left else 0 10 | r = dfs(node.right) if node.right else 0 11 | s = node.val + l + r 12 | self.d[s] = self.d.get(s, 0) + 1 13 | return s 14 | if not root: 15 | return [] 16 | dfs(root) 17 | maxv = max(self.d.values()) 18 | res = filter(lambda x : self.d[x] == maxv, self.d) 19 | return res -------------------------------------------------------------------------------- /source/All_Solutions/0510.二叉搜索树中的中序后继II/0510-二叉搜索树中的中序后继II.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def inorderSuccessor(self, node: 'Node') -> 'Node': 3 | # the successor is somewhere lower in the right subtree 4 | if node.right: 5 | node = node.right 6 | while node.left: 7 | node = node.left 8 | return node 9 | 10 | # the successor is somewhere upper in the tree 11 | while node.parent and node == node.parent.right: 12 | node = node.parent 13 | return node.parent 14 | 15 | -------------------------------------------------------------------------------- /source/All_Solutions/0513.找树左下角的值/0513-找树左下角的值.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findBottomLeftValue(self, root: TreeNode) -> int: 3 | d = {} 4 | def f(r, i): 5 | if r: 6 | d[i] = r.val 7 | f(r.right, i + 1) 8 | f(r.left, i + 1) 9 | f(root, 0) 10 | return next(reversed(d.values())) -------------------------------------------------------------------------------- /source/All_Solutions/0514.自由之路/0514-自由之路.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0514.自由之路/0514-自由之路.py -------------------------------------------------------------------------------- /source/All_Solutions/0543.二叉树的直径/0543-二叉树的直径.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | 8 | class Solution: 9 | def diameterOfBinaryTree(self, root: TreeNode) -> int: 10 | self.ans = 1 11 | def depth(node): 12 | if not node: return 0 13 | L = depth(node.left) 14 | R = depth(node.right) 15 | self.ans = max(self.ans,L + R + 1) 16 | return max(L, R) + 1 17 | depth(root) 18 | return self.ans - 1 19 | 20 | -------------------------------------------------------------------------------- /source/All_Solutions/0560.和为K的子数组/0560-和为K的子数组.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def subarraySum(self, nums: List[int], k: int) -> int: 3 | hash = {0:1} 4 | sum = 0 5 | count = 0 6 | for i in range(len(nums)): 7 | sum += nums[i] 8 | if (sum - k) in hash: 9 | count += hash[sum - k] 10 | if sum in hash: 11 | hash[sum] += 1 12 | else: 13 | hash[sum] = 1 14 | return count 15 | 16 | 17 | -------------------------------------------------------------------------------- /source/All_Solutions/0567.字符串的排列/0567-字符串的排列.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0567.字符串的排列/0567-字符串的排列.py -------------------------------------------------------------------------------- /source/All_Solutions/0569.员工薪水中位数/0569-员工薪水中位数.sql: -------------------------------------------------------------------------------- 1 | SELECT t1.Id, t1.Company, t1.Salary 2 | FROM Employee t1 3 | LEFT JOIN Employee t2 ON t1.Company = t2.Company 4 | GROUP BY t1.Company, t1.Salary 5 | HAVING SUM(CASE 6 | WHEN t1.Salary = t2.Salary THEN 1 7 | ELSE 0 8 | END) >= abs(SUM(sign(t1.Salary - t2.Salary))) 9 | ORDER BY t1.Id; -------------------------------------------------------------------------------- /source/All_Solutions/0570.至少有5名直接下属的经理/0570-至少有5名直接下属的经理.sql: -------------------------------------------------------------------------------- 1 | select Name 2 | from Employee 3 | where Id in( 4 | select distinct ManagerId 5 | from Employee 6 | group by ManagerID 7 | having count(ManagerID)>=5 8 | ) -------------------------------------------------------------------------------- /source/All_Solutions/0571.给定数字的频率查询中位数/0571-给定数字的频率查询中位数.sql: -------------------------------------------------------------------------------- 1 | select avg(t.minnum) median from 2 | (select min(a1.number) minnum 3 | from 4 | (select n1.number,sum(n2.frequency) cumsum from numbers n1,numbers n2 5 | where n1.number>=n2.number group by n1.number) a1, 6 | (select sum(frequency) cnt from numbers)a2 7 | where a1.cumsum>=floor((a2.cnt+1)/2) 8 | 9 | union all 10 | 11 | 12 | select min(a3.number) minnum 13 | from 14 | (select n3.number,sum(n4.frequency) cumsum 15 | from numbers n3,numbers n4 16 | where 17 | n3.number>=n4.number 18 | group by n3.number) a3,(select sum(frequency) cnt from numbers)a4 19 | where a3.cumsum>=ceil((a4.cnt+1)/2)) t -------------------------------------------------------------------------------- /source/All_Solutions/0574.当选者/0574-当选者.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | name AS 'Name' 3 | FROM 4 | Candidate 5 | JOIN 6 | (SELECT 7 | Candidateid 8 | FROM 9 | Vote 10 | GROUP BY Candidateid 11 | ORDER BY COUNT(*) DESC 12 | LIMIT 1) AS winner 13 | WHERE 14 | Candidate.id = winner.Candidateid 15 | ; 16 | 17 | -------------------------------------------------------------------------------- /source/All_Solutions/0577.员工奖金/0577-员工奖金.sql: -------------------------------------------------------------------------------- 1 | select e.name,b.bonus 2 | from employee e left join bonus b 3 | on e.empid=b.empid 4 | where 5 | ifnull(bonus,0)<1000 ; -------------------------------------------------------------------------------- /source/All_Solutions/0578.查询回答率最高的问题/0578-查询回答率最高的问题.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT question_id survey_log FROM survey_log 3 | WHERE answer_id IS NOT NULL 4 | GROUP BY question_id 5 | ORDER BY COUNT(answer_id) DESC 6 | LIMIT 1 7 | -------------------------------------------------------------------------------- /source/All_Solutions/0579.查询员工的累计薪水/0579-查询员工的累计薪水.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | a.Id AS id, a.Month AS month,SUM(b.Salary) AS Salary 3 | FROM 4 | Employee a, Employee b 5 | WHERE a.Id = b.Id 6 | AND a.Month >= b.Month 7 | AND a.Month < b.Month+3 8 | AND (a.Id, a.Month) NOT IN (SELECT Id, MAX(Month) FROM Employee GROUP BY Id) 9 | GROUP BY a.Id, a.Month 10 | ORDER BY a.Id, a.Month DESC -------------------------------------------------------------------------------- /source/All_Solutions/0580.统计各专业学生人数/0580-统计各专业学生人数.sql: -------------------------------------------------------------------------------- 1 | select d.dept_name , count(s.dept_id) 'student_number' 2 | from department d left join student s 3 | on s.dept_id=d.dept_id 4 | group by d.dept_id 5 | order by student_number desc,dept_name; -------------------------------------------------------------------------------- /source/All_Solutions/0584.寻找用户推荐人/0584-寻找用户推荐人.sql: -------------------------------------------------------------------------------- 1 | select name from customer 2 | where 3 | ifnull(referee_id,0)!=2; -------------------------------------------------------------------------------- /source/All_Solutions/0585.2016年的投资/0585-2016年的投资.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0585.2016年的投资/0585-2016年的投资.sql -------------------------------------------------------------------------------- /source/All_Solutions/0586.订单最多的客户/0586-订单最多的客户.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | customer_number 3 | FROM 4 | orders 5 | GROUP BY 6 | customer_number 7 | ORDER BY 8 | COUNT(customer_number) DESC 9 | LIMIT 1 -------------------------------------------------------------------------------- /source/All_Solutions/0591.标签验证器/0591-标签验证器.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isValid(self, code: str) -> bool: 3 | code = re.sub(r'|t', '-', code) 4 | prev = None 5 | while code != prev: 6 | prev = code 7 | code = re.sub(r'<([A-Z]{1,9})>[^<]*', 't', code) 8 | return code == 't' -------------------------------------------------------------------------------- /source/All_Solutions/0593.有效的正方形/0593-有效的正方形.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0593.有效的正方形/0593-有效的正方形.py -------------------------------------------------------------------------------- /source/All_Solutions/0595.大的国家/0595-大的国家.sql: -------------------------------------------------------------------------------- 1 | select name,population,area from World 2 | where area>3000000 or population>25000000 -------------------------------------------------------------------------------- /source/All_Solutions/0596.超过5名学生的课/0596-超过5名学生的课.sql: -------------------------------------------------------------------------------- 1 | select class 2 | from ( 3 | select distinct * 4 | from courses 5 | ) temp 6 | group by class 7 | having count(student)>=5 -------------------------------------------------------------------------------- /source/All_Solutions/0597.好友申请I:总体通过率/0597-好友申请I:总体通过率.sql: -------------------------------------------------------------------------------- 1 | select round( 2 | ifnull( 3 | (select count(distinct requester_id ,accepter_id) from request_accepted) / 4 | (select count(distinct sender_id ,send_to_id) from friend_request) 5 | ,0) 6 | ,2) as accept_rate ; -------------------------------------------------------------------------------- /source/All_Solutions/0598.范围求和II/0598-范围求和II.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int: 3 | return min(op[0] for op in ops) * min(op[1] for op in ops) if ops else m * n -------------------------------------------------------------------------------- /source/All_Solutions/0601.体育馆的人流量/0601-体育馆的人流量.sql: -------------------------------------------------------------------------------- 1 | select distinct a.* from stadium a,stadium b,stadium c 2 | where a.people>=100 and b.people>=100 and c.people>=100 3 | and ( 4 | (a.id = b.id-1 and b.id = c.id -1) or 5 | (a.id = b.id-1 and a.id = c.id +1) or 6 | (a.id = b.id+1 and b.id = c.id +1) 7 | ) order by a.id -------------------------------------------------------------------------------- /source/All_Solutions/0602.好友申请II:谁有最多的好友/0602-好友申请II:谁有最多的好友.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0602.好友申请II:谁有最多的好友/0602-好友申请II:谁有最多的好友.sql -------------------------------------------------------------------------------- /source/All_Solutions/0603.连续空余座位/0603-连续空余座位.sql: -------------------------------------------------------------------------------- 1 | select distinct(c1.seat_id) 2 | from cinema c1 join cinema c2 3 | on abs(c2.seat_id-c1.seat_id)=1 4 | where c1.free=1 and c2.free=1 5 | order by c1.seat_id -------------------------------------------------------------------------------- /source/All_Solutions/0606.根据二叉树创建字符串/0606-根据二叉树创建字符串.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def tree2str(self, t): 3 | if not t: 4 | return "" 5 | if not t.left and not t.right: 6 | return str(t.val) 7 | result = str(t.val) 8 | if t.left: 9 | result += "(" + self.tree2str(t.left) + ")" 10 | else: 11 | result += "()" 12 | if t.right: 13 | result += "(" + self.tree2str(t.right) + ")" 14 | return result -------------------------------------------------------------------------------- /source/All_Solutions/0607.销售员/0607-销售员.sql: -------------------------------------------------------------------------------- 1 | select s.name 2 | from salesperson s 3 | left outer join 4 | orders o 5 | on s.sales_id=o.sales_id 6 | left outer join 7 | company c 8 | on o.com_id=c.com_id and c.name='RED' 9 | group by s.name 10 | having count(c.name)=0; -------------------------------------------------------------------------------- /source/All_Solutions/0608.树节点/0608-树节点.sql: -------------------------------------------------------------------------------- 1 | select id, 2 | case when t.p_id is null then 'Root' 3 | when t.id in (select p_id from tree ) then 'Inner' 4 | else 'Leaf' 5 | end as Type 6 | from tree t -------------------------------------------------------------------------------- /source/All_Solutions/0609.在系统中查找重复文件/0609-在系统中查找重复文件.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDuplicate(self, paths: List[str]) -> List[List[str]]: 3 | d = collections.defaultdict(list) 4 | for path in paths: 5 | doc = path.split(' ') 6 | for file in doc[1: ]: 7 | name, content = file.split('(') 8 | d[hash(content)] += [doc[0] + '/' + name] 9 | return filter(lambda files: len(files) > 1, d.values()) -------------------------------------------------------------------------------- /source/All_Solutions/0610.判断三角形/0610-判断三角形.sql: -------------------------------------------------------------------------------- 1 | select 2 | * , 3 | ( 4 | case 5 | when x+y>z and x+z>y and y+z>x then 'Yes' 6 | else 'No' 7 | end 8 | )triangle 9 | from 10 | triangle -------------------------------------------------------------------------------- /source/All_Solutions/0611.有效三角形的个数/0611-有效三角形的个数.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0611.有效三角形的个数/0611-有效三角形的个数.py -------------------------------------------------------------------------------- /source/All_Solutions/0612.平面上的最近距离/0612-平面上的最近距离.sql: -------------------------------------------------------------------------------- 1 | select 2 | round(min(sqrt(pow(t1.x-t2.x,2)+pow(t1.y-t2.y,2))),2) shortest 3 | from point_2d as t1,point_2d as t2 4 | where (t1.x,t1.y) != (t2.x,t2.y) -------------------------------------------------------------------------------- /source/All_Solutions/0613.直线上的最近距离/0613-直线上的最近距离.sql: -------------------------------------------------------------------------------- 1 | SELECT min(abs(p1.x - p2.x)) shortest 2 | FROM point p1 inner join point p2 3 | ON p1.x != p2.x -------------------------------------------------------------------------------- /source/All_Solutions/0625.最小因式分解/0625-最小因式分解.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestFactorization(self, a: int) -> int: 3 | if a==1: 4 | return 1 5 | stack = [] 6 | for i in range(9,1,-1): 7 | while a % i == 0: 8 | a = a // i 9 | stack.append(i) 10 | if a != 1: 11 | return 0 12 | else: 13 | ans = 0 14 | while stack: 15 | ans = 10 * ans 16 | ans += stack.pop() 17 | return ans if ans < 2**31 -1 else 0 18 | 19 | -------------------------------------------------------------------------------- /source/All_Solutions/0627.交换工资/0627-交换工资.sql: -------------------------------------------------------------------------------- 1 | UPDATE salary 2 | SET 3 | sex = CASE sex 4 | WHEN 'm' THEN 'f' 5 | ELSE 'm' 6 | END; 7 | 8 | -------------------------------------------------------------------------------- /source/All_Solutions/0629.K个逆序对数组/0629-K个逆序对数组.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kInversePairs(self, n: int, k: int) -> int: 3 | mod = 10**9 + 7 4 | dp = [[0] * (k + 1) for i in range(n + 1)] 5 | dp[0][0] = 1 6 | for i in range(1, n + 1): 7 | dp[i][0] = 1 8 | for j in range(1, k + 1): 9 | if j>=i: 10 | dp[i][j]=(dp[i][j-1]+dp[i-1][j]-dp[i-1][j-i]) % mod 11 | else: 12 | dp[i][j]=(dp[i][j-1]+dp[i-1][j]) % mod 13 | return dp[n][k] -------------------------------------------------------------------------------- /source/All_Solutions/0630.课程表III/0630-课程表III.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0630.课程表III/0630-课程表III.py -------------------------------------------------------------------------------- /source/All_Solutions/0632.最小区间/0632-最小区间.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0632.最小区间/0632-最小区间.java -------------------------------------------------------------------------------- /source/All_Solutions/0634.寻找数组的错位排列/0634-寻找数组的错位排列.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findDerangement(int n) { 3 | if (n == 0) 4 | return 1; 5 | if (n == 1) 6 | return 0; 7 | int[] dp = new int[n + 1]; 8 | dp[0] = 1; 9 | dp[1] = 0; 10 | for (int i = 2; i <= n; i++) 11 | dp[i] = (int)(((i - 1L) * (dp[i - 1] + dp[i - 2])) % 1000000007); 12 | return dp[n]; 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /source/All_Solutions/0637.二叉树的层平均值/0637-二叉树的层平均值.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0637.二叉树的层平均值/0637-二叉树的层平均值.py -------------------------------------------------------------------------------- /source/All_Solutions/0638.大礼包/0638-大礼包.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0638.大礼包/0638-大礼包.py -------------------------------------------------------------------------------- /source/All_Solutions/0640.求解方程/0640-求解方程.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0640.求解方程/0640-求解方程.py -------------------------------------------------------------------------------- /source/All_Solutions/0645.错误的集合/0645-错误的集合.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findErrorNums(self, nums: List[int]) -> List[int]: 3 | S = sum(set(nums)) 4 | return [sum(nums)-S ,len(nums)*(len(nums)+1)//2-S] -------------------------------------------------------------------------------- /source/All_Solutions/0646.最长数对链/0646-最长数对链.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findLongestChain(self, pairs: List[List[int]]) -> int: 3 | pairs.sort(key=lambda x: x[1]) 4 | end = pairs[0][1] 5 | res = 1 6 | for i in range(1, len(pairs)): 7 | if pairs[i][0] > end: 8 | end = pairs[i][1] 9 | res += 1 10 | return res -------------------------------------------------------------------------------- /source/All_Solutions/0647.回文子串/0647-回文子串.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0647.回文子串/0647-回文子串.py -------------------------------------------------------------------------------- /source/All_Solutions/0648.单词替换/0648-单词替换.py: -------------------------------------------------------------------------------- 1 | import re 2 | class Solution: 3 | def replaceWords(self, dict, sentence): 4 | """ 5 | :type dict: List[str] 6 | :type sentence: str 7 | :rtype: str 8 | """ 9 | return re.sub(f"\\b({'|'.join(dict)})\w*", r'\1', sentence) -------------------------------------------------------------------------------- /source/All_Solutions/0649.Dota2参议院/0649-Dota2参议院.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def predictPartyVictory(self, senate): 3 | queue = collections.deque() 4 | people, bans = [0, 0], [0, 0] 5 | 6 | for person in senate: 7 | x = person == 'R' 8 | people[x] += 1 9 | queue.append(x) 10 | 11 | while all(people): 12 | x = queue.popleft() 13 | if bans[x]: 14 | bans[x] -= 1 15 | people[x] -= 1 16 | else: 17 | bans[x^1] += 1 18 | queue.append(x) 19 | 20 | return "Radiant" if people[1] else "Dire" 21 | -------------------------------------------------------------------------------- /source/All_Solutions/0650.只有两个键的键盘/0650-只有两个键的键盘.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def minSteps(self, n): 3 | ans = 0 4 | d = 2 5 | while n > 1: 6 | while n % d == 0: 7 | ans += d 8 | n /= d 9 | d += 1 10 | return ans 11 | 12 | -------------------------------------------------------------------------------- /source/All_Solutions/0653.两数之和IV-输入BST/0653-两数之和IV-输入BST.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0653.两数之和IV-输入BST/0653-两数之和IV-输入BST.py -------------------------------------------------------------------------------- /source/All_Solutions/0654.最大二叉树/0654-最大二叉树.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: 3 | if nums == []: return None 4 | max_num = max(nums) 5 | max_index = nums.index(max_num) 6 | root = TreeNode(max_num) 7 | root.left = self.constructMaximumBinaryTree(nums[0 : max_index]) 8 | root.right = self.constructMaximumBinaryTree(nums[max_index + 1 :]) 9 | return root 10 | 11 | -------------------------------------------------------------------------------- /source/All_Solutions/0655.输出二叉树/0655-输出二叉树.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0655.输出二叉树/0655-输出二叉树.py -------------------------------------------------------------------------------- /source/All_Solutions/0657.机器人能否返回原点/0657-机器人能否返回原点.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def judgeCircle(self, moves): 3 | """ 4 | :type moves: str 5 | :rtype: bool 6 | """ 7 | return moves.count('L') == moves.count('R') and moves.count('U') == moves.count('D') -------------------------------------------------------------------------------- /source/All_Solutions/0660.移除9/0660-移除9.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def newInteger(self, n: int) -> int: 3 | 4 | temp = '' 5 | 6 | while n > 0: 7 | 8 | temp+=str(n%9) 9 | 10 | n = n//9 11 | 12 | temp = temp[::-1] 13 | 14 | return(int(temp)) -------------------------------------------------------------------------------- /source/All_Solutions/0682.棒球比赛/0682-棒球比赛.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def calPoints(self, ops): 3 | scores = [] 4 | for i in ops: 5 | if i == '+': 6 | scores += [sum(scores[-2:])] 7 | elif i == 'D': 8 | scores += [scores[-1]*2] 9 | elif i == 'C': 10 | scores.pop() 11 | else: 12 | scores += [int(i)] 13 | return sum(scores) -------------------------------------------------------------------------------- /source/All_Solutions/0683.K个空花盆/0683-K个空花盆.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def kEmptySlots(self, flowers, k): 3 | days = [0] * len(flowers) 4 | for day, position in enumerate(flowers, 1): 5 | days[position - 1] = day 6 | 7 | ans = float('inf') 8 | left, right = 0, k+1 9 | while right < len(days): 10 | for i in xrange(left + 1, right): 11 | if days[i] < days[left] or days[i] < days[right]: 12 | left, right = i, i+k+1 13 | break 14 | else: 15 | ans = min(ans, max(days[left], days[right])) 16 | left, right = right, right+k+1 17 | 18 | return ans if ans < float('inf') else -1 19 | 20 | -------------------------------------------------------------------------------- /source/All_Solutions/0684.冗余连接/0684-冗余连接.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0684.冗余连接/0684-冗余连接.py -------------------------------------------------------------------------------- /source/All_Solutions/0685.冗余连接II/0685-冗余连接II.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0685.冗余连接II/0685-冗余连接II.py -------------------------------------------------------------------------------- /source/All_Solutions/0689.三个无重叠子数组的最大和/0689-三个无重叠子数组的最大和.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0689.三个无重叠子数组的最大和/0689-三个无重叠子数组的最大和.py -------------------------------------------------------------------------------- /source/All_Solutions/0690.员工的重要性/0690-员工的重要性.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0690.员工的重要性/0690-员工的重要性.py -------------------------------------------------------------------------------- /source/All_Solutions/0692.前K个高频单词/0692-前K个高频单词.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def topKFrequent(self, words, k): 3 | return [x[1] for x in heapq.nsmallest(k, [(v, k) for k, v in collections.Counter(words).items()], key=lambda a: (-a[0], a[1]))] 4 | -------------------------------------------------------------------------------- /source/All_Solutions/0693.交替位二进制数/0693-交替位二进制数.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def hasAlternatingBits(self, n): 3 | """ 4 | :type n: int 5 | :rtype: bool 6 | """ 7 | return not ('11' in str(bin(n)) or '00' in str(bin(n))) -------------------------------------------------------------------------------- /source/All_Solutions/0696.计数二进制子串/0696-计数二进制子串.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countBinarySubstrings(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | cur_length = 1 8 | pre_length = 0 9 | result = 0 10 | for i in range((len(s) - 1)): 11 | if s[i] == s[i + 1]: 12 | cur_length = cur_length + 1 13 | else: 14 | pre_length = cur_length 15 | cur_length = 1 16 | 17 | if pre_length >= cur_length: 18 | result = result + 1 19 | return result -------------------------------------------------------------------------------- /source/All_Solutions/0699.掉落的方块/0699-掉落的方块.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0699.掉落的方块/0699-掉落的方块.py -------------------------------------------------------------------------------- /source/All_Solutions/0702.搜索长度未知的有序数组/0702-搜索长度未知的有序数组.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0702.搜索长度未知的有序数组/0702-搜索长度未知的有序数组.py -------------------------------------------------------------------------------- /source/All_Solutions/0710.黑名单中的随机数/0710-黑名单中的随机数.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0710.黑名单中的随机数/0710-黑名单中的随机数.py -------------------------------------------------------------------------------- /source/All_Solutions/0713.乘积小于K的子数组/0713-乘积小于K的子数组.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def numSubarrayProductLessThanK(self, nums, k): 3 | if k == 0: return 0 4 | k = math.log(k) 5 | 6 | prefix = [0] 7 | for x in nums: 8 | prefix.append(prefix[-1] + math.log(x)) 9 | 10 | ans = 0 11 | for i, x in enumerate(prefix): 12 | j = bisect.bisect(prefix, x + k - 1e-9, i+1) 13 | ans += j - i - 1 14 | return ans 15 | 16 | -------------------------------------------------------------------------------- /source/All_Solutions/0714.买卖股票的最佳时机含手续费/0714-买卖股票的最佳时机含手续费.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0714.买卖股票的最佳时机含手续费/0714-买卖股票的最佳时机含手续费.py -------------------------------------------------------------------------------- /source/All_Solutions/0717.1比特与2比特字符/0717-1比特与2比特字符.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isOneBitCharacter(self, bits: List[int]) -> bool: 3 | i = 0 4 | while (i= len(bits): 10 | return False 11 | else: 12 | return True -------------------------------------------------------------------------------- /source/All_Solutions/0718.最长重复子数组/0718-最长重复子数组.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findLength(self, A: List[int], B: List[int]) -> int: 3 | l1 = len(A) 4 | l2 = len(B) 5 | dp = [[0 for _ in range(l2+1)] for _ in range(l1+1)] 6 | for i in range(1,l1+1): 7 | for j in range(1,l2+1): 8 | if A[i-1] == B[j-1]: 9 | dp[i][j] = dp[i-1][j-1] + 1 10 | return max(max(row) for row in dp) -------------------------------------------------------------------------------- /source/All_Solutions/0720.词典中最长的单词/0720-词典中最长的单词.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def longestWord(self, words): 3 | words.sort() 4 | words.sort(key=len, reverse=True) 5 | 6 | words_set = set(words) 7 | 8 | for w in words: 9 | flag = False 10 | for i in range(1, len(w)+1): 11 | if w[:i] not in words_set: 12 | flag = False 13 | break 14 | else: 15 | flag = True 16 | if flag: 17 | return w 18 | return '' -------------------------------------------------------------------------------- /source/All_Solutions/0725.分隔链表/0725-分隔链表.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0725.分隔链表/0725-分隔链表.py -------------------------------------------------------------------------------- /source/All_Solutions/0765.情侣牵手/0765-情侣牵手.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0765.情侣牵手/0765-情侣牵手.py -------------------------------------------------------------------------------- /source/All_Solutions/0766.托普利茨矩阵/0766-托普利茨矩阵.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool: 3 | col_len = len(matrix) #3 4 | row_len = len(matrix[0]) #4 5 | if col_len == 1 or row_len == 1: 6 | return True 7 | for i in range(len(matrix) - 1): 8 | if matrix[i][:-1] != matrix[i + 1][1:]: 9 | return False 10 | return True -------------------------------------------------------------------------------- /source/All_Solutions/0767.重构字符串/0767-重构字符串.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reorganizeString(self, S: str) -> str: 3 | a = list(S) 4 | b = dict(collections.Counter(a)) 5 | c = sorted(b,key = lambda k: 0-b[k]) 6 | d = [] 7 | for i in c: 8 | d += [i]*b[i] 9 | ans = [0]*len(a) 10 | ans[::2] = d[:len(ans[::2])] 11 | ans[1::2] = d[len(ans[::2])::] 12 | if ans[0] == ans[1]: 13 | return "" 14 | else: 15 | ans_str = '' 16 | for i in ans: 17 | ans_str +=i 18 | return ans_str -------------------------------------------------------------------------------- /source/All_Solutions/0768.最多能完成排序的块II/0768-最多能完成排序的块II.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxChunksToSorted(self, arr: List[int]) -> int: 3 | arr = zip(arr, range(len(arr))) 4 | arr = sorted(arr) 5 | arrs = [0]*len(arr) 6 | for idx,i in enumerate(arr): 7 | arrs[i[1]] = idx 8 | m = -float('inf') 9 | res = 0 10 | for i in range(len(arrs)): 11 | m = max(m, arrs[i]) 12 | if m == i: 13 | res += 1 14 | return res -------------------------------------------------------------------------------- /source/All_Solutions/0769.最多能完成排序的块/0769-最多能完成排序的块.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxChunksToSorted(self, arr: List[int]) -> int: 3 | res, max_val = 0, arr[0] 4 | for i, num in enumerate(arr): 5 | if num > max_val: 6 | max_val = num 7 | if max_val == i: 8 | res += 1 9 | return res -------------------------------------------------------------------------------- /source/All_Solutions/0773.滑动谜题/0773-滑动谜题.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0773.滑动谜题/0773-滑动谜题.py -------------------------------------------------------------------------------- /source/All_Solutions/0775.全局倒置与局部倒置/0775-全局倒置与局部倒置.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isIdealPermutation(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: bool 6 | """ 7 | for i in range(len(A)): 8 | if A[i] != i and abs(A[i] - i) > 1: 9 | return False 10 | return True -------------------------------------------------------------------------------- /source/All_Solutions/0776.拆分二叉搜索树/0776-拆分二叉搜索树.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def splitBST(self, root: TreeNode, V: int) -> List[TreeNode]: 3 | if not root:return [None,None] 4 | if root.val>V: 5 | left,right = self.splitBST(root.left,V) 6 | root.left = right 7 | return [left,root] 8 | left,right = self.splitBST(root.right,V) 9 | root.right = left 10 | return [root,right] -------------------------------------------------------------------------------- /source/All_Solutions/0778.水位上升的泳池中游泳/0778-水位上升的泳池中游泳.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0778.水位上升的泳池中游泳/0778-水位上升的泳池中游泳.py -------------------------------------------------------------------------------- /source/All_Solutions/0780.到达终点/0780-到达终点.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def reachingPoints(self, sx, sy, tx, ty): 3 | """ 4 | :type sx: int 5 | :type sy: int 6 | :type tx: int 7 | :type ty: int 8 | :rtype: bool 9 | """ 10 | if tx < sx or ty < sy: 11 | return False 12 | if tx == sx and (ty - sy) % sx == 0: 13 | return True 14 | if ty == sy and (tx - sx) % sy == 0: 15 | return True 16 | return self.reachingPoints(sx, sy, tx % ty, ty % tx) -------------------------------------------------------------------------------- /source/All_Solutions/0781.森林中的兔子/0781-森林中的兔子.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numRabbits(self, answers: List[int]) -> int: 3 | s = 0 4 | temp = {} 5 | for i in answers: 6 | if 0==i: 7 | s+=1 8 | elif temp.get(i): 9 | temp[i] += 1 10 | else: 11 | temp[i] = 1 12 | for i in temp.keys(): 13 | if temp[i] > i+1: 14 | s += temp[i]//(i+1)*(i+1)+i+1 if 0!=temp[i]%(i+1) else temp[i]//(i+1)*(i+1) 15 | else: 16 | s += i+1 17 | return s -------------------------------------------------------------------------------- /source/All_Solutions/0783.二叉搜索树结点最小距离/0783-二叉搜索树结点最小距离.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def __init__(self): 3 | self.minus = 2 ** 31 4 | self.pre = 2 ** 31 5 | 6 | def dfs(self,root): 7 | if not root: 8 | return 9 | 10 | self.dfs(root.right) 11 | self.minus = min(self.minus, self.pre - root.val) 12 | self.pre = root.val 13 | self.dfs(root.left) 14 | 15 | def minDiffInBST(self, root: TreeNode) -> int: 16 | self.dfs(root) 17 | 18 | 19 | return self.minus -------------------------------------------------------------------------------- /source/All_Solutions/0784.字母大小写全排列/0784-字母大小写全排列.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0784.字母大小写全排列/0784-字母大小写全排列.py -------------------------------------------------------------------------------- /source/All_Solutions/0785.判断二分图/0785-判断二分图.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0785.判断二分图/0785-判断二分图.py -------------------------------------------------------------------------------- /source/All_Solutions/0786.第K个最小的素数分数/0786-第K个最小的素数分数.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0786.第K个最小的素数分数/0786-第K个最小的素数分数.py -------------------------------------------------------------------------------- /source/All_Solutions/0788.旋转数字/0788-旋转数字.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotatedDigits(self, N: int) -> int: 3 | return len([i for i in range(1, N + 1) if not any([d for d in str(i) if int(d) in (3, 4, 7)]) and any([d for d in str(i) if int(d) in (2, 5, 6, 9)])]) -------------------------------------------------------------------------------- /source/All_Solutions/0789.逃脱阻碍者/0789-逃脱阻碍者.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool: 3 | return abs(target[0])+abs(target[1])0: 10 | res+=n//5 11 | n//=5 12 | return res 13 | 14 | low,up=0,5*10**9 15 | while up>low+1: 16 | mid=(low+up)//2 17 | tmp=f(mid) 18 | if K==tmp:return 5 19 | if K>tmp:low=mid 20 | if K bool: 3 | return len(A) == len(B) and B in A + A -------------------------------------------------------------------------------- /source/All_Solutions/0797.所有可能的路径/0797-所有可能的路径.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]: 3 | paths = [[0]] 4 | res = [] 5 | target = len(graph)-1 6 | while paths: 7 | newpaths = [] 8 | for i in paths: 9 | for j in graph[i[-1]]: 10 | if j == target: 11 | res.append(i+[target]) 12 | else: 13 | newpaths.append(i+[j]) 14 | paths = newpaths 15 | return res -------------------------------------------------------------------------------- /source/All_Solutions/0798.得分最高的最小轮调/0798-得分最高的最小轮调.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def bestRotation(self, A: List[int]) -> int: 3 | score = 0 4 | list1 = [0 for i in range(20005)] 5 | l = len(A) 6 | k=0 7 | for i in range(l): 8 | if A[i]<=i: 9 | score += 1 10 | list1[i-A[i]]+=1 11 | ss=score 12 | for i in range(1,l): 13 | x=list1.pop(0) 14 | list1.append(0) 15 | list1[l-1-A[0]]+=1 16 | A.append(A.pop(0)) 17 | ss=ss-x+1 18 | if score A[i - 1] and B[i] > B[i - 1]: 6 | if A[i] > B[i - 1] and B[i] > A[i - 1]: 7 | cost = [min(cost), min(cost) + 1] 8 | else: 9 | cost[1] += 1 10 | else: 11 | cost = [cost[1],cost[0] + 1] 12 | return min(cost) -------------------------------------------------------------------------------- /source/All_Solutions/0803.打砖块/0803-打砖块.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0803.打砖块/0803-打砖块.py -------------------------------------------------------------------------------- /source/All_Solutions/0804.唯一摩尔斯密码词/0804-唯一摩尔斯密码词.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uniqueMorseRepresentations(self, words): 3 | morse_dict = {'a': '.-', 'b': '-...', 'c': '-.-.', 'd': '-..', 'e': '.', 'f': '..-.', 'g': '--.', 'h': '....', 'i': '..', 'j': '.---', 'k': '-.-', 'l': '.-..', 'm': '--', 'n': '-.', 'o': '---', 'p': '.--.', 'q': '--.-', 'r': '.-.', 's': '...', 't': '-', 'u': '..-', 'v': '...-', 'w': '.--', 'x': '-..-', 'y': '-.--', 'z': '--..'} 4 | morse_rep = lambda word: ''.join([morse_dict[c] for c in word]) 5 | return len(set([morse_rep(word) for word in words])) -------------------------------------------------------------------------------- /source/All_Solutions/0806.写字符串需要的行数/0806-写字符串需要的行数.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfLines(self, widths: List[int], S: str) -> List[int]: 3 | total = 0 4 | resArr = [0] * 2 5 | for ch in S: 6 | if (total % 100 + widths[ord(ch) - 97]) > 100: 7 | total = total + 100 - (total % 100) + widths[ord(ch) - 97] 8 | else: 9 | total += widths[ord(ch) - 97] 10 | resArr[0] = total // 100 if total // 100 == total / \ 11 | 100 else total // 100 + 1 12 | resArr[1] = total % 100 13 | return resArr -------------------------------------------------------------------------------- /source/All_Solutions/0807.保持城市天际线/0807-保持城市天际线.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int: 3 | rowMax, colMax, n = [max(i) for i in grid], [max(i) for i in zip(*grid)], len(grid) 4 | return sum(min(rowMax[i], colMax[j]) - grid[i][j] for i in range(n) for j in range(n)) -------------------------------------------------------------------------------- /source/All_Solutions/0808.分汤/0808-分汤.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0808.分汤/0808-分汤.py -------------------------------------------------------------------------------- /source/All_Solutions/0810.黑板异或游戏/0810-黑板异或游戏.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def xorGame(self, nums: List[int]) -> bool: 3 | return not __import__('functools').reduce(operator.xor, nums) or not len(nums) & 1 -------------------------------------------------------------------------------- /source/All_Solutions/0899.有序队列/0899-有序队列.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def orderlyQueue(self, S: str, K: int) -> str: 3 | s = ''.join(sorted(S)) 4 | if K == 1: 5 | mi = S 6 | for i in range(len(S)): 7 | S = S[1:]+S[0] 8 | mi = min(mi,S) 9 | return mi 10 | else: 11 | return s -------------------------------------------------------------------------------- /source/All_Solutions/0901.股票价格跨度/0901-股票价格跨度.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0901.股票价格跨度/0901-股票价格跨度.py -------------------------------------------------------------------------------- /source/All_Solutions/0903.DI序列的有效排列/0903-DI序列的有效排列.py: -------------------------------------------------------------------------------- 1 | from functools import lru_cache 2 | 3 | class Solution: 4 | def numPermsDISequence(self, S): 5 | MOD = 10**9 + 7 6 | N = len(S) 7 | 8 | @lru_cache(None) 9 | def dp(i, j): 10 | # How many ways to place P_i with relative rank j? 11 | if i == 0: 12 | return 1 13 | elif S[i-1] == 'D': 14 | return sum(dp(i-1, k) for k in range(j, i)) % MOD 15 | else: 16 | return sum(dp(i-1, k) for k in range(j)) % MOD 17 | 18 | return sum(dp(N, j) for j in range(N+1)) % MOD 19 | 20 | -------------------------------------------------------------------------------- /source/All_Solutions/0904.水果成篮/0904-水果成篮.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0904.水果成篮/0904-水果成篮.py -------------------------------------------------------------------------------- /source/All_Solutions/0906.超级回文数/0906-超级回文数.py: -------------------------------------------------------------------------------- 1 | from itertools import product 2 | class Solution(object): 3 | def superpalindromesInRange(self, L, R): 4 | """ 5 | :type L: str 6 | :type R: str 7 | :rtype: int 8 | """ 9 | is_parlindrome = lambda string: string == string[::-1] 10 | l, r = int(L) ** 0.5, int(R) ** 0.5 11 | A, s = ["012"] * (((len(R) + 1) // 2 + 1) // 2), 0 12 | for a in product(*A): 13 | a = "".join(a).lstrip("0") or "0" 14 | for i in (int(a + a[::-1]), int(a + a[:-1][::-1])): 15 | s += (l <= i <= r and is_parlindrome(str(i ** 2))) 16 | return s + (int(L) <= 9 <= int(R)) -------------------------------------------------------------------------------- /source/All_Solutions/0908.最小差值I/0908-最小差值I.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestRangeI(self, A, K): 3 | """ 4 | :type A: List[int] 5 | :type K: int 6 | :rtype: int 7 | """ 8 | return max(max(A)-min(A)-2*K, 0) -------------------------------------------------------------------------------- /source/All_Solutions/0910.最小差值II/0910-最小差值II.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0910.最小差值II/0910-最小差值II.py -------------------------------------------------------------------------------- /source/All_Solutions/0915.分割数组/0915-分割数组.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def partitionDisjoint(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: int 6 | """ 7 | lmaxv = A[0] 8 | maxv = A[0] 9 | l = 0 10 | for i in xrange(len(A)): 11 | if A[i] < lmaxv: 12 | lmaxv = maxv 13 | l = i 14 | elif A[i] > maxv: 15 | maxv = A[i] 16 | return l + 1 -------------------------------------------------------------------------------- /source/All_Solutions/0916.单词子集/0916-单词子集.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def wordSubsets(self, A: List[str], B: List[str]) -> List[str]: 3 | from collections import Counter 4 | from functools import reduce 5 | cb=reduce(Counter.__or__,map(Counter,B)) 6 | def fun(a): 7 | ca=Counter(a) 8 | for k,v in cb.items(): 9 | if v>ca[k]: 10 | return False 11 | return True 12 | return filter(fun,A) -------------------------------------------------------------------------------- /source/All_Solutions/0918.环形子数组的最大和/0918-环形子数组的最大和.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0918.环形子数组的最大和/0918-环形子数组的最大和.py -------------------------------------------------------------------------------- /source/All_Solutions/0954.二倍数对数组/0954-二倍数对数组.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def canReorderDoubled(self, A): 3 | count = collections.Counter(A) 4 | for x in sorted(A, key = abs): 5 | if count[x] == 0: continue 6 | if count[2*x] == 0: return False 7 | count[x] -= 1 8 | count[2*x] -= 1 9 | 10 | return True 11 | 12 | -------------------------------------------------------------------------------- /source/All_Solutions/0956.最高的广告牌/0956-最高的广告牌.py: -------------------------------------------------------------------------------- 1 | from functools import lru_cache 2 | class Solution: 3 | def tallestBillboard(self, rods): 4 | @lru_cache(None) 5 | def dp(i, s): 6 | if i == len(rods): 7 | return 0 if s == 0 else float('-inf') 8 | return max(dp(i + 1, s), 9 | dp(i + 1, s - rods[i]), 10 | dp(i + 1, s + rods[i]) + rods[i]) 11 | 12 | return dp(0, 0) -------------------------------------------------------------------------------- /source/All_Solutions/0967.连续差相同的数字/0967-连续差相同的数字.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numsSameConsecDiff(self, N, K): 3 | """ 4 | :type N: int 5 | :type K: int 6 | :rtype: List[int] 7 | """ 8 | dp = set(range(10)) 9 | for i in range(1, N): 10 | prev_dp = dp 11 | dp = set() 12 | for num in prev_dp: 13 | if num == 0: 14 | continue 15 | left = num % 10 16 | if left >= K: 17 | dp.add(num*10+left-K) 18 | if left + K <= 9: 19 | dp.add(num*10+left+K) 20 | return sorted(list(dp)) -------------------------------------------------------------------------------- /source/All_Solutions/0968.监控二叉树/0968-监控二叉树.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0968.监控二叉树/0968-监控二叉树.py -------------------------------------------------------------------------------- /source/All_Solutions/0969.煎饼排序/0969-煎饼排序.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def pancakeSort(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: List[int] 6 | """ 7 | res = [] 8 | n = len(A) 9 | while n : 10 | idx = A.index(n) 11 | res.append(idx+1) 12 | A = A[:idx+1][::-1]+A[idx+1:] 13 | res.append(n) 14 | A = A[:n][::-1]+A[n:] 15 | n -= 1 16 | # print(A) 17 | return res -------------------------------------------------------------------------------- /source/All_Solutions/0971.翻转二叉树以匹配先序遍历/0971-翻转二叉树以匹配先序遍历.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def flipMatchVoyage(self, root: TreeNode, voyage: List[int]) -> List[int]: 3 | ans, flag = [], True 4 | def f(r): 5 | nonlocal ans, flag 6 | if r and flag: 7 | if not voyage or r.val != voyage.pop(0): 8 | flag = False 9 | elif r.left and r.right and r.left.val != voyage[0]: 10 | ans += [r.val] 11 | f(r.right); f(r.left) 12 | else: 13 | f(r.left); f(r.right) 14 | f(root) 15 | return ans if flag else [-1] -------------------------------------------------------------------------------- /source/All_Solutions/0972.相等的有理数/0972-相等的有理数.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isRationalEqual(self, S, T): 3 | """ 4 | :type S: str 5 | :type T: str 6 | :rtype: bool 7 | """ 8 | s=self.handle(S) 9 | t = self.handle(T) 10 | return abs(float(s)-float(t))<0.00000001 11 | def handle(self,s): 12 | p = s.find('(') 13 | if p!=-1: 14 | cycle = s[p+1:-1] 15 | s = s[:p]+cycle*15 16 | return s -------------------------------------------------------------------------------- /source/All_Solutions/0973.最接近原点的K个点/0973-最接近原点的K个点.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kClosest(self, points, K) : 3 | a = lambda x :x[0]**2 + x[1]**2 4 | points.sort(key = a) 5 | return points[:K] -------------------------------------------------------------------------------- /source/All_Solutions/0974.和可被K整除的子数组/0974-和可被K整除的子数组.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0974.和可被K整除的子数组/0974-和可被K整除的子数组.py -------------------------------------------------------------------------------- /source/All_Solutions/0978.最长湍流子数组/0978-最长湍流子数组.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxTurbulenceSize(self, A): 3 | N = len(A) 4 | ans = 1 5 | anchor = 0 6 | 7 | for i in xrange(1, N): 8 | c = cmp(A[i-1], A[i]) 9 | if i == N-1 or c * cmp(A[i], A[i+1]) != -1: 10 | if c != 0: 11 | ans = max(ans, i - anchor + 1) 12 | anchor = i 13 | return ans 14 | 15 | -------------------------------------------------------------------------------- /source/All_Solutions/0979.在二叉树中分配硬币/0979-在二叉树中分配硬币.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def distributeCoins(self, root): 3 | self.ans = 0 4 | 5 | def dfs(node): 6 | if not node: return 0 7 | L, R = dfs(node.left), dfs(node.right) 8 | self.ans += abs(L) + abs(R) 9 | return node.val + L + R - 1 10 | 11 | dfs(root) 12 | return self.ans 13 | 14 | -------------------------------------------------------------------------------- /source/All_Solutions/0980.不同路径III/0980-不同路径III.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0980.不同路径III/0980-不同路径III.py -------------------------------------------------------------------------------- /source/All_Solutions/0981.基于时间的键值存储/0981-基于时间的键值存储.py: -------------------------------------------------------------------------------- 1 | class TimeMap(object): 2 | def __init__(self): 3 | self.M = collections.defaultdict(list) 4 | 5 | def set(self, key, value, timestamp): 6 | self.M[key].append((timestamp, value)) 7 | 8 | def get(self, key, timestamp): 9 | A = self.M.get(key, None) 10 | if A is None: return "" 11 | i = bisect.bisect(A, (timestamp, chr(127))) 12 | return A[i-1][1] if i else "" 13 | -------------------------------------------------------------------------------- /source/All_Solutions/0982.按位与为零的三元组/0982-按位与为零的三元组.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | 4 | class Solution: 5 | def countTriplets(self, A: List[int]) -> int: 6 | mem = defaultdict(int) 7 | for n1 in A: 8 | for n2 in A: 9 | mem[n1 & n2] += 1 10 | ans = 0 11 | for num in A: 12 | for key, val in mem.items(): 13 | if num & key == 0: 14 | ans += val 15 | return ans 16 | -------------------------------------------------------------------------------- /source/All_Solutions/0983.最低票价/0983-最低票价.py: -------------------------------------------------------------------------------- 1 | from functools import lru_cache 2 | 3 | class Solution: 4 | def mincostTickets(self, days, costs): 5 | dayset = set(days) 6 | durations = [1, 7, 30] 7 | 8 | @lru_cache(None) 9 | def dp(i): 10 | if i > 365: 11 | return 0 12 | elif i in dayset: 13 | return min(dp(i + d) + c 14 | for c, d in zip(costs, durations)) 15 | else: 16 | return dp(i + 1) 17 | 18 | return dp(1) 19 | 20 | -------------------------------------------------------------------------------- /source/All_Solutions/0984.不含AAA或BBB的字符串/0984-不含AAA或BBB的字符串.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def strWithout3a3b(self, A, B): 3 | ans = [] 4 | 5 | while A or B: 6 | if len(ans) >= 2 and ans[-1] == ans[-2]: 7 | writeA = ans[-1] == 'b' 8 | else: 9 | writeA = A >= B 10 | 11 | if writeA: 12 | A -= 1 13 | ans.append('a') 14 | else: 15 | B -= 1 16 | ans.append('b') 17 | 18 | return "".join(ans) 19 | -------------------------------------------------------------------------------- /source/All_Solutions/0985.查询后的偶数和/0985-查询后的偶数和.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def sumEvenAfterQueries(self, A, queries): 3 | S = sum(x for x in A if x % 2 == 0) 4 | ans = [] 5 | 6 | for x, k in queries: 7 | if A[k] % 2 == 0: S -= A[k] 8 | A[k] += x 9 | if A[k] % 2 == 0: S += A[k] 10 | ans.append(S) 11 | 12 | return ans 13 | 14 | -------------------------------------------------------------------------------- /source/All_Solutions/0986.区间列表的交集/0986-区间列表的交集.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intervalIntersection(self, A: List[List[int]], B: List[List[int]]) -> List[List[int]]: 3 | m, n = len(A), len(B) 4 | ans = [] 5 | i, j = 0, 0 6 | while i < m and j < n: 7 | l, r = max(A[i][0], B[j][0]), min(A[i][1], B[j][1]) 8 | if l <= r: 9 | ans += [[l, r]] 10 | if A[i][1] < B[j][1]: 11 | i += 1 12 | else: 13 | j += 1 14 | return ans -------------------------------------------------------------------------------- /source/All_Solutions/0987.二叉树的垂序遍历/0987-二叉树的垂序遍历.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0987.二叉树的垂序遍历/0987-二叉树的垂序遍历.py -------------------------------------------------------------------------------- /source/All_Solutions/0988.从叶结点开始的最小字符串/0988-从叶结点开始的最小字符串.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def smallestFromLeaf(self, root): 3 | self.ans = "~" 4 | 5 | def dfs(node, A): 6 | if node: 7 | A.append(chr(node.val + ord('a'))) 8 | if not node.left and not node.right: 9 | self.ans = min(self.ans, "".join(reversed(A))) 10 | 11 | dfs(node.left, A) 12 | dfs(node.right, A) 13 | A.pop() 14 | 15 | dfs(root, []) 16 | return self.ans 17 | 18 | -------------------------------------------------------------------------------- /source/All_Solutions/0990.等式方程的可满足性/0990-等式方程的可满足性.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def equationsPossible(self, equations: List[str]) -> bool: 3 | f = {} 4 | def find(x): 5 | f.setdefault(x, x) 6 | if f[x] != x: 7 | f[x] = find(f[x]) 8 | return f[x] 9 | def union(x, y): 10 | f[find(y)] = find(x) 11 | for s in equations: 12 | if s[1]=="=": 13 | union(s[0],s[-1]) 14 | for s in equations: 15 | if s[1]=="!": 16 | if find(s[0])==find(s[-1]): 17 | return False 18 | return True -------------------------------------------------------------------------------- /source/All_Solutions/0991.坏了的计算器/0991-坏了的计算器.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0991.坏了的计算器/0991-坏了的计算器.py -------------------------------------------------------------------------------- /source/All_Solutions/0992.K个不同整数的子数组/0992-K个不同整数的子数组.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def subarraysWithKDistinct(self, A, K): 3 | B = [0] * (max(A) + 1) 4 | l = j = res = 0 5 | c = 1 6 | for i, n in enumerate(A): 7 | #print l,c,res,B 8 | if B[n] == 0: 9 | l += 1 10 | if l > K: 11 | c = 1 12 | B[A[j]] -= 1 13 | j += 1 14 | l -= 1 15 | B[n] += 1 16 | while B[A[j]] != 1: 17 | B[A[j]] -= 1 18 | c += 1 19 | j += 1 20 | if l == K: 21 | res += c 22 | return res -------------------------------------------------------------------------------- /source/All_Solutions/0993.二叉树的堂兄弟节点/0993-二叉树的堂兄弟节点.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0993.二叉树的堂兄弟节点/0993-二叉树的堂兄弟节点.py -------------------------------------------------------------------------------- /source/All_Solutions/0994.腐烂的橘子/0994-腐烂的橘子.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0994.腐烂的橘子/0994-腐烂的橘子.py -------------------------------------------------------------------------------- /source/All_Solutions/0995.K连续位的最小翻转次数/0995-K连续位的最小翻转次数.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minKBitFlips(self, A: 'List[int]', K: 'int') -> 'int': 3 | ct=0 4 | n = len(A) 5 | flag = [0]*n 6 | sm = 0 7 | for i in range(n-K+1): 8 | if (sm+A[i])%2==0: 9 | ct+=1 10 | flag[i]=1 11 | sm+=flag[i] 12 | if i-K+1>=0: 13 | sm-=flag[i-K+1] 14 | for i in range(n-K+1,n): 15 | if (sm+A[i])%2==0:return -1 16 | if i-K+1>=0: 17 | sm-=flag[i-K+1] 18 | return ct -------------------------------------------------------------------------------- /source/All_Solutions/0997.找到小镇的法官/0997-找到小镇的法官.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/0997.找到小镇的法官/0997-找到小镇的法官.py -------------------------------------------------------------------------------- /source/All_Solutions/0998.最大二叉树II/0998-最大二叉树II.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode: 3 | new = TreeNode(val) 4 | pre = None 5 | node = root 6 | while node!= None and node.val > val: 7 | pre = node 8 | node = node.right 9 | if pre == None: 10 | new.left = node 11 | return new 12 | elif node == None: 13 | pre.right = new 14 | else: 15 | new.left = node 16 | pre.right = new 17 | return root -------------------------------------------------------------------------------- /source/All_Solutions/1016.子串能表示从1到N数字的二进制串/1016-子串能表示从1到N数字的二进制串.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def queryString(self, S: str, N: int) -> bool: 3 | return all(map(lambda i: bin(i)[2:] in S, range(1, N + 1))) -------------------------------------------------------------------------------- /source/All_Solutions/1017.负二进制转换/1017-负二进制转换.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def baseNeg2(self, N: int) -> str: 3 | def function(n): 4 | result="" 5 | while True: 6 | result=str(n%2)+result 7 | n=math.ceil(n/-2) 8 | if n==0: 9 | break 10 | return result 11 | return function(N) -------------------------------------------------------------------------------- /source/All_Solutions/1019.链表中的下一个更大节点/1019-链表中的下一个更大节点.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def nextLargerNodes(self, head): 3 | """ 4 | :type head: ListNode 5 | :rtype: List[int] 6 | """ 7 | if not head: 8 | return [] 9 | nums = [] 10 | cur = head 11 | while cur: 12 | nums.append(cur.val) 13 | cur=cur.next 14 | stack = [0] 15 | res = [0]*len(nums) 16 | for i in range(1,len(nums)): 17 | while stack and nums[i]>nums[stack[-1]]: 18 | res[stack[-1]]=nums[i] 19 | stack.pop() 20 | stack.append(i) 21 | return res -------------------------------------------------------------------------------- /source/All_Solutions/1022.从根到叶的二进制数之和/1022-从根到叶的二进制数之和.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1022.从根到叶的二进制数之和/1022-从根到叶的二进制数之和.py -------------------------------------------------------------------------------- /source/All_Solutions/1023.驼峰式匹配/1023-驼峰式匹配.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def camelMatch(self, queries: List[str], pattern: str) -> List[bool]: 3 | r = re.compile(f'{"[a-z]*".join(["", *pattern, ""])}$') 4 | return map(r.match, queries) -------------------------------------------------------------------------------- /source/All_Solutions/1024.视频拼接/1024-视频拼接.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def videoStitching(self, clips: List[List[int]], T: int) -> int: 3 | dp = [T + 1] * (T + 1) 4 | dp[0] = 0 5 | for i in range(0, T+1): 6 | for c in clips: 7 | if c[0] <= i and c[1] >= i: 8 | dp[i] = min(dp[i], dp[c[0]]+1) 9 | return -1 if dp[T] == T + 1 else dp[T] 10 | -------------------------------------------------------------------------------- /source/All_Solutions/1026.节点与其祖先之间的最大差值/1026-节点与其祖先之间的最大差值.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxAncestorDiff(self, root: TreeNode) -> int: 3 | def helper(root,ma=0,mi=100000): 4 | if not root: 5 | return ma-mi 6 | return max(helper(root.left,max(ma,root.val),min(mi,root.val)),helper(root.right,max(ma,root.val),min(mi,root.val))) 7 | return helper(root) -------------------------------------------------------------------------------- /source/All_Solutions/1027.最长等差数列/1027-最长等差数列.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestArithSeqLength(self, A: List[int]) -> int: 3 | def function(s): 4 | result={} 5 | maxv=2 6 | for i in range(1,len(s)): 7 | for j in range(i): 8 | if (s[j]-s[i],j) not in result: 9 | result[(s[j]-s[i],i)]=2 10 | else: 11 | result[(s[j]-s[i],i)]=result[(s[j]-s[i],j)]+1 12 | if result[(s[j]-s[i],i)]>maxv: 13 | maxv=result[(s[j]-s[i],i)] 14 | return maxv 15 | return function(A) -------------------------------------------------------------------------------- /source/All_Solutions/1028.从先序遍历还原二叉树/1028-从先序遍历还原二叉树.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1028.从先序遍历还原二叉树/1028-从先序遍历还原二叉树.py -------------------------------------------------------------------------------- /source/All_Solutions/1029.两地调度/1029-两地调度.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoCitySchedCost(self, costs: List[List[int]]) -> int: 3 | costs.sort(key=lambda x: abs(x[1] - x[0]), reverse=True) 4 | total = 0 5 | left = 0 6 | right = 0 7 | for i in range(len(costs)): 8 | if left >= len(costs) // 2: 9 | total += costs[i][1] 10 | elif right >= len(costs) // 2: 11 | total += costs[i][0] 12 | elif costs[i][0] > costs[i][1]: 13 | total += costs[i][1] 14 | right += 1 15 | else: 16 | total += costs[i][0] 17 | left += 1 18 | return total -------------------------------------------------------------------------------- /source/All_Solutions/1030.距离顺序排列矩阵单元格/1030-距离顺序排列矩阵单元格.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def allCellsDistOrder(self, R: int, C: int, r0: int, c0: int) -> List[List[int]]: 3 | res=[] 4 | for i in range(R): 5 | for j in range(C): 6 | res.append([abs(r0-i)+abs(c0-j),[i,j]]) 7 | res=sorted(res,key=lambda x:x[0]) 8 | ans=[] 9 | for i in res: 10 | ans.append(i[1]) 11 | return ans -------------------------------------------------------------------------------- /source/All_Solutions/1031.两个非重叠子数组的最大和/1031-两个非重叠子数组的最大和.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1031.两个非重叠子数组的最大和/1031-两个非重叠子数组的最大和.py -------------------------------------------------------------------------------- /source/All_Solutions/1034.边框着色/1034-边框着色.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def colorBorder(self, grid: List[List[int]], r0: int, c0: int, color: int) -> List[List[int]]: 3 | row, col = len(grid), len(grid[0]) 4 | border,visit = set(),set() 5 | 6 | def dfs(r, c): 7 | if not (0 <= r < row and 0 <= c < col and grid[r][c] == grid[r0][c0]):return False 8 | if (r, c) in visit:return True 9 | visit.add((r,c)) 10 | if dfs(r + 1, c) + dfs(r - 1, c) + dfs(r, c + 1) + dfs(r, c - 1) < 4:border.add((r, c)) 11 | return True 12 | 13 | dfs(r0,c0) 14 | for (x,y) in border:grid[x][y]=color 15 | return grid -------------------------------------------------------------------------------- /source/All_Solutions/1035.不相交的线/1035-不相交的线.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: 3 | n = len(A) 4 | m = len(B) 5 | if n * m == 0: 6 | return n + m 7 | d = [[0] * (m + 1) for _ in range(n + 1)] 8 | # DP compute 9 | for i in range(1, n + 1): 10 | for j in range(1, m + 1): 11 | if A[i - 1] == B[j - 1]: 12 | d[i][j] = d[i-1][j-1] +1 13 | else: 14 | d[i][j] = max(d[i-1][j],d[i][j-1], d[i-1][j-1]) 15 | 16 | return d[n][m] -------------------------------------------------------------------------------- /source/All_Solutions/1037.有效的回旋镖/1037-有效的回旋镖.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1037.有效的回旋镖/1037-有效的回旋镖.py -------------------------------------------------------------------------------- /source/All_Solutions/1038.从二叉搜索树到更大和树/1038-从二叉搜索树到更大和树.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def bstToGst(self, root: TreeNode) -> TreeNode: 3 | def postOrder(nd, acc=0)->int: 4 | if not nd:return acc 5 | nd.val += postOrder(nd.right,acc) 6 | return postOrder(nd.left,nd.val) 7 | 8 | postOrder(root) 9 | return root -------------------------------------------------------------------------------- /source/All_Solutions/1039.多边形三角剖分的最低得分/1039-多边形三角剖分的最低得分.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minScoreTriangulation(self, A: List[int]) -> int: 3 | dp = [[float("inf")] * len(A) for _ in range(len(A))] 4 | for i in range(len(A)-1): 5 | dp[i][i+1] = 0 6 | for d in range(2,len(A)): 7 | for i in range(0, len(A)-d): 8 | j = i+d 9 | for k in range(i+1,j): 10 | dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + A[i] * A[j] * A[k]) 11 | return dp[0][len(A)-1] -------------------------------------------------------------------------------- /source/All_Solutions/1040.移动石子直到连续II/1040-移动石子直到连续II.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1040.移动石子直到连续II/1040-移动石子直到连续II.py -------------------------------------------------------------------------------- /source/All_Solutions/1041.困于环中的机器人/1041-困于环中的机器人.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1041.困于环中的机器人/1041-困于环中的机器人.py -------------------------------------------------------------------------------- /source/All_Solutions/1042.不邻接植花/1042-不邻接植花.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1042.不邻接植花/1042-不邻接植花.py -------------------------------------------------------------------------------- /source/All_Solutions/1046.最后一块石头的重量/1046-最后一块石头的重量.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lastStoneWeight(self, stones: List[int]) -> int: 3 | while len(stones)>=2: 4 | y=stones.pop(stones.index(max(stones))) 5 | x=stones.pop(stones.index(max(stones))) 6 | if x==y: 7 | continue 8 | else: 9 | stones.append(y-x) 10 | if len(stones)==0: 11 | return 0 12 | return stones[0] -------------------------------------------------------------------------------- /source/All_Solutions/1047.删除字符串中的所有相邻重复项/1047-删除字符串中的所有相邻重复项.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1047.删除字符串中的所有相邻重复项/1047-删除字符串中的所有相邻重复项.py -------------------------------------------------------------------------------- /source/All_Solutions/1048.最长字符串链/1048-最长字符串链.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestStrChain(self, words: List[str]) -> int: 3 | dp = {} 4 | for w in sorted(words, key=len): 5 | dp[w] = max(dp.get(w[:i] + w[i + 1:], 0) + 1 for i in range(len(w))) 6 | return max(dp.values()) -------------------------------------------------------------------------------- /source/All_Solutions/1049.最后一块石头的重量II/1049-最后一块石头的重量II.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lastStoneWeightII(self, stones: List[int]) -> int: 3 | target = sum(stones)/2.0 4 | candidates = {0} 5 | for x in stones: 6 | addition = set() 7 | for y in candidates: 8 | if x+y<= target: 9 | addition.add(x+y) 10 | candidates = candidates.union(addition) 11 | return int(2*(target-max(candidates))) 12 | 13 | -------------------------------------------------------------------------------- /source/All_Solutions/1051.高度检查器/1051-高度检查器.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def heightChecker(self, heights: List[int]) -> int: 3 | return sum(h1 != h2 for h1, h2 in zip(heights, sorted(heights))) -------------------------------------------------------------------------------- /source/All_Solutions/1053.交换一次的先前排列/1053-交换一次的先前排列.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1053.交换一次的先前排列/1053-交换一次的先前排列.py -------------------------------------------------------------------------------- /source/All_Solutions/1054.距离相等的条形码/1054-距离相等的条形码.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]: 3 | from collections import Counter 4 | data = [] 5 | for i, j in Counter(barcodes).most_common(): 6 | data += [i] * j 7 | l = len(data) 8 | ans = [0] * l 9 | ans[::2] = data[:(l+1)//2] 10 | ans[1::2] = data[(l+1)//2:] 11 | return ans -------------------------------------------------------------------------------- /source/All_Solutions/1056.易混淆数/1056-易混淆数.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def confusingNumber(self, N: int) -> bool: 3 | N = str(N) 4 | change = { 5 | '0': '0', 6 | '1': '1', 7 | '6': '9', 8 | '8': '8', 9 | '9': '6' 10 | } 11 | M = '' 12 | for x in N: 13 | if x not in change: 14 | return False 15 | M += change[x] 16 | return ''.join(reversed(N)) != M -------------------------------------------------------------------------------- /source/All_Solutions/1060.有序数组中的缺失元素/1060-有序数组中的缺失元素.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def missingElement(self, nums: List[int], k: int) -> int: 3 | for i in range(len(nums) - 1): 4 | bt = nums[i + 1] - nums[i] - 1 5 | if k > bt: 6 | k -= bt 7 | else: 8 | return nums[i] + k 9 | return nums[-1] + k -------------------------------------------------------------------------------- /source/All_Solutions/1110.删点成林/1110-删点成林.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1110.删点成林/1110-删点成林.py -------------------------------------------------------------------------------- /source/All_Solutions/1111.有效括号的嵌套深度/1111-有效括号的嵌套深度.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxDepthAfterSplit(self, seq: str) -> List[int]: 3 | d = 0 4 | dlst = [] 5 | for i in seq: 6 | if i == '(': 7 | dlst.append(d) 8 | d += 1 9 | else: 10 | d -= 1 11 | dlst.append(d) 12 | return [i%2 for i in dlst] -------------------------------------------------------------------------------- /source/All_Solutions/1120.子树的最大平均值/1120-子树的最大平均值.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumAverageSubtree(self, root: TreeNode) -> float: 3 | ans = float('-inf') 4 | 5 | def dfs(node): 6 | nonlocal ans 7 | if not node: 8 | return 0, 0 9 | left_sum, left_count = dfs(node.left) 10 | right_sum, right_count = dfs(node.right) 11 | cur_sum = left_sum + right_sum + node.val 12 | cur_count = left_count + right_count + 1 13 | ans = max(ans, cur_sum / cur_count) 14 | return cur_sum, cur_count 15 | 16 | dfs(root) 17 | return ans 18 | 19 | -------------------------------------------------------------------------------- /source/All_Solutions/1121.将数组分成几个递增序列/1121-将数组分成几个递增序列.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def canDivideIntoSubsequences(self, nums, K): 3 | """ 4 | :type nums: List[int] 5 | :type K: int 6 | :rtype: bool 7 | """ 8 | from collections import Counter 9 | return max(Counter(nums).values()) * K <= len(nums) 10 | -------------------------------------------------------------------------------- /source/All_Solutions/1123.最深叶节点的最近公共祖先/1123-最深叶节点的最近公共祖先.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lcaDeepestLeaves(self, root: TreeNode) -> TreeNode: 3 | if root == None: 4 | return None 5 | left = self.getHigh(root.left) 6 | right = self.getHigh(root.right) 7 | if left == right: 8 | return root 9 | elif left < right: 10 | return self.lcaDeepestLeaves(root.right) 11 | else: 12 | return self.lcaDeepestLeaves(root.left) 13 | 14 | def getHigh(self, root: TreeNode) -> int: 15 | if root == None: 16 | return 0 17 | return 1 + max(self.getHigh(root.left), self.getHigh(root.right)) -------------------------------------------------------------------------------- /source/All_Solutions/1124.表现良好的最长时间段/1124-表现良好的最长时间段.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1124.表现良好的最长时间段/1124-表现良好的最长时间段.py -------------------------------------------------------------------------------- /source/All_Solutions/1125.最小的必要团队/1125-最小的必要团队.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1125.最小的必要团队/1125-最小的必要团队.py -------------------------------------------------------------------------------- /source/All_Solutions/1128.等价多米诺骨牌对的数量/1128-等价多米诺骨牌对的数量.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def numEquivDominoPairs(self, dominoes): 3 | """ 4 | :type dominoes: List[List[int]] 5 | :rtype: int 6 | """ 7 | m = {} 8 | 9 | for a,b in dominoes: 10 | if a > b: 11 | a,b = b,a 12 | if (a,b) not in m: 13 | m[(a,b)] = 0 14 | m[(a,b)] += 1 15 | 16 | count = 0 17 | 18 | for i in m.values(): 19 | if i > 1: 20 | count += i*(i-1)//2 21 | 22 | return count -------------------------------------------------------------------------------- /source/All_Solutions/1130.叶值的最小代价生成树/1130-叶值的最小代价生成树.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mctFromLeafValues(self, arr: List[int]) -> int: 3 | n = len(arr) 4 | dp=[[0]*n for i in range(n)] 5 | mx=[[0]*n for i in range(n)] 6 | for i in range(n): 7 | mx[i][i]=arr[i] 8 | for l in range(1,n): 9 | for i in range(n-l): 10 | j = i+l 11 | for k in range(i,j): 12 | tmp = dp[i][k]+dp[k+1][j]+mx[i][k]*mx[k+1][j] 13 | if dp[i][j] == 0 or dp[i][j]>tmp: 14 | dp[i][j]=tmp 15 | mx[i][j] = max(mx[i][k],mx[k+1][j],mx[i][j]) 16 | return dp[0][n-1] 17 | -------------------------------------------------------------------------------- /source/All_Solutions/1131.绝对值表达式的最大值/1131-绝对值表达式的最大值.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxAbsValExpr(self, arr1: List[int], arr2: List[int]) -> int: 3 | n=len(arr1) 4 | d=[[],[],[],[]] 5 | b=[[1,1],[1,-1],[-1,1],[-1,-1]] 6 | for i in range(n): 7 | for j in range(4): 8 | d[j]+=[arr1[i]+b[j][0]*arr2[i]+b[j][1]*i] 9 | return max([max(d[i])-min(d[i]) for i in range(4)]) 10 | -------------------------------------------------------------------------------- /source/All_Solutions/1133.最大唯一数/1133-最大唯一数.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestUniqueNumber(self, A: List[int]) -> int: 3 | maxa=-1 4 | for i in range(0,len(A)): 5 | if maxa bool: 3 | return sum(map(lambda c: int(c) ** len(str(N)), str(N))) == N 4 | -------------------------------------------------------------------------------- /source/All_Solutions/1135.最低成本联通所有城市/1135-最低成本联通所有城市.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumCost(self, N: int, connections: List[List[int]]) -> int: 3 | p = [i for i in range(N + 1)] 4 | connections.sort(key = lambda x: x[2]) 5 | count = 0 6 | ans = 0 7 | def f(x): 8 | if p[x] != x: 9 | p[x] = f(p[x]) 10 | return p[x] 11 | for x, y, c in connections: 12 | px, py = f(x), f(y) 13 | if px != py: 14 | count += 1 15 | ans += c 16 | if count == N - 1: 17 | return ans 18 | p[px] = py 19 | return -1 -------------------------------------------------------------------------------- /source/All_Solutions/1148.文章浏览I/1148-文章浏览I.sql: -------------------------------------------------------------------------------- 1 | select distinct author_id as id from Views 2 | where author_id = viewer_id 3 | order by author_id asc -------------------------------------------------------------------------------- /source/All_Solutions/1175.质数排列/1175-质数排列.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1175.质数排列/1175-质数排列.py -------------------------------------------------------------------------------- /source/All_Solutions/1176.健身计划评估/1176-健身计划评估.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def dietPlanPerformance(self, calories, k, lower, upper): 3 | if k >= len(calories): 4 | s = sum(calories) 5 | if supper: return 1 7 | else: return 0 8 | p = k 9 | currS = sum(calories[0:p]) 10 | res = 0 11 | while p < len(calories): 12 | if currSupper: res+=1 14 | currS = currS-calories[p-k]+calories[p] 15 | p+=1 16 | if currSupper: res+=1 18 | return res -------------------------------------------------------------------------------- /source/All_Solutions/1178.猜字谜/1178-猜字谜.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1178.猜字谜/1178-猜字谜.py -------------------------------------------------------------------------------- /source/All_Solutions/1180.统计只含单一字母的子串/1180-统计只含单一字母的子串.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countLetters(self, S: str) -> int: 3 | if not S: 4 | return 0 5 | left = 0 6 | n = len(S) 7 | res = 0 8 | while left < n: 9 | com_alp = S[left] 10 | right = left 11 | while right < n and S[right] == com_alp: 12 | right += 1 13 | res += ((1 + right- left) * (right - left)) // 2 14 | left = right 15 | return res -------------------------------------------------------------------------------- /source/All_Solutions/1181.前后拼接/1181-前后拼接.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1181.前后拼接/1181-前后拼接.py -------------------------------------------------------------------------------- /source/All_Solutions/1182.与目标颜色间的最短距离/1182-与目标颜色间的最短距离.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1182.与目标颜色间的最短距离/1182-与目标颜色间的最短距离.py -------------------------------------------------------------------------------- /source/All_Solutions/1184.公交站间的距离/1184-公交站间的距离.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def distanceBetweenBusStops(self, distance, start, destination): 3 | sumdistance = sum(distance) 4 | res = 0 5 | if start > destination: 6 | start,destination = destination,start 7 | while start < destination: 8 | res += distance[start] 9 | start += 1 10 | return min(res,sumdistance-res) -------------------------------------------------------------------------------- /source/All_Solutions/1186.删除一次得到子数组最大和/1186-删除一次得到子数组最大和.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumSum(self, arr): 3 | sumre = [0]*(len(arr)) 4 | resre = [0]*len(arr) 5 | sumre[0] = arr[0] 6 | resre[0] = arr[0] 7 | res = arr[0] 8 | for i in range(1,len(arr)): 9 | sumre[i] = max(sumre[i-1]+arr[i],arr[i]) 10 | resre[i] = max(sumre[i-1],resre[i-1]+arr[i]) 11 | res = max(res,max(sumre[i],resre[i])) 12 | return res -------------------------------------------------------------------------------- /source/All_Solutions/1188.设计有限阻塞队列/1188-设计有限阻塞队列.py: -------------------------------------------------------------------------------- 1 | from queue import Queue 2 | class BoundedBlockingQueue(object): 3 | 4 | def __init__(self, capacity: int): 5 | self.q = Queue(capacity) 6 | 7 | 8 | def enqueue(self, element: int) -> None: 9 | self.q.put(element) 10 | 11 | 12 | def dequeue(self) -> int: 13 | return self.q.get() 14 | 15 | 16 | def size(self) -> int: 17 | return self.q.qsize() 18 | 19 | 20 | -------------------------------------------------------------------------------- /source/All_Solutions/1190.反转每对括号间的子串/1190-反转每对括号间的子串.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def reverseParentheses(self, s): 3 | while '(' in s: 4 | s = re.sub(r'\(([^()]*)\)', lambda x:x.group(1)[::-1], s) 5 | return s -------------------------------------------------------------------------------- /source/All_Solutions/1192.查找集群内的「关键连接」/1192-查找集群内的「关键连接」.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def criticalConnections(self, n, connections): 3 | ans, low, d = [], [-1] * n, [[] for _ in range(n)] 4 | for i, j in connections: 5 | d[i].append(j) 6 | d[j].append(i) 7 | def tarjan(c, v, p): 8 | dfn = low[v] = c 9 | for i in d[v]: 10 | if i != p: 11 | if low[i] == -1: 12 | c += 1 13 | tarjan(c, i, v) 14 | if low[i] > dfn: 15 | ans.append([v, i]) 16 | low[v] = min(low[v], low[i]) 17 | tarjan(0, 0, -1) 18 | return ans -------------------------------------------------------------------------------- /source/All_Solutions/1193.每月交易I/1193-每月交易I.sql: -------------------------------------------------------------------------------- 1 | SELECT DATE_FORMAT(trans_date, '%Y-%m') month, 2 | country, 3 | COUNT(*) trans_count, 4 | SUM(IF(state='approved',1,0))approved_count, 5 | SUM(amount) trans_total_amount, 6 | SUM(IF(state='approved',amount,0)) approved_total_amount 7 | FROM Transactions 8 | GROUP BY month, country -------------------------------------------------------------------------------- /source/All_Solutions/1196.最多可以买到的苹果数量/1196-最多可以买到的苹果数量.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxNumberOfApples(self, arr: List[int]) -> int: 3 | max_weight = 5000 4 | if sum(arr)<=5000: 5 | return len(arr) 6 | arr.sort() 7 | count = 0 8 | weight = 0 9 | while weight <= 5000: 10 | weight+=arr[count] 11 | count+=1 12 | return count-1 13 | 14 | 15 | -------------------------------------------------------------------------------- /source/All_Solutions/1198.找出所有行中最小公共元素/1198-找出所有行中最小公共元素.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1198.找出所有行中最小公共元素/1198-找出所有行中最小公共元素.py -------------------------------------------------------------------------------- /source/All_Solutions/1199.建造街区的最短时间/1199-建造街区的最短时间.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | class Solution: 3 | def minBuildTime(self, blocks: List[int], split: int) -> int: 4 | n = len(blocks) 5 | if n == 1: 6 | return blocks[0] 7 | heapq.heapify(blocks) 8 | heap = blocks 9 | while len(heap) > 1: 10 | a = heapq.heappop(heap) 11 | b = heapq.heappop(heap) 12 | heapq.heappush(heap, max(a+split, b+split)) 13 | return heap[0] -------------------------------------------------------------------------------- /source/All_Solutions/1200.最小绝对差/1200-最小绝对差.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]: 3 | arr.sort() 4 | result = [] 5 | min_dis = abs(arr[1] - arr[0]) 6 | for i in range(1,len(arr)): 7 | if abs(arr[i]-arr[i-1]) str: 3 | p = {i:i for i in range(len(s))} 4 | def f(x): 5 | if x != p[x]: 6 | p[x] = f(p[x]) 7 | return p[x] 8 | 9 | for i, j in pairs: 10 | p[f(j)] = f(i) 11 | 12 | d = collections.defaultdict(list) 13 | for i, j in enumerate(map(f, p)): 14 | d[j].append(i) 15 | 16 | ans = list(s) 17 | for q in d.values(): 18 | t = sorted(ans[i] for i in q) 19 | for i, c in zip(sorted(q), t): 20 | ans[i] = c 21 | return ''.join(ans) -------------------------------------------------------------------------------- /source/All_Solutions/1205.每月交易II/1205-每月交易II.sql: -------------------------------------------------------------------------------- 1 | select date_format(a.trans_date,"%Y-%m") month, 2 | a.country, 3 | sum(a.sta="approved") approved_count, 4 | sum((a.sta="approved")*a.amount) approved_amount, 5 | sum(a.sta="backs") chargeback_count, 6 | sum((a.sta="backs")*a.amount) chargeback_amount 7 | from 8 | (select *,"approved" as sta 9 | from Transactions 10 | where state="approved" 11 | union all 12 | select c.trans_id,t.country,t.state,t.amount,c.trans_date,"backs" as sta 13 | from Chargebacks c 14 | left join transactions t 15 | on t.id=c.trans_id 16 | ) as a 17 | group by date_format(a.trans_date,"%Y-%m"),a.country 18 | having sum(a.sta="approved")+sum(a.sta="backs")<>0 19 | order by date_format(a.trans_date,"%Y-%m") 20 | -------------------------------------------------------------------------------- /source/All_Solutions/1206.设计跳表/1206-设计跳表.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1206.设计跳表/1206-设计跳表.py -------------------------------------------------------------------------------- /source/All_Solutions/1208.尽可能使字符串相等/1208-尽可能使字符串相等.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def equalSubstring(self, s: str, t: str, maxCost: int) -> int: 3 | arr = [abs(ord(s[i]) - ord(t[i])) for i in range(len(s))] 4 | maxlen = 0 5 | reslen = 0 6 | sum = 0 7 | for i in range(len(arr)): 8 | sum += arr[i] 9 | if sum <= maxCost: 10 | reslen += 1 11 | elif sum>0: 12 | sum -= arr[i-reslen] 13 | maxlen = max(maxlen,reslen) 14 | return maxlen -------------------------------------------------------------------------------- /source/All_Solutions/1209.删除字符串中的所有相邻重复项II/1209-删除字符串中的所有相邻重复项II.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def removeDuplicates(self, s, k): 3 | """ 4 | :type s: str 5 | :type k: int 6 | :rtype: str 7 | """ 8 | temp = [] 9 | for i, j in enumerate(s): 10 | if not temp: 11 | temp.append([j, 1]) 12 | else: 13 | if j == temp[-1][0]: 14 | if temp[-1][1] == k - 1: 15 | temp.pop() 16 | else: 17 | temp[-1][1] += 1 18 | else: 19 | temp.append([j, 1]) 20 | 21 | return ''.join(item[0] * item[1] for item in temp) -------------------------------------------------------------------------------- /source/All_Solutions/1214.查找两棵二叉搜索树之和/1214-查找两棵二叉搜索树之和.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSumBSTs(self, root1: TreeNode, root2: TreeNode, target: int) -> bool: 3 | d = set() 4 | def f(r): 5 | if r: 6 | d.add(target - r.val) 7 | f(r.left) 8 | f(r.right) 9 | f(root1) 10 | ans = False 11 | def g(r): 12 | nonlocal ans 13 | if r and not ans: 14 | if r.val in d: 15 | ans = True 16 | g(r.left) 17 | g(r.right) 18 | g(root2) 19 | return ans -------------------------------------------------------------------------------- /source/All_Solutions/1215.步进数/1215-步进数.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1215.步进数/1215-步进数.java -------------------------------------------------------------------------------- /source/All_Solutions/1216.验证回文字符串III/1216-验证回文字符串III.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isValidPalindrome(self, s: str, k: int) -> bool: 3 | return LCS(s,s[::-1]) + k >= len(s) 4 | 5 | def LCS(a, b): 6 | m, n = len(a)+1, len(b)+1 7 | F = [[0]*n for _ in range(m)] 8 | for i in range(1,m): 9 | for j in range(1,n): 10 | if a[i-1]==b[j-1]: 11 | F[i][j]=F[i-1][j-1]+1 12 | else: 13 | F[i][j]=max(F[i-1][j],F[i][j-1]) 14 | return F[m-1][n-1] -------------------------------------------------------------------------------- /source/All_Solutions/1225.报告系统状态的连续日期/1225-报告系统状态的连续日期.sql: -------------------------------------------------------------------------------- 1 | select * from 2 | ((select 'succeeded' period_state, min(success_date)start_date, max(success_date)end_date 3 | from (select success_date, if(datediff(success_date, @prev)=1, @count, @count:=@count+1) num, @prev:=success_date 4 | from Succeeded, (select @prev:=null, @count:=0)i 5 | where year(success_date)='2019') t 6 | group by t.num) 7 | 8 | union all 9 | 10 | (select 'failed' period_state, min(fail_date)start_date, max(fail_date)end_date 11 | from (select fail_date, if(datediff(fail_date, @prev)=1, @count, @count:=@count+1) num, @prev:=fail_date 12 | from Failed, (select @prev:=null, @count:=0)i 13 | where year(fail_date)='2019') t 14 | group by t.num)) t 15 | order by start_date; 16 | -------------------------------------------------------------------------------- /source/All_Solutions/1241.每个帖子的评论数/1241-每个帖子的评论数.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT p.post_id, COUNT(DISTINCT(s.sub_id)) AS number_of_comments 3 | FROM Submissions AS s 4 | RIGHT JOIN( 5 | SELECT DISTINCT(sub_id) as post_id 6 | FROM Submissions 7 | WHERE parent_id IS NULL 8 | )AS p 9 | ON p.post_id = s.parent_id 10 | GROUP BY p.post_id 11 | -------------------------------------------------------------------------------- /source/All_Solutions/1260.二维网格迁移/1260-二维网格迁移.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def shiftGrid(self, grid, k): 3 | """ 4 | :type grid: List[List[int]] 5 | :type k: int 6 | :rtype: List[List[int]] 7 | """ 8 | line = [] 9 | for i in range(len(grid)): 10 | for j in range(len(grid[0])): 11 | line.append(grid[i][j]) 12 | idx = k % len(line) 13 | line[:] = line[len(line) - idx:] + line[:len(line) - idx] 14 | for i in range(len(grid)): 15 | for j in range(len(grid[0])): 16 | grid[i][j] = line.pop(0) 17 | return grid -------------------------------------------------------------------------------- /source/All_Solutions/1261.在受污染的二叉树中查找元素/1261-在受污染的二叉树中查找元素.py: -------------------------------------------------------------------------------- 1 | class FindElements: 2 | 3 | def __init__(self, root: TreeNode): 4 | self.vals = set() 5 | def f(r, val): 6 | if r: 7 | r.val = val 8 | self.vals.add(val) 9 | if r.left: 10 | f(r.left, 2*val+1) 11 | if r.right: 12 | f(r.right, 2*val+2) 13 | f(root, 0) 14 | 15 | def find(self, target: int) -> bool: 16 | return target in self.vals -------------------------------------------------------------------------------- /source/All_Solutions/1262.可被三整除的最大和/1262-可被三整除的最大和.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSumDivThree(self, nums: List[int]) -> int: 3 | s = sum(nums) 4 | k = s % 3 5 | if not k: 6 | return s 7 | a, b, hq = s, [-s, -s], heapq.heappushpop 8 | c, d = (1, 2) if k == 1 else (2, 1) 9 | for i in nums: 10 | if i % 3 == c: 11 | a = min(a, i) 12 | elif i % 3 == d: 13 | hq(b, -i) 14 | return s - min(a, -sum(b)) -------------------------------------------------------------------------------- /source/All_Solutions/1264.页面推荐/1264-页面推荐.sql: -------------------------------------------------------------------------------- 1 | select 2 | distinct page_id as recommended_page 3 | from Likes 4 | where user_id in (select 5 | if(user1_id=1,user2_id,user1_id) as friend 6 | from Friendship where user1_id =1 or user2_id =1 7 | ) 8 | and page_id not in (select page_id from Likes where user_id= 1) -------------------------------------------------------------------------------- /source/All_Solutions/1265.逆序打印不可变链表/1265-逆序打印不可变链表.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def printLinkedListInReverse(self, head: 'ImmutableListNode') -> None: 3 | if not head: return 4 | self.printLinkedListInReverse(head.getNext()) 5 | head.printValue() 6 | 7 | -------------------------------------------------------------------------------- /source/All_Solutions/1266.访问所有点的最小时间/1266-访问所有点的最小时间.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int: 3 | return sum(max(abs(points[i][0] - points[i - 1][0]), abs(points[i][1] - points[i - 1][1])) for i in range(1, len(points))) -------------------------------------------------------------------------------- /source/All_Solutions/1267.统计参与通信的服务器/1267-统计参与通信的服务器.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1267.统计参与通信的服务器/1267-统计参与通信的服务器.py -------------------------------------------------------------------------------- /source/All_Solutions/1268.搜索推荐系统/1268-搜索推荐系统.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]: 3 | n=len(searchWord) 4 | products.sort() 5 | ans=[] 6 | for i in range(1,n+1): 7 | res=[] 8 | for j in products: 9 | if j[0:i]==searchWord[0:i]: 10 | res.append(j) 11 | products=res 12 | ans.append(res[0:3]) 13 | return ans -------------------------------------------------------------------------------- /source/All_Solutions/1269.停在原地的方案数/1269-停在原地的方案数.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numWays(self, steps: int, arrLen: int) -> int: 3 | if steps==1 or arrLen==1: 4 | return 1 5 | m=min(steps,arrLen) 6 | dp=[0]*m 7 | dp[0]=dp[1]=1 8 | DP=[0]*m 9 | for _ in range(1,steps): 10 | for i in range(m): 11 | if i ==0: 12 | DP[i]=dp[0]+dp[1] 13 | elif i==(m-1): 14 | DP[i]=dp[m-1]+dp[m-2] 15 | else: 16 | DP[i]=dp[i-1]+dp[i]+dp[i+1] 17 | dp=DP.copy() 18 | return DP[0]%(10**9 + 7) -------------------------------------------------------------------------------- /source/All_Solutions/1270.向公司CEO汇报工作的所有人/1270-向公司CEO汇报工作的所有人.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1270.向公司CEO汇报工作的所有人/1270-向公司CEO汇报工作的所有人.sql -------------------------------------------------------------------------------- /source/All_Solutions/1272.删除区间/1272-删除区间.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeInterval(self, intervals: List[List[int]], toBeRemoved: List[int]) -> List[List[int]]: 3 | res = [] 4 | l = toBeRemoved[0] 5 | r = toBeRemoved[1] 6 | 7 | for time in intervals: 8 | if time[0] > r or time[1] < l: 9 | res.append(time) 10 | if time[0] < l and l < time[1]: 11 | res.append([time[0],l]) 12 | if time[0] < r and r < time[1]: 13 | res.append([r,time[1]]) 14 | 15 | return res -------------------------------------------------------------------------------- /source/All_Solutions/1273.删除树节点/1273-删除树节点.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def deleteTreeNodes(self, nodes: int, parent: List[int], value: List[int]) -> int: 3 | tree = [{'count': 0, 'sum': 0} for _ in range(nodes + 1)] 4 | for i, (p, v) in [*enumerate(zip(parent, value))][:: -1]: 5 | tree[i]['sum'] += v 6 | if tree[i]['sum']: 7 | tree[p]['count'] += tree[i]['count'] + 1 8 | tree[p]['sum'] += tree[i]['sum'] 9 | return tree[-1]['count'] -------------------------------------------------------------------------------- /source/All_Solutions/1276.不浪费原料的汉堡制作方案/1276-不浪费原料的汉堡制作方案.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]: 3 | if tomatoSlices&1 or tomatoSlices>cheeseSlices<<2 or tomatoSlices>1 6 | small=cheeseSlices-jumbo 7 | return jumbo,small -------------------------------------------------------------------------------- /source/All_Solutions/1278.分割回文串III/1278-分割回文串III.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1278.分割回文串III/1278-分割回文串III.py -------------------------------------------------------------------------------- /source/All_Solutions/1280.学生们参加各科测试的次数/1280-学生们参加各科测试的次数.sql: -------------------------------------------------------------------------------- 1 | select s1.student_id, s1.student_name, s2.subject_name, count(e.subject_name) attended_exams 2 | from Students s1 3 | join Subjects s2 4 | left join 5 | Examinations e 6 | on s1.student_id=e.student_id and s2.subject_name=e.subject_name 7 | group by s1.student_id, s2.subject_name; -------------------------------------------------------------------------------- /source/All_Solutions/1281.整数的各位积和之差/1281-整数的各位积和之差.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def subtractProductAndSum(self, n: int) -> int: 3 | n = str(n) 4 | num = [] 5 | for i in n: 6 | i = int(i) 7 | num.append(i) 8 | ji = 1 9 | he = 0 10 | for i in num: 11 | ji *= i 12 | he += i 13 | cha = ji-he 14 | return cha -------------------------------------------------------------------------------- /source/All_Solutions/1282.用户分组/1282-用户分组.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1282.用户分组/1282-用户分组.py -------------------------------------------------------------------------------- /source/All_Solutions/1283.使结果不超过阈值的最小除数/1283-使结果不超过阈值的最小除数.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1283.使结果不超过阈值的最小除数/1283-使结果不超过阈值的最小除数.py -------------------------------------------------------------------------------- /source/All_Solutions/1285.找到连续区间的开始和结束数字/1285-找到连续区间的开始和结束数字.sql: -------------------------------------------------------------------------------- 1 | select min(t.log_id) start_id, max(t.log_id) end_id 2 | from( 3 | select log_id, if(@prev=log_id-1, @count, @count:=@count+1) num, @prev:=log_id 4 | from Logs, (select @prev:=null, @count:=0)i 5 | )t 6 | group by t.num; -------------------------------------------------------------------------------- /source/All_Solutions/1286.字母组合迭代器/1286-字母组合迭代器.py: -------------------------------------------------------------------------------- 1 | class CombinationIterator: 2 | 3 | def __init__(self, characters: str, combinationLength: int): 4 | self.d = itertools.combinations(characters, combinationLength) 5 | self.n = math.comb(len(characters), combinationLength) 6 | 7 | def next(self) -> str: 8 | self.n -= 1 9 | return ''.join(next(self.d)) 10 | 11 | def hasNext(self) -> bool: 12 | return self.n -------------------------------------------------------------------------------- /source/All_Solutions/1287.有序数组中出现次数超过25%的元素/1287-有序数组中出现次数超过25%的元素.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findSpecialInteger(self, arr: List[int]) -> int: 3 | n = len(arr) 4 | for i in set(arr): 5 | num = arr.count(i) 6 | if num/n > 0.25: 7 | return i -------------------------------------------------------------------------------- /source/All_Solutions/1288.删除被覆盖区间/1288-删除被覆盖区间.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: 3 | intervals.sort(key=lambda x:[x[0], -1*x[1]]) 4 | left, mx, res = -1, -1, 0 5 | for l, r in intervals: 6 | if l != left and mx < r: 7 | left, mx = l, max(mx, r) 8 | res += 1 9 | return res -------------------------------------------------------------------------------- /source/All_Solutions/1289.下降路径最小和II/1289-下降路径最小和II.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1289.下降路径最小和II/1289-下降路径最小和II.py -------------------------------------------------------------------------------- /source/All_Solutions/1294.不同国家的天气类型/1294-不同国家的天气类型.sql: -------------------------------------------------------------------------------- 1 | select country_name, 2 | (case 3 | when avg <= 15 then "Cold" 4 | when avg >= 25 then "Hot" 5 | else "Warm" 6 | end) as weather_type 7 | from ( 8 | select c.country_name, avg(w.weather_state) as avg 9 | from Countries as c, Weather as w 10 | where c.country_id = w.country_id and month(w.day)=11 11 | group by c.country_name 12 | order by c.country_id 13 | ) as new; -------------------------------------------------------------------------------- /source/All_Solutions/1296.划分数组为连续数字的集合/1296-划分数组为连续数字的集合.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1296.划分数组为连续数字的集合/1296-划分数组为连续数字的集合.py -------------------------------------------------------------------------------- /source/All_Solutions/1297.子串的最大出现次数/1297-子串的最大出现次数.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/z1ming/LeetCode-Notebook/1dbd18114ed688ddeaa3ee83181d373dcc1429e5/source/All_Solutions/1297.子串的最大出现次数/1297-子串的最大出现次数.py -------------------------------------------------------------------------------- /source/All_Solutions/1298.你能从盒子里获得的最大糖果数/1298-你能从盒子里获得的最大糖果数.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes): 3 | if not initialBoxes: 4 | return 0 5 | cur_stack = initialBoxes 6 | seen = set([]) # visited boxes 7 | while cur_stack: 8 | next_stack = [] 9 | for box in cur_stack: 10 | seen.add(box) 11 | for k in keys[box]: 12 | status[k] = 1 13 | next_stack += containedBoxes[box] 14 | cur_stack = next_stack 15 | 16 | return sum(candies[s] for s in seen if status[s]) -------------------------------------------------------------------------------- /source/All_Solutions/1299.将每个元素替换为右侧最大元素/1299-将每个元素替换为右侧最大元素.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def replaceElements(self, arr: List[int]) -> List[int]: 3 | if len(arr) == 1: 4 | return [-1] 5 | n = len(arr) 6 | ans = [0] * (n - 2)+[arr[-1]] +[-1] 7 | for i in range(n - 3, -1, -1): 8 | ans[i] = max(ans[i + 1], arr[i + 1]) 9 | return ans -------------------------------------------------------------------------------- /source/All_Solutions/1302.层数最深叶子节点的和/1302-层数最深叶子节点的和.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def deepestLeavesSum(self, root: TreeNode) -> int: 3 | if root == None: 4 | return 0; 5 | queue = []; 6 | res = 0; 7 | resList = []; 8 | queue.append(root); 9 | while(queue): 10 | length = len(queue); 11 | for i in range(length): 12 | res += queue[0].val; 13 | if queue[0].left: 14 | queue.append(queue[0].left); 15 | if queue[0].right: 16 | queue.append(queue[0].right); 17 | del queue[0]; 18 | resList.append(res); 19 | res = 0; 20 | return resList[-1]; -------------------------------------------------------------------------------- /source/All_Solutions/1303.求团队人数/1303-求团队人数.sql: -------------------------------------------------------------------------------- 1 | select e1.employee_id, count(*) team_size 2 | from employee e1 left join employee e2 3 | on e1.team_id = e2.team_id 4 | group by e1.employee_id; -------------------------------------------------------------------------------- /source/All_Solutions/1304.和为零的N个唯一整数/1304-和为零的N个唯一整数.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumZero(self, n: int) -> List[int]: 3 | return range(1 - n, n, 2) -------------------------------------------------------------------------------- /source/All_Solutions/1306.跳跃游戏III/1306-跳跃游戏III.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canReach(self, arr: List[int], start: int) -> bool: 3 | q, v, n = [start], {start}, len(arr) 4 | while q: 5 | p = [] 6 | for i in q: 7 | if not arr[i]: 8 | return True 9 | for j in i - arr[i], i + arr[i]: 10 | if 0 <= j < n and j not in v: 11 | p.append(j) 12 | v.add(j) 13 | q = p 14 | return False -------------------------------------------------------------------------------- /source/All_Solutions/1310.子数组异或查询/1310-子数组异或查询.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: 3 | acc = list(itertools.accumulate([0] + arr, operator.xor)) 4 | return [acc[a] ^ acc[b + 1] for a, b in queries] 5 | -------------------------------------------------------------------------------- /source/All_Solutions/1311.获取你好友已观看的视频/1311-获取你好友已观看的视频.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def watchedVideosByFriends(self, watchedVideos: List[List[str]], friends: List[List[int]], id: int, level: int) -> List[str]: 3 | C, R = set(), {id} 4 | for i in range(level): 5 | C |= R 6 | R = set(sum([friends[r] for r in R], [])) - C 7 | D = collections.Counter(sum([watchedVideos[r] for r in R], [])) 8 | return sorted(D, key = lambda a: (D[a], a)) -------------------------------------------------------------------------------- /source/All_Solutions/1312.让字符串成为回文串的最少插入次数/1312-让字符串成为回文串的最少插入次数.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minInsertions(self, s: str) -> int: 3 | from functools import lru_cache 4 | @lru_cache(300000) 5 | def F(i, j): 6 | if i >= j: 7 | return 0 8 | if s[i] == s[j]: 9 | return F(i + 1, j - 1) 10 | return 1 + min(F(i + 1, j), F(i, j - 1)) 11 | return F(0, len(s) - 1) -------------------------------------------------------------------------------- /source/All_Solutions/1314.矩阵区域和/1314-矩阵区域和.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: 3 | m, n = len(mat), len(mat[0]) 4 | s = [[0] * (n + 1)] + [[0, *itertools.accumulate(row)] for row in mat] 5 | for i, j in itertools.product(range(1, m + 1), range(1, n + 1)): 6 | s[i][j] += s[i - 1][j] 7 | def area(i, j): 8 | a = min(i + K, m - 1) + 1 9 | b = min(j + K, n - 1) + 1 10 | c = max(i - K, 0) 11 | d = max(j - K, 0) 12 | return s[a][b] + s[c][d] - s[a][d] - s[c][b] 13 | return [[area(i, j) for j in range(n)] for i in range(m)] -------------------------------------------------------------------------------- /source/All_Solutions/1315.祖父节点值为偶数的节点和/1315-祖父节点值为偶数的节点和.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumEvenGrandparent(self, root: TreeNode) -> int: 3 | d = collections.defaultdict(lambda: 1) 4 | def f(r, i): 5 | if r: 6 | if not d[i - 2] & 1: 7 | d['ans'] += r.val 8 | d[i] = r.val 9 | f(r.left, i + 1) 10 | f(r.right, i + 1) 11 | f(root, 0) 12 | return d['ans'] - 1 -------------------------------------------------------------------------------- /source/All_Solutions/1316.不同的循环子字符串/1316-不同的循环子字符串.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def distinctEchoSubstrings(self, text: str) -> int: 3 | n = len(text) 4 | seen = set() 5 | ans = 0 6 | for i in range(n): 7 | for j in range(i + 1, n): 8 | if j * 2 - i <= n and text[i:j] == text[j:j*2-i] and text[i:j] not in seen: 9 | ans += 1 10 | seen.add(text[i:j]) 11 | return ans 12 | -------------------------------------------------------------------------------- /source/All_Solutions/1324.竖直打印单词/1324-竖直打印单词.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def printVertically(self, s: str) -> List[str]: 3 | return [''.join(i).rstrip() for i in itertools.zip_longest(*s.split(),fillvalue=' ')] -------------------------------------------------------------------------------- /source/All_Solutions/1325.删除给定值的叶子节点/1325-删除给定值的叶子节点.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def removeLeafNodes(self, root, target): 3 | """ 4 | :type root: TreeNode 5 | :type target: int 6 | :rtype: TreeNode 7 | """ 8 | if not root: 9 | return root 10 | root.left = self.removeLeafNodes(root.left, target) 11 | root.right = self.removeLeafNodes(root.right, target) 12 | if not root.left and not root.right and root.val == target: 13 | return None 14 | return root -------------------------------------------------------------------------------- /source/All_Solutions/1367.二叉树中的列表/1367-二叉树中的列表.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | bool work(ListNode* head, TreeNode* root) 3 | { 4 | if(head==nullptr)return 1; 5 | if(root==nullptr||root->val!=head->val)return 0; 6 | return work(head->next,root->left)||work(head->next,root->right); 7 | } 8 | public: 9 | bool isSubPath(ListNode* head, TreeNode* root) { 10 | if(root==nullptr)return 0; 11 | return work(head,root)||isSubPath(head,root->left)||isSubPath(head,root->right); 12 | } 13 | }; -------------------------------------------------------------------------------- /source/Clarification/Array/1002.查找常用字符.py: -------------------------------------------------------------------------------- 1 | # 给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。 2 | # 3 | # 你可以按任意顺序返回答案。 4 | # 5 | #   6 | # 7 | # 示例 1: 8 | # 9 | # 输入:["bella","label","roller"] 10 | # 输出:["e","l","l"] 11 | # 12 | class Solution: 13 | def commonChars(self, A: List[str]) -> List[str]: 14 | if not A: 15 | return [] 16 | 17 | res = [] 18 | for c in set(A[0]): # 遍历A[0]中的字符串c,set()去重 19 | count = [w.count(c) for w in A] # c在各字符串中的出现次数保存在count中 20 | s = c * min(count) 21 | for i in s: 22 | res.append(i) 23 | return res 24 | -------------------------------------------------------------------------------- /source/Clarification/Array/1085.最小元素各数位之和.py: -------------------------------------------------------------------------------- 1 | # 给你一个正整数的数组 A。 2 | # 3 | # 然后计算 S,使其等于数组 A 当中最小的那个元素各个数位上数字之和。 4 | # 5 | # 最后,假如 S 所得计算结果是 奇数 的请你返回 0,否则请返回 1。 6 | # 7 | #   8 | # 9 | # 示例 1: 10 | # 11 | # 输入:[34,23,1,24,75,33,54,8] 12 | # 输出:0 13 | # 解释: 14 | # 最小元素为 1,该元素各个数位上的数字之和 S = 1,是奇数所以答案为 0。 15 | # 示例 2: 16 | # 17 | # 输入:[99,77,33,66,55] 18 | # 输出:1 19 | # 解释: 20 | # 最小元素为 33,该元素各个数位上的数字之和 S = 3 + 3 = 6,是偶数所以答案为 1。 21 | 22 | class Solution: 23 | def sumOfDigits(self, A: List[int]) -> int: 24 | A.sort() 25 | n = A[0] 26 | sum_ = 0 27 | while n > 0: 28 | sum_ += n % 10 29 | n = n // 10 30 | if sum_ % 2 == 1: 31 | return 0 32 | return 1 -------------------------------------------------------------------------------- /source/Clarification/Array/1119.删去字符串中的元音.py: -------------------------------------------------------------------------------- 1 | # 给你一个字符串 S,请你删去其中的所有元音字母( 'a','e','i','o','u'),并返回这个新字符串。 2 | # 3 | #   4 | # 5 | # 示例 1: 6 | # 7 | # 输入:"leetcodeisacommunityforcoders" 8 | # 输出:"ltcdscmmntyfrcdrs" 9 | 10 | 11 | class Solution: 12 | def removeVowels(self, S: str) -> str: 13 | return ''.join(filter(lambda c:c not in "aeiou",S)) -------------------------------------------------------------------------------- /source/Clarification/Array/163.缺失的区间.py: -------------------------------------------------------------------------------- 1 | # 给定一个排序的整数数组 nums ,其中元素的范围在 闭区间 [lower, upper] 当中,返回不包含在数组中的缺失区间。 2 | # 3 | # 示例: 4 | # 5 | # 输入: nums = [0, 1, 3, 50, 75], lower = 0 和 upper = 99, 6 | # 输出: ["2", "4->49", "51->74", "76->99"] 7 | 8 | class Solution: 9 | def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]: 10 | ans = [] 11 | nums = [lower-1]+nums+[upper+1] 12 | for i in range(len(nums)-1): 13 | d = nums[i+1] - nums[i] 14 | if d == 2: 15 | ans.append(str(nums[i]+1)) 16 | elif d > 2: 17 | ans.append("{}->{}".format(nums[i]+1, nums[i+1]-1)) 18 | return ans 19 | -------------------------------------------------------------------------------- /source/Clarification/Array/169.多数元素.py: -------------------------------------------------------------------------------- 1 | # 给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。 2 | # 3 | # 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 4 | # 5 | # 示例 1: 6 | # 7 | # 输入: [3,2,3] 8 | # 输出: 3 9 | # 10 | 11 | from collections import Counter 12 | class Solution: 13 | def majorityElement(self, nums: List[int]) -> int: 14 | dic = Counter(nums) 15 | for key,value in dic.items(): 16 | if value > len(nums) / 2: 17 | return key 18 | # 时间复杂度:O(n).将哈希表迭代一次,哈希表的插入是常数时间的。所以总时间复杂度是O(n) 19 | # 空间复杂度:O(n)哈希表最多包含n-|n/2|个关系,所以占用的空间为O(n) -------------------------------------------------------------------------------- /source/Clarification/Array/179.最大数.py: -------------------------------------------------------------------------------- 1 | # 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: [10,2] 6 | # 输出: 210 7 | # 示例 2: 8 | # 9 | # 输入: [3,30,34,5,9] 10 | # 输出: 9534330 11 | 12 | class LargerNumKey(str): 13 | def __lt__(x,y): 14 | return x + y > y + x 15 | class Solution: 16 | def largestNumber(self, nums: List[int]) -> str: 17 | largest_num = ''.join(sorted(map(str,nums),key=LargerNumKey)) 18 | return '0' if largest_num[0] == '0' else largest_num -------------------------------------------------------------------------------- /source/Clarification/Array/229.求众数Ⅱ.py: -------------------------------------------------------------------------------- 1 | # 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。 2 | # 3 | # 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1)。 4 | # 5 | # 示例 1: 6 | # 7 | # 输入: [3,2,3] 8 | # 输出: [3] 9 | # 示例 2: 10 | # 11 | # 输入: [1,1,1,3,3,2,2,2] 12 | # 输出: [1,2] 13 | 14 | class Solution: 15 | def majorityElement(self, nums: List[int]) -> List[int]: 16 | res, n = [], len(nums) 17 | dic = collections.Counter(nums) 18 | for key ,value in dic.items(): 19 | if value > n/3: 20 | res.append(key) 21 | return res -------------------------------------------------------------------------------- /source/Clarification/Array/274.H指数.py: -------------------------------------------------------------------------------- 1 | # 给定一位研究者论文被引用次数的数组(被引用次数是非负整数)。编写一个方法,计算出研究者的 h 指数。 2 | # 3 | # h 指数的定义: “h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)至多有 h 篇论文分别被引用了至少 h 次。(其余的 N - h 篇论文每篇被引用次数不多于 h 次。)” 4 | # 5 | #   6 | # 7 | # 示例: 8 | # 9 | # 输入: citations = [3,0,6,1,5] 10 | # 输出: 3 11 | # 解释: 给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。 12 | #   由于研究者有 3 篇论文每篇至少被引用了 3 次,其余两篇论文每篇被引用不多于 3 次,所以她的 h 指数是 3。 13 | #   14 | # 15 | # 说明: 如果 h 有多种可能的值,h 指数是其中最大的那个。 16 | 17 | class Solution: 18 | def hIndex(self, citations: List[int]) -> int: 19 | citations = sorted(citations,reverse=True) 20 | i = 0 21 | while i < len(citations) and citations[i] > i: 22 | i += 1 23 | return i -------------------------------------------------------------------------------- /source/Clarification/Array/293.翻转游戏.py: -------------------------------------------------------------------------------- 1 | # 你和朋友玩一个叫做「翻转游戏」的游戏,游戏规则:给定一个只有 + 和 - 的字符串。你和朋友轮流将 连续 的两个 "++" 反转成 "--"。 当一方无法进行有效的翻转时便意味着游戏结束,则另一方获胜。 2 | # 3 | # 请你写出一个函数,来计算出每个有效操作后,字符串所有的可能状态。 4 | # 5 | # 示例: 6 | # 7 | # 输入: s = "++++" 8 | # 输出: 9 | # [ 10 | # "--++", 11 | # "+--+", 12 | # "++--" 13 | # ] 14 | 15 | class Solution: 16 | def generatePossibleNextMoves(self, s: str) -> List[str]: 17 | res = [] 18 | for i in range(1,len(s)): 19 | if s[i-1] == "+" and s[i] == "+": 20 | tmp = s[:i-1] + "--" + s[i+1:] 21 | res.append(tmp) 22 | return res -------------------------------------------------------------------------------- /source/Clarification/Array/389.找不同.py: -------------------------------------------------------------------------------- 1 | # 给定两个字符串 s 和 t,它们只包含小写字母。 2 | # 3 | # 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 4 | # 5 | # 请找出在 t 中被添加的字母。 6 | # 7 | #   8 | # 9 | # 示例: 10 | # 11 | # 输入: 12 | # s = "abcd" 13 | # t = "abcde" 14 | # 15 | # 输出: 16 | # e 17 | # 18 | # 解释: 19 | # 'e' 是那个被添加的字母。 20 | 21 | class Solution: 22 | def findTheDifference(self, s: str, t: str) -> str: 23 | t = list(t) 24 | for i in s: 25 | t.remove(i) 26 | return t[0] -------------------------------------------------------------------------------- /source/Clarification/Array/414.第三大的数.py: -------------------------------------------------------------------------------- 1 | # 给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: [3, 2, 1] 6 | # 7 | # 输出: 1 8 | # 9 | # 解释: 第三大的数是 1 10 | 11 | class Solution: 12 | def thirdMax(self, nums: List[int]) -> int: 13 | nums = list(set(nums)) 14 | if len(nums) < 3: 15 | return max(nums) 16 | nums = sorted(nums,reverse=True) 17 | return nums[2] 18 | # 时间复杂度:O(n)? 19 | # 空间复杂度:O(n)? -------------------------------------------------------------------------------- /source/Clarification/Array/415.字符串相加.py: -------------------------------------------------------------------------------- 1 | # 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。 2 | # 3 | # 注意: 4 | # 5 | # num1 和num2 的长度都小于 5100. 6 | # num1 和num2 都只包含数字 0-9. 7 | # num1 和num2 都不包含任何前导零。 8 | # 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。 9 | 10 | class Solution: 11 | def addStrings(self, num1: str, num2: str) -> str: 12 | res = '' 13 | i,j,carry = len(num1) - 1,len(num2) - 1,0 14 | while i >= 0 or j >= 0: 15 | n1 = int(num1[i]) if i >= 0 else 0 16 | n2 = int(num2[j]) if j >= 0 else 0 17 | tmp = n1 + n2 + carry 18 | carry = tmp // 10 19 | res = str(tmp % 10) + res 20 | i,j = i - 1,j - 1 21 | return res if carry==0 else '1'+res -------------------------------------------------------------------------------- /source/Clarification/Array/434.字符串中的单词数.py: -------------------------------------------------------------------------------- 1 | # 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。 2 | # 3 | # 请注意,你可以假定字符串里不包括任何不可打印的字符。 4 | # 5 | # 示例: 6 | # 7 | # 输入: "Hello, my name is John" 8 | # 输出: 5 9 | class Solution: 10 | def countSegments(self, s: str) -> int: 11 | return len(s.split()) 12 | 13 | class Solution: 14 | def countSegments(self, s: str) -> int: 15 | sigment_count = 0 16 | for i in range(len(s)): 17 | if (i == 0 or s[i-1] == ' ') and s[i] != ' ': 18 | sigment_count += 1 19 | return sigment_count 20 | -------------------------------------------------------------------------------- /source/Clarification/Array/448.找到所有数组中消失的数字.py: -------------------------------------------------------------------------------- 1 | # 给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。 2 | # 3 | # 找到所有在 [1, n] 范围之间没有出现在数组中的数字。 4 | # 5 | # 您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。 6 | # 7 | # 示例: 8 | # 9 | # 输入: 10 | # [4,3,2,7,8,2,3,1] 11 | # 12 | # 输出: 13 | # [5,6] 14 | # 题目要求不适用额外空间,故可在原数组上修改,数组内的元素作为索引值,遍历一趟,取负后,没有被置为负值的索引即为所求。 15 | class Solution: 16 | def findDisappearedNumbers(self, nums: List[int]) -> List[int]: 17 | for i in nums: 18 | index = abs(i)-1 # 保证索引值为正 19 | nums[index] = -abs(nums[index]) 20 | return [i+1 for i,j in enumerate(nums) if j > 0] 21 | -------------------------------------------------------------------------------- /source/Clarification/Array/459.重复的子字符串.py: -------------------------------------------------------------------------------- 1 | # 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: "abab" 6 | # 7 | # 输出: True 8 | # 9 | # 解释: 可由子字符串 "ab" 重复两次构成。 10 | # 示例 2: 11 | # 12 | # 输入: "aba" 13 | # 14 | # 输出: False 15 | class Solution: 16 | def repeatedSubstringPattern(self, s: str) -> bool: 17 | n = len(s) 18 | for i in range(1,n // 2 + 1): 19 | if n % i == 0 and s[:i]*(n//i) == s: 20 | return True 21 | return False -------------------------------------------------------------------------------- /source/Clarification/Array/485.最大连续1的个数.py: -------------------------------------------------------------------------------- 1 | # 给定一个二进制数组, 计算其中最大连续1的个数。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: [1,1,0,1,1,1] 6 | # 输出: 3 7 | # 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 8 | # 9 | ### 算法思路: 10 | 遍历一遍数组,`left`指针指向子序列的第一个`1`的位置,当`nums[i]`为0时,`left`指针指向`i`的下一个位置,直到出现下一个连续子序列。 11 | 遍历的过程中,我们始终更新子序列的最大值。 12 | ### 参考代码 13 | ``` 14 | class Solution: 15 | def findMaxConsecutiveOnes(self, nums: List[int]) -> int: 16 | res = 0 17 | left = 0 18 | for i in range(len(nums)): 19 | if nums[i] == 0: 20 | left = i + 1 21 | res = max(res,i-left + 1) 22 | return res 23 | ``` 24 | ### 复杂度分析 25 | - 时间复杂度:*O(N)*,*N*是数组的长度,我们遍历了一遍数组 26 | - 空间复杂度:*O(1)*,`res`,`left`占用了常数空间。 27 | -------------------------------------------------------------------------------- /source/Clarification/Array/504.七进制数.py: -------------------------------------------------------------------------------- 1 | # 给定一个整数,将其转化为7进制,并以字符串形式输出。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: 100 6 | # 输出: "202" 7 | # 示例 2: 8 | # 9 | # 输入: -7 10 | # 输出: "-10" 11 | # 注意: 输入范围是 [-1e7, 1e7] 。 12 | 13 | class Solution: 14 | def convertToBase7(self, num: int) -> str: 15 | ans = '' 16 | if num < 0: 17 | s = '-' 18 | else: 19 | s = '' 20 | while abs(num) // 7 != 0: 21 | ans += str(abs(num) % 7) 22 | num = abs(num) // 7 23 | ans += str(abs(num) % 7) 24 | return ans[::-1] if s == '' else '-' + ans[::-1] 25 | -------------------------------------------------------------------------------- /source/Clarification/Array/628.三个数的最大乘积.py: -------------------------------------------------------------------------------- 1 | # 给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: [1,2,3] 6 | # 输出: 6 7 | 8 | # 先对数组进行排序,返回数组前两个负数和最后一个正数,或后3个正数的乘积,取最大值。 9 | class Solution: 10 | def maximumProduct(self, nums: List[int]) -> int: 11 | if len(nums) < 3: 12 | return 0 13 | nums = sorted(nums) 14 | return max(nums[-1] * nums[-2] * nums[-3],nums[0]*nums[1]*nums[-1]) 15 | 16 | # 时间复杂度:O(NlogN),N为数组的长度,排序的时间复杂度为NlogN吗? 17 | # 空间复杂度:O(logN),排序使用的空间 -------------------------------------------------------------------------------- /source/Clarification/Array/643.子数组最大平均数Ⅰ.py: -------------------------------------------------------------------------------- 1 | # 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: [1,12,-5,-6,50,3], k = 4 6 | # 输出: 12.75 7 | # 解释: 最大平均数 (12-5-6+50)/4 = 51/4 = 12.75 8 | #   9 | # 10 | # 注意: 11 | # 12 | # 1 <= k <= n <= 30,000。 13 | # 所给数据范围 [-10,000,10,000]。 14 | 15 | class Solution: 16 | def findMaxAverage(self, nums: List[int], k: int) -> float: 17 | if k == 1: 18 | return max(nums) 19 | res = sum(nums[0:k]) 20 | temp = res 21 | for i in range(1,len(nums)-k+1): 22 | temp = temp - nums[i-1] + nums[i+k-1] 23 | if temp > res: 24 | res = temp 25 | return res / k -------------------------------------------------------------------------------- /source/Clarification/Array/651.数组拆分.py: -------------------------------------------------------------------------------- 1 | # 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: [1,4,3,2] 6 | # 7 | # 输出: 4 8 | # 解释: n 等于 2, 最大总和为 4 = min(1, 2) + min(3, 4). 9 | 10 | class Solution: 11 | def arrayPairSum(self, nums: List[int]) -> int: 12 | nums.sort() 13 | return sum(nums[::2]) 14 | # res = 0 15 | # for i in range(0,len(nums)-1,2): 16 | # res += nums[i] 17 | # return res 18 | # 时间复杂度:O(NlogN),使用排序的时间复杂度 19 | # 空间复杂度:O(N),使用蒂姆排序的空间复杂度 -------------------------------------------------------------------------------- /source/Clarification/Array/674.最长连续递增序列.py: -------------------------------------------------------------------------------- 1 | # 给定一个未经排序的整数数组,找到最长且连续的的递增序列。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: [1,3,5,4,7] 6 | # 输出: 3 7 | # 解释: 最长连续递增序列是 [1,3,5], 长度为3。 8 | # 尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为5和7在原数组里被4隔开。 9 | # 10 | class Solution: 11 | def findLengthOfLCIS(self, nums: List[int]) -> int: 12 | # 运用滑动窗口 13 | ans,archor = 0,0 14 | for i in range(len(nums)): 15 | if i and nums[i-1] >= nums[i]: 16 | archor = i 17 | ans = max(ans,i-archor+1) # 长度 = i - archor + 1 18 | return ans 19 | 20 | # 时间复杂度:O(N),N是nums的长度,我们通过nums执行一个循环 21 | # 空间复杂度:O(1),anchor和ans使用了常数级空间 -------------------------------------------------------------------------------- /source/Clarification/Array/680.验证回文字符串.py: -------------------------------------------------------------------------------- 1 | # 给定一个非空字符串 s,最多删除一个字符。判断是否能成为回文字符串。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: "aba" 6 | # 输出: True 7 | # 示例 2: 8 | # 9 | # 输入: "abca" 10 | # 输出: True 11 | # 解释: 你可以删除c字符。 12 | 13 | class Solution: 14 | def validPalindrome(self, s: str) -> bool: 15 | l, r = 0, len(s) - 1 16 | count = 0 17 | while l < r: 18 | # 考虑端点可删除情况 19 | if s[l] != s[r]: 20 | # 舍弃左字符 21 | a = s[l + 1:r + 1] 22 | # 舍弃右字符 23 | b = s[l:r] 24 | return a[::-1] == a or b[::-1] == b 25 | l += 1 26 | r -= 1 27 | return True 28 | -------------------------------------------------------------------------------- /source/Clarification/Array/686.重复叠加字符串匹配.py: -------------------------------------------------------------------------------- 1 | # 给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1。 2 | 3 | # 举个例子,A = "abcd",B = "cdabcdab"。 4 | # 5 | # 答案为 3, 因为 A 重复叠加三遍后为 “abcdabcdabcd”,此时 B 是其子串;A 重复叠加两遍后为"abcdabcd",B 并不是其子串。 6 | # 7 | # 注意: 8 | # 9 | #  A 与 B 字符串的长度在1和10000区间范围内。 10 | # 11 | class Solution: 12 | def repeatedStringMatch(self, A: str, B: str) -> int: 13 | base_num = len(B) // len(A) 14 | new_A = A * base_num 15 | if B in new_A: 16 | return base_num 17 | if B in new_A + A: 18 | return base_num + 1 19 | if B in new_A + A + A: 20 | return base_num + 2 21 | return -1 -------------------------------------------------------------------------------- /source/Clarification/Array/709.转换成小写字母.py: -------------------------------------------------------------------------------- 1 | # 实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串。 2 | # 3 | #   4 | # 5 | # 示例 1: 6 | # 7 | # 输入: "Hello" 8 | # 输出: "hello" 9 | # 示例 2: 10 | # 11 | # 输入: "here" 12 | # 输出: "here" 13 | # 示例 3: 14 | # 15 | # 输入: "LOVELY" 16 | # 输出: "lovely" 17 | class Solution: 18 | def toLowerCase(self, str: str) -> str: 19 | res = '' 20 | for i in range(len(str)): 21 | if 65 <= ord(str[i]) <= 90: 22 | res += chr(ord(str[i]) + 32) 23 | else: 24 | res += str[i] 25 | return res -------------------------------------------------------------------------------- /source/Clarification/Array/896.单调数列.py: -------------------------------------------------------------------------------- 1 | # 如果数组是单调递增或单调递减的,那么它是单调的。 2 | # 3 | # 如果对于所有 i <= j,A[i] <= A[j],那么数组 A 是单调递增的。 如果对于所有 i <= j,A[i]> = A[j],那么数组 A 是单调递减的。 4 | # 5 | # 当给定的数组 A 是单调数组时返回 true,否则返回 false。 6 | # 使用all()函数 7 | class Solution: 8 | def isMonotonic(self, A: List[int]) -> bool: 9 | return (all(A[i] <= A[i+1] for i in range(len(A) - 1)) or 10 | all(A[i] >= A[i+1] for i in range(len(A) - 1))) 11 | # 时间复杂度:O(N)N是A的长度。遍历了两次。 12 | # 空间复杂度:O(1)。 -------------------------------------------------------------------------------- /source/Clarification/Array/917.仅仅反转字母.py: -------------------------------------------------------------------------------- 1 | # 给定一个字符串 S,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转。 2 | # 3 | #   4 | # 5 | # 示例 1: 6 | # 7 | # 输入:"ab-cd" 8 | # 输出:"dc-ba" 9 | # 示例 2: 10 | # 11 | # 输入:"a-bC-dEf-ghIj" 12 | # 输出:"j-Ih-gfE-dCba" 13 | # 示例 3: 14 | # 15 | # 输入:"Test1ng-Leet=code-Q!" 16 | # 输出:"Qedo1ct-eeLg=ntse-T!" 17 | class Solution: 18 | def reverseOnlyLetters(self, S: str) -> str: 19 | ans = [] 20 | letter = [c for c in S if c.isalpha()] 21 | for c in S: 22 | if c.isalpha(): 23 | ans.append(letter.pop()) 24 | else: 25 | ans.append(c) 26 | return ''.join(ans) 27 | # 时间复杂度:O(N) 28 | # 空间复杂度:O(N) -------------------------------------------------------------------------------- /source/Clarification/Array/977.有序数组的平方.py: -------------------------------------------------------------------------------- 1 | # 给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序。 2 | # 3 | #   4 | # 5 | # 示例 1: 6 | # 7 | # 输入:[-4,-1,0,3,10] 8 | # 输出:[0,1,9,16,100] 9 | # 示例 2: 10 | # 11 | # 输入:[-7,-3,2,3,11] 12 | # 输出:[4,9,9,49,121] 13 | 14 | class Solution: 15 | def sortedSquares(self, A: List[int]) -> List[int]: 16 | res = [] 17 | for i in range(len(A)): 18 | res.append(A[i]**2) 19 | res.sort() 20 | return res 21 | 22 | class Solution: 23 | def sortedSquares(self, A: List[int]) -> List[int]: 24 | return sorted(x*x for x in A) -------------------------------------------------------------------------------- /source/Clarification/Array/989.数组形式的整数加法.py: -------------------------------------------------------------------------------- 1 | # 对于非负整数 X 而言,X 的数组形式是每位数字按从左到右的顺序形成的数组。例如,如果 X = 1231,那么其数组形式为 [1,2,3,1]。 2 | # 3 | # 给定非负整数 X 的数组形式 A,返回整数 X+K 的数组形式。 4 | # 5 | #   6 | # 7 | # 示例 1: 8 | # 9 | # 输入:A = [1,2,0,0], K = 34 10 | # 输出:[1,2,3,4] 11 | # 解释:1200 + 34 = 1234 12 | 13 | class Solution: 14 | def addToArrayForm(self, A: List[int], K: int) -> List[int]: 15 | a = '' 16 | for i in A: 17 | a += str(i) # 提取a 18 | s = int(a) + K # 相加 19 | return [x for x in str(s)] -------------------------------------------------------------------------------- /source/Clarification/Array/反转字符串中的单词Ⅲ.py: -------------------------------------------------------------------------------- 1 | # 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: "Let's take LeetCode contest" 6 | # 输出: "s'teL ekat edoCteeL tsetnoc" 7 | # 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。 8 | 9 | class Solution: 10 | def reverseWords(self, s: str) -> str: 11 | res = '' 12 | i,j = 0,0 13 | while i < len(s):反转反转 14 | if s[i] == ' ': 15 | res += s[j:i][::-1] + ' ' 16 | j = i+1 17 | i += 1 18 | return res + s[j:i][::-1] -------------------------------------------------------------------------------- /source/Clarification/Array/按奇偶排序数组.py: -------------------------------------------------------------------------------- 1 | # 给定一个非负整数数组 A,返回一个数组,在该数组中, A 的所有偶数元素之后跟着所有奇数元素。 2 | # 3 | # 你可以返回满足此条件的任何数组作为答案。 4 | # 5 | #   6 | # 7 | # 示例: 8 | # 9 | # 输入:[3,1,2,4] 10 | # 输出:[2,4,3,1] 11 | # 输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。 12 | 13 | class Solution: 14 | def sortArrayByParity(self, A: List[int]) -> List[int]: 15 | return ([x for x in A if x % 2 == 0] 16 | +[x for x in A if x % 2 == 1]) -------------------------------------------------------------------------------- /source/Clarification/Array/缺失数字.py: -------------------------------------------------------------------------------- 1 | # 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: [3,0,1] 6 | # 输出: 2 7 | # 示例 2: 8 | # 9 | # 输入: [9,6,4,2,3,5,7,0,1] 10 | # 输出: 8 11 | 12 | class Solution: 13 | def missingNumber(self, nums: List[int]) -> int: 14 | l = [None]*(len(nums)+1) 15 | for i in nums: 16 | l[i] = 1 17 | for i in range(len(l)): 18 | if l[i] == None: 19 | return i 20 | -------------------------------------------------------------------------------- /source/Clarification/Array/翻转字符串里的单词.py: -------------------------------------------------------------------------------- 1 | # 给定一个字符串,逐个翻转字符串中的每个单词。 2 | # 3 | # 4 | # 5 | # 示例 6 | # 1: 7 | # 8 | # 输入: "the sky is blue" 9 | # 输出: "blue is sky the" 10 | # 示例 11 | # 采用倒序遍历,双指针法 12 | # 利用while循环 13 | class Solution: 14 | def reverseWords(self, s: str) -> str: 15 | # 去除首位空格 16 | s = s.strip() 17 | res = '' 18 | i,j = len(s) - 1,len(s) 19 | while i > 0: 20 | if s[i] == ' ': 21 | res += s[i+1:j] + ' ' 22 | while s[i] == ' ':i-=1 23 | j = i + 1 24 | i -= 1 25 | return res + s[:j] 26 | # 时间复杂度:O(N)遍历了一遍字符串 27 | # 空间复杂度:O(N)使用额外的res -------------------------------------------------------------------------------- /source/Clarification/Array/翻转游戏Ⅱ.py: -------------------------------------------------------------------------------- 1 | # 你和朋友玩一个叫做「翻转游戏」的游戏,游戏规则:给定一个只有 + 和 - 的字符串。你和朋友轮流将 连续 的两个 "++" 反转成 "--"。 当一方无法进行有效的翻转时便意味着游戏结束,则另一方获胜。 2 | # 3 | # 请你写出一个函数来判定起始玩家是否存在必胜的方案。 4 | # 5 | # 示例: 6 | # 7 | # 输入: s = "++++" 8 | # 输出: true 9 | # 解析: 起始玩家可将中间的 "++" 翻转变为 "+--+" 从而得胜。 10 | 11 | from functools import lru_cache 12 | class Solution: 13 | def canWin(self, s: str) -> bool: 14 | for i in range(len(s)-1): 15 | if s[i] == "+" and s[i+1] == "+": 16 | if not self.canWin(s[:i]+"--"+s[i+2:]): 17 | return True 18 | return False -------------------------------------------------------------------------------- /source/Clarification/Array/除自身以外数组的乘积.py: -------------------------------------------------------------------------------- 1 | # 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。 2 | # 3 | # 示例: 4 | # 5 | # 输入: [1,2,3,4] 6 | # 输出: [24,12,8,6] 7 | # 说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题。 8 | # 9 | # 进阶: 10 | # 你可以在常数空间复杂度内完成这个题目吗?( 出于对空间复杂度分析的目的,输出数组不被视为额外空间。) 11 | 12 | class Solution: 13 | def productExceptSelf(self, nums: List[int]) -> List[int]: 14 | n = len(nums) 15 | res = [0]*n 16 | k = 1 17 | for i in range(n): 18 | res[i] = k 19 | k *= nums[i] 20 | k = 1 21 | for i in range(n-1,-1,-1): 22 | res[i]*= k 23 | k *= nums[i] 24 | return res -------------------------------------------------------------------------------- /source/Clarification/Backtracking/131.分割回文串.py: -------------------------------------------------------------------------------- 1 | # 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。 2 | # 3 | # 返回 s 所有可能的分割方案。 4 | # 5 | # 示例: 6 | # 7 | # 输入: "aab" 8 | # 输出: 9 | # [ 10 | # ["aa","b"], 11 | # ["a","a","b"] 12 | # ] 13 | # 方法一:回溯 14 | class Solution: 15 | def partition(self, s: str) -> List[List[str]]: 16 | res = [] 17 | 18 | def helper(s,tmp): 19 | if not s: 20 | res.append(tmp) 21 | for i in range(1,len(s) + 1): 22 | if s[:i] == s[:i][::-1]: 23 | helper(s[i:], tmp + [s[:i]]) 24 | helper(s,[]) 25 | return res 26 | # 方法二:动态规划 27 | -------------------------------------------------------------------------------- /source/Clarification/Backtracking/90.子集2.py: -------------------------------------------------------------------------------- 1 | # 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 2 | # 3 | # 说明:解集不能包含重复的子集。 4 | # 5 | # 示例: 6 | # 7 | # 输入: [1,2,2] 8 | # 输出: 9 | # [ 10 | # [2], 11 | # [1], 12 | # [1,2,2], 13 | # [2,2], 14 | # [1,2], 15 | # [] 16 | # ] 17 | 18 | class Solution: 19 | def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: 20 | n = len(nums) 21 | nums.sort() 22 | def track_back(idx,tmp): 23 | res.append(tmp) 24 | for i in range(idx,n): 25 | if i > idx and nums[i] == nums[i-1]: 26 | continue 27 | track_back(i+1,tmp + [nums[i]]) 28 | res = [] 29 | track_back(0,[]) 30 | return res -------------------------------------------------------------------------------- /source/Clarification/Backtracking/全排列.py: -------------------------------------------------------------------------------- 1 | # 给定一个没有重复数字的序列,返回其所有可能的全排列。 2 | # 3 | # 示例: 4 | # 5 | # 输入: [1,2,3] 6 | # 输出: 7 | # [ 8 | # [1,2,3], 9 | # [1,3,2], 10 | # [2,1,3], 11 | # [2,3,1], 12 | # [3,1,2], 13 | # [3,2,1] 14 | # ] 15 | 16 | class Solution: 17 | def permute(self, nums: List[int]) -> List[List[int]]: 18 | res = [] 19 | def backtrack(nums,tmp): 20 | if not nums: 21 | res.append(tmp) 22 | return 23 | for i in range(len(nums)): 24 | backtrack(nums[:i]+nums[i+1:],tmp+[nums[i]]) 25 | backtrack(nums,[]) 26 | return res -------------------------------------------------------------------------------- /source/Clarification/Backtracking/子集.py: -------------------------------------------------------------------------------- 1 | # 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 2 | # 3 | # 说明:解集不能包含重复的子集。 4 | # 5 | # 示例: 6 | # 7 | # 输入: nums = [1,2,3] 8 | # 输出: 9 | # [ 10 | # [3], 11 | # [1], 12 | # [2], 13 | # [1,2,3], 14 | # [1,3], 15 | # [2,3], 16 | # [1,2], 17 | # [] 18 | # ] 19 | 20 | # 为什么我搞不懂递归?????? 21 | # 回溯?????????? 22 | class Solution: 23 | def subsets(self, nums: List[int]) -> List[List[int]]: 24 | res = [] 25 | n = len(nums) 26 | 27 | def helper(i, tmp): 28 | res.append(tmp) 29 | for j in range(i, n): 30 | helper(j + 1, tmp + [nums[j]]) 31 | 32 | helper(0, []) 33 | return res -------------------------------------------------------------------------------- /source/Clarification/Backtracking/生成括号.py: -------------------------------------------------------------------------------- 1 | # 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。 2 | # 3 | # 例如,给出 n = 3,生成结果为: 4 | # 5 | # [ 6 | # "((()))", 7 | # "(()())", 8 | # "(())()", 9 | # "()(())", 10 | # "()()()" 11 | # ] 12 | 13 | class Solution: 14 | def generateParenthesis(self, n: int) -> List[str]: 15 | ans = [] 16 | def backtrack(s='',left=0,right=0): 17 | if len(s) == 2 * n: 18 | ans.append(s) 19 | return 20 | if left < n: 21 | backtrack(s+'(',left+1,right) 22 | if right < left: 23 | backtrack(s+')',left,right+1) 24 | backtrack() 25 | return ans -------------------------------------------------------------------------------- /source/Clarification/Binary Search/Pow(x,n).py: -------------------------------------------------------------------------------- 1 | # 实现 pow(x, n) ,即计算 x 的 n 次幂函数。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: 2.00000, 10 6 | # 输出: 1024.00000 7 | # 示例 2: 8 | # 9 | # 输入: 2.10000, 3 10 | # 输出: 9.26100 11 | # 示例 3: 12 | # 13 | # 输入: 2.00000, -2 14 | # 输出: 0.25000 15 | # 解释: 2-2 = 1/22 = 1/4 = 0.25 16 | class Solution: 17 | def myPow(self, x: float, n: int) -> float: 18 | 19 | if n < 0: 20 | x = 1 / x 21 | n = -n 22 | 23 | res = 1 24 | while n: 25 | if n & 1: 26 | res *= x 27 | x *= x 28 | n >>= 1 29 | return res 30 | -------------------------------------------------------------------------------- /source/Clarification/Binary Search/x的平方根.py: -------------------------------------------------------------------------------- 1 | 实现 int sqrt(int x) 函数。 2 | 3 | 计算并返回 x 的平方根,其中 x 是非负整数。 4 | 5 | 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 6 | 7 | 示例 1: 8 | 9 | 输入: 4 10 | 输出: 2 11 | 示例 2: 12 | 13 | 输入: 8 14 | 输出: 2 15 | 说明: 8 的平方根是 2.82842..., 16 | 由于返回类型是整数,小数部分将被舍去。 17 | 18 | class Solution: 19 | def mySqrt(self, x: int) -> int: 20 | left = 0 21 | right = x // 2 + 1 22 | while left < right: 23 | mid = left + (right - left + 1) // 2 24 | square = mid * mid 25 | if square > x: 26 | right = mid - 1 27 | else: 28 | left = mid 29 | return left -------------------------------------------------------------------------------- /source/Clarification/Binary Search/寻找旋转排序数组中的最小值Ⅱ.py: -------------------------------------------------------------------------------- 1 | # 假设按照升序排序的数组在预先未知的某个点上进行了旋转。 2 | # 3 | # ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 4 | # 5 | # 请找出其中最小的元素。 6 | # 7 | # 注意数组中可能存在重复的元素。 8 | # 9 | # 示例 1: 10 | # 11 | # 输入: [1,3,5] 12 | # 输出: 1 13 | # 示例 2: 14 | # 15 | # 输入: [2,2,2,0,1] 16 | # 输出: 0 17 | 18 | class Solution: 19 | def findMin(self, nums: List[int]) -> int: 20 | left,right = 0,len(nums) - 1 21 | while left < right: 22 | mid = (left + right) // 2 23 | if nums[mid] > nums[right]: 24 | left = mid + 1 25 | elif nums[mid] < nums[right]: 26 | right = mid 27 | else: 28 | right = right - 1 29 | return nums[left] -------------------------------------------------------------------------------- /source/Clarification/Binary Search/有效的完全平方数.py: -------------------------------------------------------------------------------- 1 | # 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。 2 | # 3 | # 说明:不要使用任何内置的库函数,如 sqrt。 4 | # 5 | # 示例 1: 6 | # 7 | # 输入:16 8 | # 输出:True 9 | # 示例 2: 10 | # 11 | # 输入:14 12 | # 输出:False 13 | 14 | class Solution: 15 | def isPerfectSquare(self, num: int) -> bool: 16 | if num == 1: 17 | return True 18 | a = 1 19 | b = num 20 | while a <= b: 21 | mid = (a + b) // 2 22 | if mid * mid == num: 23 | return True 24 | elif mid * mid > num: 25 | b = mid - 1 26 | else: 27 | a = mid + 1 28 | return False -------------------------------------------------------------------------------- /source/Clarification/Bit_Manipulation/338.比特位计数.py: -------------------------------------------------------------------------------- 1 | # 给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: 2 6 | # 输出: [0,1,1] 7 | # 示例 2: 8 | # 9 | # 输入: 5 10 | # 输出: [0,1,1,2,1,2] 11 | # 进阶: 12 | # 13 | # 给出时间复杂度为O(n*sizeof(integer))的解答非常容易。但你可以在线性时间O(n)内用一趟扫描做到吗? 14 | # 要求算法的空间复杂度为O(n)。 15 | # 你能进一步完善解法吗?要求在C++或任何其他语言中不使用任何内置函数(如 C++ 中的 __builtin_popcount)来执行此操作。 16 | 17 | class Solution: 18 | def countBits(self, num: int) -> List[int]: 19 | dp=[0]*(num+1) 20 | for i in range(1,num+1): 21 | if(i%2==1): 22 | dp[i]=dp[i-1]+1 23 | else: 24 | dp[i]=dp[i//2] 25 | return dp -------------------------------------------------------------------------------- /source/Clarification/Dynamic_Programming/746.使用最小花费爬楼梯.py: -------------------------------------------------------------------------------- 1 | # 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始)。 2 | # 3 | # 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯。 4 | # 5 | # 您需要找到达到楼层顶部的最低花费。在开始时,你可以选择从索引为 0 或 1 的元素作为初始阶梯 6 | # 7 | class Solution: 8 | def minCostClimbingStairs(self, cost: List[int]) -> int: 9 | # 关系式:f(i) = cost(i) + min(f(i+1),f(i+2)) 10 | f1,f2 = 0,0 11 | for i in reversed(cost): # reverse从后向前遍历,因为先求f(i+1)和f(i+2)再求f(i) 12 | f1,f2 = i + min(f1,f2),f1 13 | return min(f1,f2) -------------------------------------------------------------------------------- /source/Clarification/Dynamic_Programming/不同路径.py: -------------------------------------------------------------------------------- 1 | # 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。 2 | # 3 | # 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。 4 | # 5 | # 问总共有多少条不同的路径? 6 | 7 | class Solution: 8 | def uniquePaths(self, m: int, n: int) -> int: 9 | dp = [[1]*n] + [[1]+[0]*(n-1) for _ in range(m-1)] 10 | for i in range(1,m): 11 | for j in range(1,n): 12 | dp[i][j] = dp[i-1][j] + dp[i][j-1] 13 | print(dp) 14 | return dp[-1][-1] -------------------------------------------------------------------------------- /source/Clarification/Dynamic_Programming/最长上升子序列.py: -------------------------------------------------------------------------------- 1 | # 给定一个无序的整数数组,找到其中最长上升子序列的长度。 2 | # 3 | # 示例: 4 | # 5 | # 输入: [10,9,2,5,3,7,101,18] 6 | # 输出: 4 7 | # 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。 8 | # 说明: 9 | # 10 | # 可能会有多种最长上升子序列的组合,你只需要输出对应的长度即可。 11 | # 你算法的时间复杂度应该为 O(n2) 。 12 | # 进阶: 你能将算法的时间复杂度降低到 O(n log n) 吗? 13 | 14 | class Solution: 15 | def lengthOfLIS(self, nums: List[int]) -> int: 16 | if not nums: 17 | return 0 18 | dp = [1]*len(nums) 19 | for i in range(len(nums)): 20 | for j in range(i): 21 | if nums[j] bool: 20 | max_i = 0 # 初始化最远位置 21 | for i,jump in enumerate(nums): # i为当前位置,jump为跳数 22 | if i <= max_i and i+jump > max_i: # 如果当前位置+跳数大于最远位置 23 | max_i = i + jump # 更新最远位置 24 | return max_i>= len(nums)-1 -------------------------------------------------------------------------------- /source/Clarification/Graph/二叉搜索树中第k小的元素.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | # 方法:中序遍历二叉搜索树,返回第k小的元素 8 | class Solution: 9 | def kthSmallest(self, root: TreeNode, k: int) -> int: 10 | st = [] 11 | p = root 12 | s = 0 13 | while p is not None or st: 14 | while p is not None: 15 | st.append(p) 16 | p = p.left 17 | p = st.pop() 18 | s += 1 19 | if s == k: 20 | return p.val 21 | p = p.right 22 | # 时间复杂度:O(N),N为树中节点个数 23 | # 空间复杂度:O(log(N)) -------------------------------------------------------------------------------- /source/Clarification/Graph/将有序数组转换为二叉搜索树.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | # 方法:递归 8 | class Solution: 9 | def sortedArrayToBST(self, nums: List[int]) -> TreeNode: 10 | if nums is None: 11 | return None 12 | begin = 0 13 | end = len(nums) - 1 14 | if begin > end: 15 | return None 16 | mid = (begin + end) >> 1 17 | root = TreeNode(nums[mid]) 18 | root.left = self.sortedArrayToBST(nums[begin:mid]) 19 | root.right = self.sortedArrayToBST(nums[mid+1:end+1]) 20 | return root -------------------------------------------------------------------------------- /source/Clarification/LinkedList/删除排序链表中的重复元素.py: -------------------------------------------------------------------------------- 1 | # 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: 1->1->2 6 | # 输出: 1->2 7 | # 示例 2: 8 | # 9 | # 输入: 1->1->2->3->3 10 | # 输出: 1->2->3 11 | # Definition for singly-linked list. 12 | # class ListNode: 13 | # def __init__(self, x): 14 | # self.val = x 15 | # self.next = None 16 | 17 | class Solution: 18 | def deleteDuplicates(self, head: ListNode) -> ListNode: 19 | if not (head and head.next): 20 | return head 21 | i,j = head,head 22 | while j: 23 | if i.val != j.val: 24 | i = i.next 25 | i.val = j.val 26 | j = j.next 27 | i.next = None 28 | return head -------------------------------------------------------------------------------- /source/Clarification/Math/168.Excel表列名称.py: -------------------------------------------------------------------------------- 1 | # 给定一个正整数,返回它在 Excel 表中相对应的列名称。 2 | # 3 | # 例如, 4 | # 5 | # 1 -> A 6 | # 2 -> B 7 | # 3 -> C 8 | # ... 9 | # 26 -> Z 10 | # 27 -> AA 11 | # 28 -> AB 12 | # ... 13 | # 示例 1: 14 | # 15 | # 输入: 1 16 | # 输出: "A" 17 | # 示例 2: 18 | # 19 | # 输入: 28 20 | # 输出: "AB" 21 | # 示例 3: 22 | # 23 | # 输入: 701 24 | # 输出: "ZY" 25 | 26 | class Solution: 27 | def convertToTitle(self, n: int) -> str: 28 | res = '' 29 | while n: 30 | n,y = divmod(n, 26) # n,y分别位n%26的结果和余数 31 | if y == 0: 32 | n -= 1 33 | y = 26 34 | res = chr(y + 64) + res 35 | return res -------------------------------------------------------------------------------- /source/Clarification/Math/233.数字1的个数.py: -------------------------------------------------------------------------------- 1 | 给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数。 2 | 3 | 示例: 4 | 5 | 输入: 13 6 | 输出: 6 7 | 解释: 数字 1 出现在以下数字中: 1, 10, 11, 12, 13 。 8 | 9 | class Solution(object): 10 | def countDigitOne(self, n): 11 | """ 12 | 用递归做的,可以改成记忆化搜索,加快时间 13 | """ 14 | if n<=0: return 0 15 | if n<10: return 1 16 | last = int(str(n)[1:]) 17 | power = 10**(len(str(n))-1) 18 | high = int(str(n)[0]) 19 | if high == 1: 20 | return self.countDigitOne(last) + self.countDigitOne(power-1) + last+1 21 | else: 22 | return power+high*self.countDigitOne(power-1) + self.countDigitOne(last); 23 | -------------------------------------------------------------------------------- /source/Clarification/Math/258.各位相加.py: -------------------------------------------------------------------------------- 1 | # 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。 2 | # 3 | # 示例: 4 | # 5 | # 输入: 38 6 | # 输出: 2 7 | # 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数,所以返回 2。 8 | # 9 | class Solution: 10 | def addDigits(self, num: int) -> int: 11 | if num < 10: 12 | return num 13 | while num >= 10: 14 | res = 0 15 | while num > 0: 16 | res += num % 10 17 | num //= 10 18 | num = res 19 | return res -------------------------------------------------------------------------------- /source/Clarification/Math/263.丑数.py: -------------------------------------------------------------------------------- 1 | # 编写一个程序判断给定的数是否为丑数。 2 | # 3 | # 丑数就是只包含质因数 2, 3, 5 的正整数。 4 | # 5 | # 示例 1: 6 | # 7 | # 输入: 6 8 | # 输出: true 9 | # 解释: 6 = 2 × 3 10 | 11 | class Solution: 12 | def isUgly(self, num: int) -> bool: 13 | if num ==0: 14 | return False 15 | if num == 1: 16 | return True 17 | while num %2==0 or num%3==0 or num%5==0: 18 | if num%2==0: 19 | num/=2 20 | if num%3==0: 21 | num/=3 22 | if num%5==0: 23 | num/=5 24 | return num ==1 -------------------------------------------------------------------------------- /source/Clarification/Math/264.丑数Ⅱ.py: -------------------------------------------------------------------------------- 1 | # 写一个程序,找出第 n 个丑数。 2 | # 3 | # 丑数就是只包含质因数 2, 3, 5 的正整数。 4 | # 5 | # 示例: 6 | # 7 | # 输入: n = 10 8 | # 输出: 12 9 | # 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。 10 | # 说明:   11 | # 12 | # 1 是丑数。 13 | # n 不超过1690。 14 | class Solution: 15 | def nthUglyNumber(self, n: int) -> int: 16 | dp = [0]*n 17 | l_a = 0 18 | l_b = 0 19 | l_c = 0 20 | dp[0] = 1 21 | for i in range(1,n): 22 | dp[i] = min(2*dp[l_a],3*dp[l_b],5*dp[l_c]) 23 | if dp[i] >= 2*dp[l_a]: 24 | l_a += 1 25 | if dp[i] >= 3*dp[l_b]: 26 | l_b += 1 27 | if dp[i] >= 5* dp[l_c]: 28 | l_c += 1 29 | return dp[-1] -------------------------------------------------------------------------------- /source/Clarification/Math/2的幂.py: -------------------------------------------------------------------------------- 1 | # 给定一个整数,编写一个函数来判断它是否是 2 的幂次方。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: 1 6 | # 输出: true 7 | # 解释: 20 = 1 8 | # 示例 2: 9 | # 10 | # 输入: 16 11 | # 输出: true 12 | # 解释: 24 = 16 13 | # 示例 3: 14 | # 15 | # 输入: 218 16 | # 输出: false 17 | # 方法一 18 | class Solution: 19 | def isPowerOfTwo(self, n: int) -> bool: 20 | if n == 1 or n == 2: 21 | return True 22 | num = 2 23 | while num <= n: 24 | if num == n: 25 | return True 26 | else: 27 | num *= 2 28 | return False 29 | # 方法二 30 | class Solution: 31 | def isPowerOfTwo(self, n: int) -> bool: 32 | return n > 0 and n &(n-1) == 0 -------------------------------------------------------------------------------- /source/Clarification/Math/319.灯泡开关.py: -------------------------------------------------------------------------------- 1 | 初始时有 n 个灯泡关闭。 第 1 轮,你打开所有的灯泡。 第 2 轮,每两个灯泡你关闭一次。 第 3 轮,每三个灯泡切换一次开关(如果关闭则开启,如果开启则关闭)。第 i 轮,每 i 个灯泡切换一次开关。 对于第 n 轮,你只切换最后一个灯泡的开关。 找出 n 轮后有多少个亮着的灯泡。 2 | 3 | 示例: 4 | 5 | 输入: 3 6 | 输出: 1 7 | 解释: 8 | 初始时, 灯泡状态 [关闭, 关闭, 关闭]. 9 | 第一轮后, 灯泡状态 [开启, 开启, 开启]. 10 | 第二轮后, 灯泡状态 [开启, 关闭, 开启]. 11 | 第三轮后, 灯泡状态 [开启, 关闭, 关闭]. 12 | 13 | 你应该返回 1,因为只有一个灯泡还亮着。 14 | 15 | from math import sqrt 16 | class Solution: 17 | def bulbSwitch(self, n: int) -> int: 18 | return int(sqrt(n)) -------------------------------------------------------------------------------- /source/Clarification/Math/343.整数拆分.py: -------------------------------------------------------------------------------- 1 | # 给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: 2 6 | # 输出: 1 7 | # 解释: 2 = 1 + 1, 1 × 1 = 1。 8 | # 示例 2: 9 | # 10 | # 输入: 10 11 | # 输出: 36 12 | # 解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36。 13 | # 说明: 你可以假设 n 不小于 2 且不大于 58。 14 | 15 | class Solution: 16 | def integerBreak(self, n: int) -> int: 17 | if n == 2: 18 | return 1 19 | if n == 3: 20 | return 2 21 | if n == 4: 22 | return 4 23 | res = 1 24 | while n > 4: 25 | n -= 3 26 | res *= 3 27 | return res * n -------------------------------------------------------------------------------- /source/Clarification/Math/357.计算各个位数不同的数字个数.py: -------------------------------------------------------------------------------- 1 | # 给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n 。 2 | # 3 | # 示例: 4 | # 5 | # 输入: 2 6 | # 输出: 91 7 | # 解释: 答案应为除去 11,22,33,44,55,66,77,88,99 外,在 [0,100) 区间内的所有数字。 8 | 9 | class Solution: 10 | def countNumbersWithUniqueDigits(self, n: int) -> int: 11 | if n == 0: 12 | return 1 13 | if n == 1: 14 | return 10 15 | return self.countNumbersWithUniqueDigits(n - 1) + (self.countNumbersWithUniqueDigits(n - 1) - self.countNumbersWithUniqueDigits(n - 2)) * (11 - n) -------------------------------------------------------------------------------- /source/Clarification/Math/365.水壶问题.py: -------------------------------------------------------------------------------- 1 | # 有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水? 2 | # 3 | # 如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水。 4 | # 5 | # 你允许: 6 | # 7 | # 装满任意一个水壶 8 | # 清空任意一个水壶 9 | # 从一个水壶向另外一个水壶倒水,直到装满或者倒空 10 | # 示例 1: (From the famous "Die Hard" example) 11 | # 12 | # 输入: x = 3, y = 5, z = 4 13 | # 输出: True 14 | # 示例 2: 15 | # 16 | # 输入: x = 2, y = 6, z = 5 17 | # 输出: False 18 | 19 | class Solution: 20 | def canMeasureWater(self, x: int, y: int, z: int) -> bool: 21 | import math 22 | if x + y < z: 23 | return False 24 | if x == z or y == z or x + y == z: 25 | return True 26 | return z % math.gcd(x, y) == 0 -------------------------------------------------------------------------------- /source/Clarification/Math/368.最大整除子集.py: -------------------------------------------------------------------------------- 1 | # 给出一个由无重复的正整数组成的集合,找出其中最大的整除子集,子集中任意一对 (Si,Sj) 都要满足:Si % Sj = 0 或 Sj % Si = 0。 2 | # 3 | # 如果有多个目标子集,返回其中任何一个均可。 4 | # 5 | #   6 | # 7 | # 示例 1: 8 | # 9 | # 输入: [1,2,3] 10 | # 输出: [1,2] (当然, [1,3] 也正确) 11 | # 示例 2: 12 | # 13 | # 输入: [1,2,4,8] 14 | # 输出: [1,2,4,8] 15 | 16 | class Solution: 17 | def largestDivisibleSubset(self, nums: List[int]) -> List[int]: 18 | S = {-1:set()} 19 | for x in sorted(nums): 20 | S[x] = max((S[d] for d in S if x % d == 0),key = len) | {x} 21 | return list(max(S.values(),key = len)) -------------------------------------------------------------------------------- /source/Clarification/Math/372.超级次方.py: -------------------------------------------------------------------------------- 1 | # 你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: a = 2, b = [3] 6 | # 输出: 8 7 | # 示例 2: 8 | # 9 | # 输入: a = 2, b = [1,0] 10 | # 输出: 1024 11 | 12 | class Solution: 13 | def qpow(self,x,n,m): 14 | ans = 1 15 | while n > 0: 16 | if n & 1 == 1: 17 | ans = ans * x % m 18 | x = x * x % m 19 | n >>= 1 20 | return ans 21 | def superPow(self, a: int, b: List[int]) -> int: 22 | res = 1 23 | for i in b: 24 | res = self.qpow(res, 10, 1337) * self.qpow(a, i, 1337) 25 | return res % 1337 -------------------------------------------------------------------------------- /source/Clarification/Math/386.字典序排数.py: -------------------------------------------------------------------------------- 1 | # 给定一个整数 n, 返回从 1 到 n 的字典顺序。 2 | # 3 | # 例如, 4 | # 5 | # 给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] 。 6 | # 7 | # 请尽可能的优化算法的时间复杂度和空间复杂度。 输入的数据 n 小于等于 5,000,000。 8 | 9 | class Solution: 10 | def lexicalOrder(self, n: int) -> List[int]: 11 | def dfs(cur,n,res): 12 | if cur > n: 13 | return 14 | else: 15 | res.append(cur) 16 | for i in range(10): 17 | if 10 * cur + i > n: 18 | return 19 | dfs(10 * cur + i, n, res) 20 | res = [] 21 | for i in range(1,10): 22 | dfs(i, n, res) 23 | return res -------------------------------------------------------------------------------- /source/Clarification/Math/397.整数替换.py: -------------------------------------------------------------------------------- 1 | # 给定一个正整数 n,你可以做如下操作: 2 | # 3 | # 1. 如果 n 是偶数,则用 n / 2替换 n。 4 | # 2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n。 5 | # n 变为 1 所需的最小替换次数是多少? 6 | # 7 | # 示例 1: 8 | # 9 | # 输入: 10 | # 8 11 | # 12 | # 输出: 13 | # 3 14 | # 15 | # 解释: 16 | # 8 -> 4 -> 2 -> 1 17 | 18 | class Solution: 19 | def integerReplacement(self, n: int) -> int: 20 | res = 0 21 | while n != 1: 22 | if n & 1 == 0: 23 | n >>= 1 24 | elif n == 3 or n & 2 == 0: 25 | n -= 1 26 | else: 27 | n += 1 28 | res += 1 29 | return res -------------------------------------------------------------------------------- /source/Clarification/Math/3的幂.py: -------------------------------------------------------------------------------- 1 | # 给定一个整数,写一个函数来判断它是否是 3 的幂次方。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: 27 6 | # 输出: true 7 | # 示例 2: 8 | # 9 | # 输入: 0 10 | # 输出: false 11 | # 示例 3: 12 | # 13 | # 输入: 9 14 | # 输出: true 15 | # 示例 4: 16 | # 17 | # 输入: 45 18 | # 输出: false 19 | # 20 | class Solution: 21 | def isPowerOfThree(self, n: int) -> bool: 22 | if n == 0: 23 | return False 24 | # 一直除以3,看它是否能除尽 25 | while n % 3 == 0: 26 | n //= 3 27 | return n == 1 -------------------------------------------------------------------------------- /source/Clarification/Math/4的幂.py: -------------------------------------------------------------------------------- 1 | # 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: 16 6 | # 输出: true 7 | # 示例 2: 8 | # 9 | # 输入: 5 10 | # 输出: false 11 | # 方法一 12 | class Solution: 13 | def isPowerOfFour(self, num: int) -> bool: 14 | if num == 0: 15 | return False 16 | while num % 4 == 0: 17 | num //= 4 18 | return num == 1 19 | 20 | # 方法二 21 | class Solution: 22 | def isPowerOfFour(self, num: int) -> bool: 23 | if num <= 0: 24 | return False 25 | # 首先判断是否为2的幂 26 | if num & (num-1) != 0: 27 | return False 28 | if num & 0x55555555 == num: 29 | return True 30 | return False -------------------------------------------------------------------------------- /source/Clarification/Math/507.完美数.py: -------------------------------------------------------------------------------- 1 | # 对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”。 2 | # 3 | # 给定一个 整数 n, 如果他是完美数,返回 True,否则返回 False 4 | # 5 | #   6 | # 7 | # 示例: 8 | # 9 | # 输入: 28 10 | # 输出: True 11 | # 解释: 28 = 1 + 2 + 4 + 7 + 14 12 | 13 | class Solution: 14 | def checkPerfectNumber(self, num: int) -> bool: 15 | def helper(n): 16 | return (2 ** (n - 1)) * (2 ** n - 1) 17 | 18 | list_su = [2, 3, 5, 7, 13, 17, 19, 31] 19 | for i in list_su: 20 | if helper(i) == num: 21 | return True 22 | return False 23 | 24 | -------------------------------------------------------------------------------- /source/Clarification/Math/633.平方数之和.py: -------------------------------------------------------------------------------- 1 | # 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c。 2 | # 3 | # 示例1: 4 | # 5 | # 输入: 5 6 | # 输出: True 7 | # 解释: 1 * 1 + 2 * 2 = 5 8 | #   9 | # 10 | # 示例2: 11 | # 12 | # 输入: 3 13 | # 输出: False 14 | 15 | import math 16 | class Solution: 17 | def judgeSquareSum(self, c: int) -> bool: 18 | i = 0 19 | while i <= c ** 0.5: 20 | b = math.sqrt(c-i**2) 21 | if b == int(b): 22 | return True 23 | i += 1 24 | return False -------------------------------------------------------------------------------- /source/Clarification/Math/812.最大三角形面积.py: -------------------------------------------------------------------------------- 1 | # 给定包含多个点的集合,从其中取三个点组成三角形,返回能组成的最大三角形的面积。 2 | # 3 | # 示例: 4 | # 输入: points = [[0,0],[0,1],[1,0],[0,2],[2,0]] 5 | # 输出: 2 6 | # 解释: 7 | # 这五个点如下图所示。组成的橙色三角形是最大的,面积为2。 8 | 9 | class Solution(object): 10 | def largestTriangleArea(self, points): 11 | def area(p, q, r): 12 | return .5 * abs(p[0]*q[1]+q[0]*r[1]+r[0]*p[1] 13 | -p[1]*q[0]-q[1]*r[0]-r[1]*p[0]) 14 | 15 | return max(area(*triangle) 16 | for triangle in itertools.combinations(points, 3)) 17 | -------------------------------------------------------------------------------- /source/Clarification/Math/976.三角形的最大周长.py: -------------------------------------------------------------------------------- 1 | # 给定由一些正数(代表长度)组成的数组 A,返回由其中三个长度组成的、面积不为零的三角形的最大周长。 2 | # 3 | # 如果不能形成任何面积不为零的三角形,返回 0。 4 | # 5 | #   6 | # 7 | # 示例 1: 8 | # 9 | # 输入:[2,1,2] 10 | # 输出:5 11 | # 示例 2: 12 | # 13 | # 输入:[1,2,1] 14 | # 输出:0 15 | 16 | class Solution: 17 | def largestPerimeter(self, A: List[int]) -> int: 18 | A.sort() 19 | for i in range(len(A)-3,-1,-1): 20 | if A[i] + A[i+1] > A[i+2]: 21 | return A[i] + A[i+1] + A[i+2] 22 | return 0处。 -------------------------------------------------------------------------------- /source/Clarification/Math/回文数.py: -------------------------------------------------------------------------------- 1 | # 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: 121 6 | # 输出: true 7 | # 示例 2: 8 | # 9 | # 输入: -121 10 | # 输出: false 11 | # 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。 12 | # 示例 3: 13 | # 14 | # 输入: 10 15 | # 输出: false 16 | # 解释: 从右向左读, 为 01 。因此它不是一个回文数。 17 | 18 | class Solution: 19 | def isPalindrome(self, x: int) -> bool: 20 | tmp = x 21 | if x < 0: 22 | return False 23 | res = 0 24 | while tmp > 0: 25 | last_num = tmp % 10 26 | res = res * 10 + last_num 27 | tmp //= 10 28 | return res == x -------------------------------------------------------------------------------- /source/Clarification/Math/数字的补数.py: -------------------------------------------------------------------------------- 1 | # 给定一个正整数,输出它的补数。补数是对该数的二进制表示取反。 2 | # 3 | # 注意: 4 | # 5 | # 给定的整数保证在32位带符号整数的范围内。 6 | # 你可以假定二进制数不包含前导零位。 7 | # 示例 1: 8 | # 9 | # 输入: 5 10 | # 输出: 2 11 | # 解释: 5的二进制表示为101(没有前导零位),其补数为010。所以你需要输出2。 12 | # 示例 2: 13 | # 14 | # 输入: 1 15 | # 输出: 0 16 | # 解释: 1的二进制表示为1(没有前导零位),其补数为0。所以你需要输出0。 17 | 18 | class Solution: 19 | def findComplement(self, num: int) -> int: 20 | ans = 0 21 | a = bin(num)[2:] # 转换为二进制的字符串格式 22 | for i in range(-1,-len(a)-1,-1): 23 | if a[i] == '0': 24 | ans += 2**(-i-1) 25 | return ans -------------------------------------------------------------------------------- /source/Clarification/Math/阶乘后的零.py: -------------------------------------------------------------------------------- 1 | # 给定一个整数 n,返回 n! 结果尾数中零的数量。 2 | # 3 | # 示例 1: 4 | # 5 | # 输入: 3 6 | # 输出: 0 7 | # 解释: 3! = 6, 尾数中没有零。 8 | # 示例 2: 9 | # 10 | # 输入: 5 11 | # 输出: 1 12 | # 解释: 5! = 120, 尾数中有 1 个零. 13 | # 说明: 你算法的时间复杂度应为 O(log n) 。 14 | 15 | class Solution: 16 | def trailingZeroes(self, n: int) -> int: 17 | p = 0 18 | while n >= 5: 19 | n = n // 5 20 | p += n 21 | return p -------------------------------------------------------------------------------- /source/Clarification/Matrix/378.有序矩阵中第K小的元素.py: -------------------------------------------------------------------------------- 1 | # 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。 2 | # 请注意,它是排序后的第k小元素,而不是第k个元素。 3 | # 4 | # 示例: 5 | # 6 | # matrix = [ 7 | # [ 1, 5, 9], 8 | # [10, 11, 13], 9 | # [12, 13, 15] 10 | # ], 11 | # k = 8, 12 | # 13 | # 返回 13。 14 | 15 | import heapq 16 | class Solution: 17 | def kthSmallest(self, matrix: List[List[int]], k: int) -> int: 18 | # 将数组拉平 19 | array = [y for x in matrix for y in x] 20 | return heapq.nsmallest(k,array)[-1] -------------------------------------------------------------------------------- /source/Clarification/Stack_PriorityQueue/每日温度.py: -------------------------------------------------------------------------------- 1 | # 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数。如果之后都不会升高,请在该位置用 0 来代替。 2 | # 3 | # 例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。 4 | # 5 | # 提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。 6 | 7 | class Solution: 8 | def dailyTemperatures(self, T: List[int]) -> List[int]: 9 | ans = [0] * len(T) 10 | stack = [] 11 | for i in range(len(T) - 1,-1,-1): 12 | while stack and T[i] >= T[stack[-1]]: 13 | stack.pop() 14 | if stack: 15 | ans[i] = stack[-1] - i 16 | stack.append(i) 17 | return ans -------------------------------------------------------------------------------- /source/Clarification/Stack_PriorityQueue/逆波兰表达式求值.py: -------------------------------------------------------------------------------- 1 | # 根据逆波兰表示法,求表达式的值。 2 | # 3 | # 有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。 4 | # 5 | # 说明: 6 | # 7 | # 整数除法只保留整数部分。 8 | # 给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。 9 | # 示例 1: 10 | # 11 | # 输入: ["2", "1", "+", "3", "*"] 12 | # 输出: 9 13 | # 解释: ((2 + 1) * 3) = 9 14 | 15 | class Solution: 16 | def evalRPN(self, tokens: List[str]) -> int: 17 | # eval函数用来执行一个字符串表达式,返回表达式的值 18 | # 用栈来处理 19 | stack = [] 20 | for i in tokens: 21 | if i in {'+','-','*','/'}: 22 | a,b = stack.pop(),stack.pop() 23 | stack.append(str(int(eval(b + i + a)))) 24 | else: 25 | stack.append(i) 26 | return stack[0] -------------------------------------------------------------------------------- /source/Clarification/Tree/255.验证前序遍历序列二叉搜索树.py: -------------------------------------------------------------------------------- 1 | # 给定一个整数数组,你需要验证它是否是一个二叉搜索树正确的先序遍历序列。 2 | # 3 | # 你可以假定该序列中的数都是不相同的。 4 | # 5 | # 参考以下这颗二叉搜索树: 6 | # 7 | # 5 8 | # / \ 9 | # 2 6 10 | # / \ 11 | # 1 3 12 | # 示例 1: 13 | # 14 | # 输入: [5,2,6,1,3] 15 | # 输出: false 16 | # 示例 2: 17 | # 18 | # 输入: [5,2,1,3,6] 19 | # 输出: true 20 | 21 | class Solution: 22 | def verifyPreorder(self, preorder: List[int]) -> bool: 23 | stack = [] 24 | root = float('-inf') 25 | for i in range(len(preorder)): 26 | if preorder[i] < root: return False 27 | while stack and preorder[i] > stack[-1]: 28 | root = stack.pop() 29 | stack.append(preorder[i]) 30 | return True -------------------------------------------------------------------------------- /source/Clarification/Tree/96.不同的二叉搜索树.py: -------------------------------------------------------------------------------- 1 | # 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 2 | # 3 | # 示例: 4 | # 5 | # 输入: 3 6 | # 输出: 5 7 | # 解释: 8 | # 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 9 | # 10 | # 1 3 3 2 1 11 | # \ / / / \ \ 12 | # 3 2 1 1 3 2 13 | # / / \ \ 14 | # 2 1 2 3 15 | 16 | class Solution: 17 | def numTrees(self, n: int) -> int: 18 | G = [0] * (n+1) 19 | G[0],G[1] = 1,1 20 | for i in range(2,n+1): 21 | for j in range(1,i+1): 22 | G[i] += G[j - 1] * G[i - j] 23 | return G[n] -------------------------------------------------------------------------------- /source/weeks/1000002.找出隐藏信息.java: -------------------------------------------------------------------------------- 1 | // xm 2 | // 和同事们出完题后开始互相验题,结果 3 | // xm 4 | // 一直卡在其中一道题目上愁眉苦脸,这时那题的出题人发过来一张图片和一句话“只能帮你到这了”。 xm 5 | // 认为解出那题的关键隐藏在这张图片中,但是 6 | // xm 7 | // 盯着图片看了很久也不知道图片隐藏的信息,只好请聪明的你帮忙找出图片隐藏的信息。 8 | 9 | class Solution { 10 | public String findHiddenInfo(int placeholder) { 11 | return "明年再战狼人杀!"; 12 | } 13 | } -------------------------------------------------------------------------------- /source/weeks/5307. 将整数转换为两个无零整数的和.py: -------------------------------------------------------------------------------- 1 | # 无零整数」是十进制表示中不含任何0的正整数。 2 | # 3 | # 给你一个整数n,请你返回一个由两个整数组成的列表[A, B],满足: 4 | # 5 | # A和B都是无零整数 6 | # A + B = n 7 | # 题目数据保证至少有一个有效的解决方案。 8 | # 9 | # 如果存在多个有效解决方案,你可以返回其中任意一个。 10 | # 11 | # 12 | # 13 | # 示例1: 14 | # 15 | # 输入:n = 2 16 | # 输出:[1, 1] 17 | # 解释:A = 1, B = 1.A + B = n并且A和B的十进制表示形式都不包含任何0 。 18 | class Solution: 19 | def getNoZeroIntegers(self, n: int) -> List[int]: 20 | for i in range(1,n): 21 | if '0' not in str(i) and '0' not in str(n-i): 22 | return [i,n-i] -------------------------------------------------------------------------------- /source/weeks/5328.方针中战斗力最弱的k行.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]: 3 | dic = {} 4 | for i in range(len(mat)): 5 | dic[i] = mat[i].count(1) 6 | sort_dic = sorted(dic.items(),key = lambda x:x[1]) 7 | res = [] 8 | i = 0 9 | for key,value in sort_dic: 10 | res.append(key) 11 | i += 1 12 | if i == k: 13 | break 14 | return res -------------------------------------------------------------------------------- /source/weeks/5329.数组大小减半.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSetSize(self, arr: List[int]) -> int: 3 | from collections import Counter 4 | n = len(arr) 5 | dic = Counter(arr) 6 | sorted_dic = sorted(dic.items(),key = lambda x:x[1],reverse = True) 7 | ans = 0 8 | for num, count in sorted_dic: 9 | n -= count 10 | ans += 1 11 | if n <= len(arr) / 2: 12 | break 13 | return ans -------------------------------------------------------------------------------- /source/weeks/5330.分裂二叉树的最大乘积.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | 8 | class Solution: 9 | def maxProduct(self, root: TreeNode) -> int: 10 | def getSum(root): 11 | if not root: 12 | return 0 13 | ret = getSum(root.left) + getSum(root.right) + root.val 14 | hashmap[root] = ret 15 | return ret 16 | hashmap = {} 17 | totalSum = getSum(root) 18 | ans = 0 19 | for k in hashmap: 20 | ans = max(ans,(totalSum - hashmap[k]) * hashmap[k]) 21 | return ans % (10**9+7) -------------------------------------------------------------------------------- /source/weeks/5331.跳跃游戏Ⅴ.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxJumps(self, arr: List[int], d: int) -> int: 3 | D = {} 4 | l = len(arr) 5 | def P(n): 6 | if n in D: 7 | return D[n] 8 | t = 1 9 | for i in range(1,d+1): 10 | if n + i >= l or arr[n] <= arr[n + i]: 11 | break 12 | t = max(t, 1+ P(n + i)) 13 | for i in range(1, d + 1): 14 | if n - i < 0 or arr[n] <= arr[n - i]: 15 | break 16 | t = max(t, 1 + P(n - i)) 17 | D[n] = t 18 | return t 19 | return max(P(i) for i in range(l)) -------------------------------------------------------------------------------- /source/weeks/全排列.py: -------------------------------------------------------------------------------- 1 | # 给定一个没有重复数字的序列,返回其所有可能的全排列。 2 | # 3 | # 示例: 4 | # 5 | # 输入: [1,2,3] 6 | # 输出: 7 | # [ 8 | # [1,2,3], 9 | # [1,3,2], 10 | # [2,1,3], 11 | # [2,3,1], 12 | # [3,1,2], 13 | # [3,2,1] 14 | # ] 15 | 16 | class Solution: 17 | def permute(self, nums: List[int]) -> List[List[int]]: 18 | res = [] 19 | def backtrack(nums,tmp): 20 | if not nums: 21 | res.append(tmp) 22 | return 23 | for i in range(len(nums)): 24 | backtrack(nums[:i]+nums[i+1:],tmp+[nums[i]]) 25 | backtrack(nums,[]) 26 | return res --------------------------------------------------------------------------------