leetcode easy
文章平均质量分 63
keep moving 123
这个作者很懒,什么都没留下…
展开
-
226. 翻转二叉树(python3)
题目:力扣翻转一棵二叉树。示例:输入: 4 / \ 2 7/ \ / \1 3 6 9输出: 4 / \ 7 2/ \ / \9 6 3 1代码:class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if not root: return None...原创 2021-11-09 11:14:20 · 79 阅读 · 0 评论 -
617. 合并二叉树(python3)
题目:力扣给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为NULL 的节点将直接作为新二叉树的节点。示例1:输入: Tree 1 Tree 2 1 2 ...原创 2021-11-09 10:13:06 · 261 阅读 · 0 评论 -
543. 二叉树的直径(python3)
题目:力扣给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回3, 它的长度是路径 [4,2,1,3] 或者[5,2,1,3]。注意:两结点之间的路径长度是以它们之间边的数目表示。解题思路:左子树+右子树+1(父节点)代表路径长...原创 2021-11-04 11:08:37 · 161 阅读 · 0 评论 -
461. 汉明距离(python3)
题目:力扣两个整数之间的 汉明距离 指的是这两个数字对应二进制位不同的位置的数目。给你两个整数 x 和 y,计算并返回它们之间的汉明距离。示例 1:输入:x = 1, y = 4输出:2解释:1 (0 0 0 1)4 (0 1 0 0) ↑ ↑上面的箭头指出了对应二进制位不同的位置。示例 2:输入:x = 3, y = 1输出:1提示:0 <=x, y <= 231 - 1解题思路:异或运算x^y,再计算结...原创 2021-11-03 18:43:59 · 244 阅读 · 0 评论 -
448. 找到所有数组中消失的数字(python3)
题目:力扣给你一个含 n 个整数的数组 nums ,其中 nums[i] 在区间 [1, n] 内。请你找出所有在 [1, n] 范围内但没有出现在 nums 中的数字,并以数组的形式返回结果。示例 1:输入:nums = [4,3,2,7,8,2,3,1]输出:[5,6]示例 2:输入:nums = [1,1]输出:[2]提示:n == nums.length1 <= n <= 1051 <= nums[i] <= n进阶:你能在不使用额.原创 2021-11-03 18:07:32 · 267 阅读 · 0 评论 -
338. 比特位计数(python3)
题目:力扣给你一个整数 n ,对于0 <= i <= n 中的每个 i ,计算其二进制表示中 1 的个数 ,返回一个长度为 n + 1 的数组 ans 作为答案。示例 1:输入:n = 2输出:[0,1,1]解释:0 --> 01 --> 12 --> 10示例 2:输入:n = 5输出:[0,1,1,2,1,2]解释:0 --> 01 --> 12 --> 103 --> 114 --> 1005.原创 2021-11-03 15:02:09 · 148 阅读 · 0 评论 -
283. 移动零(python3)
题目:力扣给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。示例:输入: [0,1,0,3,12]输出: [1,3,12,0,0]说明:必须在原数组上操作,不能拷贝额外的数组。尽量减少操作次数。解题思路:代码:class Solution: def moveZeroes(self, nums: List[int]) -> None: n = len(nums) left =原创 2021-11-03 10:36:36 · 425 阅读 · 0 评论 -
234. 回文链表(python3)
题目:力扣给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。示例 1:输入:head = [1,2,2,1]输出:true示例 2:输入:head = [1,2]输出:false提示:链表中节点数目在范围[1, 105] 内0 <= Node.val <= 9进阶:你能否用O(n) 时间复杂度和 O(1) 空间复杂度解决此题?解题思路:将链...原创 2021-11-01 16:54:00 · 168 阅读 · 0 评论 -
169. 多数元素(python3)
给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于⌊ n/2 ⌋的元素。你可以假设数组是非空的,并且给定的数组总是存在多数元素。示例1:输入:[3,2,3]输出:3示例2:输入:[2,2,1,1,1,2,2]输出:2进阶:尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。解题思路:代码:class Solution: def majorityElement(self, nu...原创 2021-11-01 14:22:50 · 192 阅读 · 0 评论 -
206. 反转链表(python3)
题目:力扣给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。示例 1:输入:head = [1,2,3,4,5]输出:[5,4,3,2,1]示例 2:输入:head = [1,2]输出:[2,1]示例 3:输入:head = []输出:[]提示:链表中节点的数目范围是 [0, 5000]-5000 <= Node.val <= 5000解题思路:置三个指针,分别代表前一个位置pre,当前位置cur和下一个位置fol,...原创 2021-11-01 14:05:30 · 213 阅读 · 0 评论 -
160. 相交链表(python3)
给你两个单链表的头节点headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。图示两个链表在节点 c1 开始相交:题目数据 保证 整个链式结构中不存在环。注意,函数返回结果后,链表必须 保持其原始结构 。示例 1:输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3输出:Intersected at ..原创 2021-10-28 16:52:49 · 153 阅读 · 0 评论 -
141. 环形链表(python3)
题目:力扣给定一个链表,判断链表中是否有环。如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。如果链表中存在环,则返回 true 。 否则,返回 false 。进阶:你能用 O(1)(即,常量)内存解决此问题吗?示例 1:输入:h.原创 2021-10-19 16:39:07 · 133 阅读 · 0 评论 -
136. 只出现一次的数字(python3)
题目:力扣给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。说明:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?示例 1:输入: [2,2,1]输出: 1示例2:输入: [4,1,2,1,2]输出: 4解题思路:线性复杂度:直接用list的count函数,set记录已有的再遇到相同的删除,dict记录数和出现次数等方法o(1)存储空间:没有思路,参考别人方案,使用位运算的异或运算处理。异.原创 2021-09-28 16:51:35 · 94 阅读 · 0 评论 -
104. 二叉树的最大深度(python3)
题目:力扣给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明:叶子节点是指没有子节点的节点。示例:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回它的最大深度3 。代码:# Definition for a binary tree node.# class TreeNode:# def __init__(self, va...原创 2021-09-28 14:32:29 · 99 阅读 · 0 评论 -
101. 对称二叉树(python3)
题目:力扣给定一个二叉树,检查它是否是镜像对称的。例如,二叉树[1,2,2,3,4,4,3] 是对称的。 1 / \ 2 2/ \ / \3 4 4 3但是下面这个[1,2,2,null,3,null,3] 则不是镜像对称的: 1 / \ 2 2 \ \ 3 3进阶:你可以运用递归和迭代两种方法解决这个问题吗?解题思路:递归比较左子树的左孩子与右子树的右孩子,左子树的右孩子和右子树...原创 2021-09-28 14:30:48 · 104 阅读 · 0 评论 -
94. 二叉树的中序遍历(python3)
题目:力扣给定一个二叉树的根节点 root ,返回它的 中序遍历。示例 1:输入:root = [1,null,2,3]输出:[1,3,2]示例 2:输入:root = []输出:[]示例 3:输入:root = [1]输出:[1]示例 4:输入:root = [1,2]输出:[2,1]示例 5:输入:root = [1,null,2]输出:[1,2]提示:树中节点数目在范围 [0, 100] 内-100 <= Node.val.原创 2021-09-23 14:44:25 · 397 阅读 · 0 评论 -
21. 合并两个有序链表(python3)
题目:力扣将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。示例 1:输入:l1 = [1,2,4], l2 = [1,3,4]输出:[1,1,2,3,4,4]示例 2:输入:l1 = [], l2 = []输出:[]示例 3:输入:l1 = [], l2 = [0]输出:[0]提示:两个链表的节点数目范围是 [0, 50]-100 <= Node.val <= 100l1 和 l2 均按 非递减..原创 2021-09-22 16:25:17 · 262 阅读 · 0 评论 -
20. 有效的括号(python3)
题目:力扣给定一个只包括 '(',')','{','}','[',']'的字符串 s ,判断字符串是否有效。有效字符串需满足:左括号必须用相同类型的右括号闭合。左括号必须以正确的顺序闭合。示例 1:输入:s = "()"输出:true示例2:输入:s = "()[]{}"输出:true示例3:输入:s = "(]"输出:false示例4:输入:s = "([)]"输出:false示例5:输入:s = "{[]}"输出:true提示...原创 2021-09-22 14:37:27 · 310 阅读 · 0 评论 -
1435. 制作会话柱状图(SQL)
题目:https://leetcode-cn.com/problems/create-a-session-bar-chart/表:Sessions+---------------------+---------+| Column Name | Type |+---------------------+---------+| session_id | int || duration | int |+--------...原创 2021-02-04 10:15:06 · 221 阅读 · 0 评论 -
1179. 重新格式化部门表(SQL)
题目:https://leetcode-cn.com/problems/reformat-department-table/部门表Department:+---------------+---------+| Column Name | Type |+---------------+---------+| id | int || revenue | int || month | varchar |+------...原创 2021-02-03 16:42:37 · 129 阅读 · 0 评论 -
597. 好友申请 I:总体通过率(SQL)
题目:在 Facebook 或者 Twitter 这样的社交应用中,人们经常会发好友申请也会收到其他人的好友申请。表:FriendRequest+----------------+---------+| Column Name | Type |+----------------+---------+| sender_id | int || send_to_id | int || request_date | date |+...原创 2021-02-03 13:46:07 · 289 阅读 · 0 评论 -
1142. 过去30天的用户活动 II(SQL)
题目:https://leetcode-cn.com/problems/user-activity-for-the-past-30-days-ii/Table: Activity+---------------+---------+| Column Name | Type |+---------------+---------+| user_id | int || session_id | int || activity_date | date ...原创 2021-02-03 11:19:52 · 293 阅读 · 0 评论 -
596. 超过5名学生的课(SQL)
题目:https://leetcode-cn.com/problems/classes-more-than-5-students/有一个courses 表 ,有: student(学生) 和 class (课程)。请列出所有超过或等于5名学生的课。例如,表:+---------+------------+| student | class |+---------+------------+| A | Math || B | Englis...原创 2021-02-03 10:50:51 · 110 阅读 · 0 评论 -
1280. 学生们参加各科测试的次数(SQL)
题目:https://leetcode-cn.com/problems/students-and-examinations/学生表: Students+---------------+---------+| Column Name | Type |+---------------+---------+| student_id | int || student_name | varchar |+---------------+---------+主键为 stude...原创 2021-02-03 09:31:24 · 475 阅读 · 0 评论 -
619. 只出现一次的最大数字(SQL)
题目:https://leetcode-cn.com/problems/biggest-single-number/表my_numbers的 num字段包含很多数字,其中包括很多重复的数字。你能写一个 SQL 查询语句,找到只出现过一次的数字中,最大的一个数字吗?+---+|num|+---+| 8 || 8 || 3 || 3 || 1 || 4 || 5 || 6 |对于上面给出的样例数据,你的查询语句应该返回如下结果:+---+|num|+---+|...原创 2021-02-03 15:42:55 · 207 阅读 · 0 评论 -
1076. 项目员工II(SQL)
题目:https://leetcode-cn.com/problems/project-employees-ii/Table:Project+-------------+---------+| Column Name | Type |+-------------+---------+| project_id | int || employee_id | int |+-------------+---------+主键为 (project_id, employee...原创 2021-02-03 21:59:16 · 324 阅读 · 0 评论 -
1731. The Number of Employees Which Report to Each Employee(SQL)
题目:https://leetcode-cn.com/problems/the-number-of-employees-which-report-to-each-employee/Table: Employees+-------------+----------+| Column Name | Type |+-------------+----------+| employee_id | int || name | varchar || reports_t...原创 2021-02-02 17:11:51 · 297 阅读 · 0 评论 -
1141. 查询近30天活跃用户数(SQL)
题目:https://leetcode-cn.com/problems/user-activity-for-the-past-30-days-i/活动记录表:Activity+---------------+---------+| Column Name | Type |+---------------+---------+| user_id | int || session_id | int || activity_date | date ...原创 2021-02-02 16:26:17 · 1761 阅读 · 0 评论 -
1083. 销售分析 II(SQL)
题目:https://leetcode-cn.com/problems/sales-analysis-ii/Table:Product+--------------+---------+| Column Name | Type |+--------------+---------+| product_id | int || product_name | varchar || unit_price | int |+--------------+------...原创 2021-02-02 16:15:27 · 275 阅读 · 0 评论 -
1543. 产品名称格式修复(SQL)
题目:https://leetcode-cn.com/problems/fix-product-name-format/表:Sales+--------------+---------+| Column Name | Type |+--------------+---------+| sale_id | int || product_name | varchar || sale_date | date |+--------------+-------...原创 2021-02-02 15:31:02 · 245 阅读 · 0 评论 -
197. 上升的温度(SQL)
题目:https://leetcode-cn.com/problems/rising-temperature/表 Weather+---------------+---------+| Column Name | Type |+---------------+---------+| id | int || recordDate | date || temperature | int |+---------------+---...原创 2021-02-02 16:01:17 · 147 阅读 · 0 评论 -
512. 游戏玩法分析 II(SQL)
题目:https://leetcode-cn.com/problems/game-play-analysis-ii/Table:Activity+--------------+---------+| Column Name | Type |+--------------+---------+| player_id | int || device_id | int || event_date | date || games_played | ...原创 2021-02-02 14:20:59 · 216 阅读 · 0 评论 -
1084. 销售分析III(SQL)
题目:https://leetcode-cn.com/problems/sales-analysis-iii/Table:Product+--------------+---------+| Column Name | Type |+--------------+---------+| product_id | int || product_name | varchar || unit_price | int |+--------------+-----...原创 2021-02-01 18:54:39 · 210 阅读 · 0 评论 -
1113. 报告的记录(SQL)
题目:https://leetcode-cn.com/problems/reported-posts/动作表:Actions+---------------+---------+| Column Name | Type |+---------------+---------+| user_id | int || post_id | int || action_date | date || action | enu...原创 2021-02-01 17:17:57 · 134 阅读 · 0 评论 -
1495. 上月播放的儿童适宜电影(SQL)
题目:https://leetcode-cn.com/problems/friendly-movies-streamed-last-month/表: TVProgram+---------------+---------+| Column Name | Type |+---------------+---------+| program_date | date || content_id | int || channel | varchar |...原创 2021-02-01 13:47:24 · 567 阅读 · 0 评论 -
1607. 没有卖出的卖家(SQL)
题目:https://leetcode-cn.com/problems/sellers-with-no-sales/表: Customer+---------------+---------+| Column Name | Type |+---------------+---------+| customer_id | int || customer_name | varchar |+---------------+---------+customer_id 是该表...原创 2021-02-01 13:40:02 · 249 阅读 · 1 评论 -
1741. Find Total Time Spent by Each Employee(SQL)
题目:https://leetcode-cn.com/problems/find-total-time-spent-by-each-employee/Table: Employees+-------------+------+| Column Name | Type |+-------------+------+| emp_id | int || event_day | date || in_time | int || out_time | int |...原创 2021-02-01 13:17:17 · 368 阅读 · 0 评论 -
1322. 广告效果(SQL)
题目:https://leetcode-cn.com/problems/number-of-comments-per-post/solution/表Submissions 结构如下:+---------------+----------+| 列名 | 类型 |+---------------+----------+| sub_id | int || parent_id | int |+---------------+-...原创 2021-02-01 13:24:40 · 184 阅读 · 0 评论 -
1241. 每个帖子的评论数(SQL)
题目:https://leetcode-cn.com/problems/number-of-comments-per-post/表Submissions 结构如下:+---------------+----------+| 列名 | 类型 |+---------------+----------+| sub_id | int || parent_id | int |+---------------+----------...原创 2021-02-01 13:34:57 · 434 阅读 · 0 评论 -
1667. 修复表中的名字(SQL)
题目:https://leetcode-cn.com/problems/fix-names-in-a-table/表: Users+----------------+---------+| Column Name | Type |+----------------+---------+| user_id | int || name | varchar |+----------------+---------+user_id 是该表的主...原创 2021-02-02 15:06:46 · 244 阅读 · 0 评论