Leetcode 0032.longest-valid-parentheses(python)
32. Longest Valid Parentheses题目Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. Example 1: 123Input: s = "(()"Output: 2Explanation: The longest valid parentheses substring is "()". Example 2: 123Input: s = ")()())"Output: 4Explanation: The longest valid parentheses substring is "()()". Example 3: 12Input: s = ""Output: 0 题目大意给定一个只包含 '(...
Leetcode 0032.longest-valid-parentheses
32. Longest Valid Parentheses题目Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. Example 1: 123Input: s = "(()"Output: 2Explanation: The longest valid parentheses substring is "()". Example 2: 123Input: s = ")()())"Output: 4Explanation: The longest valid parentheses substring is "()()". Example 3: 12Input: s = ""Output: 0 题目大意给定一个只包含 '(...
Leetcode 0031.next-permutation(python)
31. Next Permutation题目A permutation of an array of integers is an arrangement of its members into a sequence or linear order. For example, for arr = [1,2,3], the following are all the permutations of arr: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]. The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then ...
Leetcode 0031.next-permutation
31. Next Permutation题目A permutation of an array of integers is an arrangement of its members into a sequence or linear order. For example, for arr = [1,2,3], the following are all the permutations of arr: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]. The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then ...
Leetcode 0030.Substring with Concatenation of All Words(python)
30. Substring with Concatenation of All Words一、问题描述给定一个字符串 s 和一个字符串数组 words,找出 s 中所有恰好由 words 中所有单词串联形成的子串的起始索引。 注意:words 中的单词可以以任意顺序串联。 示例 1: 12输入:s = "barfoothefoobarman", words = ["foo","bar"]输出:[0,9] 示例 2: 12输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]输出:[] 示例 3: 12输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]输出:[6,9,12] 二...
Leetcode 0030.Substring with Concatenation of All Words(C++)
30. Substring with Concatenation of All Words题目You are given a string s and an array of strings words of the same length. Return all starting indices of substring(s) in s that is a concatenation of each word in words exactly once, in any order, and without any intervening characters. You can return the answer in any order. Example 1: 1234Input: s = "barfoothefoobarman", words = ["foo","bar"]Output: [0,9]Explanation: Substrings starting at index 0 and 9 are "...
Leetcode 0029.Divide Two Integers(python)
29. Divide Two Integers你选用何种方法解题?本题的核心是不使用乘法、除法和取模运算实现整数除法。 方法 时间复杂度 空间复杂度 是否推荐 位运算(位移法) O(log n) O(1) 推荐 负数倍增法 O(log n) O(1) 推荐 方法选择理由: 位运算(位移法):通过左移实现快速乘法,时间效率高 负数倍增法:使用负数处理避免溢出问题,更安全 解题过程问题分析输入:被除数 dividend,除数 divisor输出:两数相除的商(截断小数部分) 关键约束: 不能使用乘法、除法、取模运算 需要处理溢出情况 核心洞察 位运算替代乘法:x << k 等价于 x * 2^k 二分查找思想:找到最大的 k 使得 divisor * 2^k <= dividend 符号处理:先记录符号,将问题转化为正数或负数除法 负数处理:使用负数避免 abs(INT_MIN) 溢出 算法流程(负数倍增法)以 dividend = 15, divisor = 3 为例: 12345678910111213141516符号:正转化为负...
Leetcode 0029.Divide Two Integers(C++)
29. Divide Two Integers题目Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator. The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2. Return the quotient after dividing dividend by divisor. Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−2^31...
Leetcode 0028.Implement strStr()(python)
28. Implement strStr()你选用何种方法解题?本题的核心是实现字符串匹配算法。 方法 时间复杂度 空间复杂度 是否推荐 暴力匹配 O(n×m) O(1) 简单场景 KMP 算法 O(n + m) O(m) 推荐 方法选择理由: 暴力匹配:代码简单,适合短字符串 KMP 算法:时间效率更高,适合长字符串 解题过程问题分析输入:主串 haystack,模式串 needle输出:needle 在 haystack 中第一次出现的索引,不存在返回 -1 核心洞察 暴力匹配:逐个比较,不匹配时回溯 KMP 算法:利用部分匹配信息,避免不必要的回溯 暴力匹配算法流程以 haystack = "hello", needle = "ll" 为例: 1234i=0: h vs l -> 不匹配i=1: e vs l -> 不匹配i=2: l vs l -> 匹配,继续 i+j=3: l vs l -> 匹配,j=1 == lenn-1=1,返回 2 这些方法具体怎么运用?方法一...
Leetcode 0027.Remove Element(python)
27. Remove Element你选用何种方法解题?本题的核心是原地移除数组中等于给定值的元素。 方法 时间复杂度 空间复杂度 是否推荐 快慢指针 O(n) O(1) 推荐 方法选择理由: 快慢指针:只需一次遍历,空间复杂度 O(1) 解题过程问题分析输入:数组 nums,目标值 val输出:移除目标值后的数组长度 关键约束: 原地修改数组,不能使用额外空间 不需要保持元素顺序 核心洞察 双指针技巧:使用两个指针,一个指向待填位置,另一个遍历数组 原地修改:直接在原数组上进行修改 算法流程以 nums = [3,2,2,3], val = 3 为例: 1234567891011121314初始化: num = 0(指向待填位置)遍历过程: i=0: nums[0]=3 == val=3,跳过 i=1: nums[1]=2 != val=3 nums[0] = 2, num = 1 nums = [2,2,2,3] i=2: nums[2]=2 != val=3 nums[1] = 2, num = 2 ...
Leetcode 0026.Remove Duplicates from Sorted Array(python)
26. Remove Duplicates from Sorted Array你选用何种方法解题?本题的核心是原地删除有序数组中的重复元素。 方法 时间复杂度 空间复杂度 是否推荐 快慢指针 O(n) O(1) 推荐 方法选择理由: 快慢指针:只需一次遍历,空间复杂度 O(1) 解题过程问题分析输入:有序数组 nums输出:删除重复元素后的数组长度 关键约束: 原地修改数组,不能使用额外空间 相同元素只保留一个 核心洞察 双指针技巧:使用两个指针,一个指向不重复元素的最后一个位置,另一个遍历数组 原地修改:直接在原数组上进行修改 算法流程以 nums = [1,1,2,2,3,4,4,5] 为例: 12345678910111213141516171819202122初始化: num = 0(指向第一个元素)遍历过程: i=1: nums[0]=1, nums[1]=1, 1 < 1 不成立,跳过 i=2: nums[0]=1, nums[2]=2, 1 < 2 成立 num = 1, nums[1] = 2 n...
Leetcode 0025.Reverse Nodes in k-Group(python)
25. Reverse Nodes in k-Group题目Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. You may not alter the values in the list's nodes, only nodes themselves may be changed. Example 1: 12Input: head = [1,2,3,4,5], k = 2Output: [2,1,4,3,5] Example 2: 12Inp...
Leetcode 3002. Maximum Size of a Set After Removals
3002. Maximum Size of a Set After RemovalsYou are given two 0-indexed integer arrays nums1 and nums2 of even length n. You must remove n / 2 elements from nums1 and n / 2 elements from nums2. After the removals, you insert the remaining elements of nums1 and nums2 into a set s. Return the maximum possible size of the set s. Example 1: 1234Input: nums1 = [1,2,1,2], nums2 = [1,1,1,1]Output: 2Explanation: We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become e...
Leetcode 0024.Swap Nodes in Pairs(python)
24. Swap Nodes in Pairs题目Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.) Example 1: 12Input: head = [1,2,3,4]Output: [2,1,4,3] Example 2: 12Input: head = []Output: [] Example 3: 12Input: head = [1]Output: [1] 题目大意给定一个链表,两两交换其中相邻的节点,并返回交换后的链表头节点。要求不能修改节点的值,只能通过改变节点指针来实现交换。 你选用何种方法解题?本题的核心是两两交换链表节点。 方法 时间复杂度 空间复杂度 是否推荐 递归 O(n) O(n) 推荐 ...
Leetcode 2356. 每位教师所教授的科目种类的数量
2356. 每位教师所教授的科目种类的数量表: Teacher 123456789+-------------+------+| Column Name | Type |+-------------+------+| teacher_id | int || subject_id | int || dept_id | int |+-------------+------+在 SQL 中,(subject_id, dept_id) 是该表的主键。该表中的每一行都表示带有 teacher_id 的教师在系 dept_id 中教授科目 subject_id。 查询每位老师在大学里教授的科目种类的数量。 以 任意顺序 返回结果表。 查询结果格式示例如下。 示例 1: 1234567891011121314151617181920212223242526272829输入: Teacher 表:+------------+------------+---------+| teacher_id | subject_id | dept_id |+------------+---...
Leetcode 2356. 每位教师所教授的科目种类的数量
2356. 每位教师所教授的科目种类的数量表: Teacher 123456789+-------------+------+| Column Name | Type |+-------------+------+| teacher_id | int || subject_id | int || dept_id | int |+-------------+------+在 SQL 中,(subject_id, dept_id) 是该表的主键。该表中的每一行都表示带有 teacher_id 的教师在系 dept_id 中教授科目 subject_id。 查询每位老师在大学里教授的科目种类的数量。 以 任意顺序 返回结果表。 查询结果格式示例如下。 示例 1: 1234567891011121314151617181920212223242526272829输入: Teacher 表:+------------+------------+---------+| teacher_id | subject_id | dept_id |+------------+---...
Leetcode 0023.Merge k Sorted Lists(python)
23. Merge k Sorted Lists题目You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Example 1: 12345678910Input: lists = [[1,4,5],[1,3,4],[2,6]]Output: [1,1,2,3,4,4,5,6]Explanation: The linked-lists are:[ 1->4->5, 1->3->4, 2->6]merging them into one sorted list:1->1->2->3->4->4->5->6 Example 2: 12Input: lists = []Output: [] Example 3: 12Input: lis...
Leetcode 1934. 确认率
1934. 确认率表: Signups 12345678+----------------+----------+| Column Name | Type |+----------------+----------+| user_id | int || time_stamp | datetime |+----------------+----------+User_id是该表的主键。每一行都包含ID为user_id的用户的注册时间信息。 表: Confirmations 1234567891011+----------------+----------+| Column Name | Type |+----------------+----------+| user_id | int || time_stamp | datetime || action | ENUM |+----------------+----------+(user_id, time_sta...
Leetcode 0022.Generate Parentheses(python)
22. Generate Parentheses题目Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example 1: 12Input: n = 3Output: ["((()))","(()())","(())()","()(())","()()()"] Example 2: 12Input: n = 1Output: ["()"] 题目大意数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。 你选用何种方法解题?本题的核心是生成所有有效的括号组合,属于经典的回溯算法(Backtracking)问题。 方法 时间复杂度 空间复杂度 是否推荐 DFS(字符串拼接) $O(\frac{4^n}{\sqrt{n}})$ $O(n)$ 推荐 DF...
Leetcode 1757. 可回收且低脂的产品
1757. 可回收且低脂的产品表:Products 12345678910+-------------+---------+| Column Name | Type |+-------------+---------+| product_id | int || low_fats | enum || recyclable | enum |+-------------+---------+product_id 是该表的主键(具有唯一值的列)。low_fats 是枚举类型,取值为以下两种 ('Y', 'N'),其中 'Y' 表示该产品是低脂产品,'N' 表示不是低脂产品。recyclable 是枚举类型,取值为以下两种 ('Y', 'N'),其中 'Y' 表示该产品可回收,而 'N' 表示不可回收。 编写解决方案找出既是低脂又是可回收的产品编号。 返回结果 无顺序要求 。 返回结果格式如下例...
Leetcode 0021.Merge Two Sorted Lists(python)
21. Merge Two Sorted Lists题目You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. Example 1: 12Input: list1 = [1,2,4], list2 = [1,3,4]Output: [1,1,2,3,4,4] Example 2: 12Input: list1 = [], list2 = []Output: [] Example 3: 12Input: list1 = [], list2 = [0]Output: [0] 题目大意将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 ...
Leetcode 1201. Ugly Number III
1201. Ugly Number III题目Write a program to find the n-th ugly number. Ugly numbers are positive integers which are divisible by a or b or c. Example 1: Input: n = 3, a = 2, b = 3, c = 5 Output: 4 Explanation: The ugly numbers are 2, 3, 4, 5, 6, 8, 9, 10... The 3rd is 4. Example 2: Input: n = 4, a = 2, b = 3, c = 4 Output: 6 Explanation: The ugly numbers are 2, 3, 4, 6, 8, 9, 10, 12... The 4th is 6. Example 3: Input: n = 5, a = 2, b = 11, c = 13 Output: 10 Explanation: The ugly numbers are 2,...
Leetcode 0020.Valid Parentheses(python)
20. Valid Parentheses题目Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type. Example 1: 12Input: s = "()"Output: true Example 2: 12Input: s = "(...
Leetcode 1193. 每月交易 I
1193. 每月交易 I表:Transactions 123456789101112+---------------+---------+| Column Name | Type |+---------------+---------+| id | int || country | varchar || state | enum || amount | int || trans_date | date |+---------------+---------+id 是这个表的主键。该表包含有关传入事务的信息。state 列类型为 ["approved", "declined"] 之一。 编写一个 sql 查询来查找每个月和每个国家/地区的事务数及其总金额、已批准的事务数及其总金额。 以 任意顺序 返回结果表。 查询结果格式如下所示。 示例 1: 123456789101112131415161718输入:Transactio...
Leetcode 0019.Remove Nth Node From End of List(python)
19. Remove Nth Node From End of List题目Given the head of a linked list, remove the nth node from the end of the list and return its head. Example 1: 12Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5] Example 2: 12Input: head = [1], n = 1Output: [] Example 3: 12Input: head = [1,2], n = 1Output: [1] 题目大意给定一个链表的头节点 head,删除链表的倒数第 n 个节点,并返回链表的头节点。 你选用何种方法解题?本题的核心是找到链表的倒数第 n 个节点并删除。 方法 时间复杂度 空间复杂度 是否推荐 双指针(快慢指针,哑节点) O(n) O(1) 推荐 双指针(快慢指针,无哑节点) O(n) O(1) 推荐 两次遍历 O(n) O(1) 可选 栈 O(n) O...

