Leetcode 0008. String to Integer (atoi) (python)
8. String to Integer (atoi)题目Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm for myAtoi(string s) is as follows: Whitespace: Ignore any leading whitespace (" "). Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity if neither present. Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is rea...
Leetcode 0611. Valid Triangle Number
611. Valid Triangle NumberGiven an integer array nums, return the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. Example 1: 123456Input: nums = [2,2,3,4]Output: 3Explanation: Valid combinations are: 2,3,4 (using the first 2)2,3,4 (using the second 2)2,2,3 Example 2: 12Input: nums = [4,2,3,4]Output: 4 题目大意给定一个整数数组 nums,返回数组中可作为三角形三条边长度的三元组数量。三角形三条边需满足:任意两边之和大于第三边。 核心解题思路:排序 + 双指针三角形三边的核心条件是:两边之和大于第三边。对于排序后的数组,可简化为: 设三条边为 a ≤ b...
Leetcode 0007. Reverse Integer (python)
7. Reverse Integer题目Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2³¹, 2³¹ - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Example 1: 12Input: x = 123Output: 321 Example 2: 12Input: x = -123Output: -321 Example 3: 12Input: x = 120Output: 21 Constraints: -2³¹ <= x <= 2³¹ - 1 题目大意将一个 32 位有符号整数的数字部分反转。如果反转后的整数溢出 32 位有符号整数范围...
Leetcode 0572. Subtree of Another Tree
572. Subtree of Another TreeGiven the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise. A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself. Example 1: 12Input: root = [3,4,5,1,2], subRoot = [4,1,2]Output: true Example 2: 12Input: root = [3,4,5,1,2,null,null,nu...
Leetcode 0570. 至少有5名直接下属的经理
570. 至少有5名直接下属的经理表: Employee 123456789101112+-------------+---------+| Column Name | Type |+-------------+---------+| id | int || name | varchar || department | varchar || managerId | int |+-------------+---------+id 是此表的主键(具有唯一值的列)。该表的每一行表示雇员的名字、他们的部门和他们的经理的id。如果managerId为空,则该员工没有经理。没有员工会成为自己的管理者。 编写一个解决方案,找出至少有五个直接下属的经理。 以 任意顺序 返回结果表。 查询结果格式如下所示。 示例 1: 123456789101112131415161718输入: Employee 表:+-----+-------+------------+-----------+| id | name | department ...
Leetcode 0006. Zigzag Conversion (python)
6. Zigzag Conversion题目The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) 123P A H NA P L S I I GY I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: 1string convert(string s, int numRows); Example 1: 12Input: s = "PAYPALISHIRING", numRows = 3Outpu...
Leetcode 0515. Find Largest Value in Each Tree Row
515. Find Largest Value in Each Tree RowGiven the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed). Example 1: 12Input: root = [1,3,2,5,3,null,9]Output: [1,3,9] Example 2: 12Input: root = [1,2,3]Output: [1,3] 题目大意给定一棵二叉树的根节点 root,返回一个数组,其中每个元素是二叉树对应行(从 0 开始索引)中的最大值。 例如: 输入二叉树 [1,3,2,5,3,null,9],第 0 行最大值为 1,第 1 行最大值为 3,第 2 行最大值为 9,因此返回 [1,3,9]。 解题思路要找到每一行的最大值,核心是按层遍历二叉树,并在遍历过程中记录每层的最大值。具体步骤如下: 层序遍历初始化:若根节点为空,直接返回空数组;否则将根节点入队。 逐层处理节点: 记录当前队列...
Leetcode 0005. Longest Palindromic Substring (python)
5. Longest Palindromic Substring题目Given a string s, return the longest palindromic substring in s. Example 1: 123Input: s = "babad"Output: "bab"Explanation: "aba" is also a valid answer. Example 2: 12Input: s = "cbbd"Output: "bb" Constraints: 1 <= s.length <= 1000 s consist of only digits and English letters. 题目大意在给定字符串中找出最长的回文子串(palindromic substring)。回文串是指正读反读都一样的字符串。若存在多个等长的最长回文子串,返回任意一个即可。字符串长度上限为 1000,仅包含数字和英文字母。 你选用何种方法解题?本题的核...
Leetcode 0513. Find Bottom Left Tree Value
513. Find Bottom Left Tree ValueGiven the root of a binary tree, return the leftmost value in the last row of the tree. Example 1: 12Input: root = [2,1,3]Output: 1 Example 2: 12Input: root = [1,2,3,4,null,5,6,null,null,7]Output: 7 题目大意给定一棵二叉树的根节点 root,返回树的最后一行中最左边的节点值。即需要找到二叉树最深一层的最左侧节点的值。 例如: 输入二叉树 [2,1,3],最深层是第 2 层,最左侧节点值为 1,返回 1; 输入二叉树 [1,2,3,4,null,5,6,null,null,7],最深层是第 4 层,最左侧节点值为 7,返回 7。 解题思路找到树左下角的值,关键是确定最深层和该层的最左侧节点。主要有两种实现方法: 1. 层序遍历(BFS) 按层遍历二叉树,记录每一层的第一个节点值; 最后一层的第一个节点值即为结果...
Leetcode 0509. Fibonacci Number
509. Fibonacci Number题目The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N > 1. Given N, calculate F(N). Example 1: Input: 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. Example 2: Input: 3 Output: 2 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. Example 3: Input: 4 Output: 3 Explanation: F(4) ...
Leetcode 0004. Median of Two Sorted Arrays (python)
4. Median of Two Sorted Arrays题目Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: 123Input: nums1 = [1,3], nums2 = [2]Output: 2.00000Explanation: merged array = [1,2,3] and median is 2. Example 2: 123Input: nums1 = [1,2], nums2 = [3,4]Output: 2.50000Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. 题目大意给定两个已排序的数组 nums1 和 nums2,找出这两个有序数组的中位数。...
Leetcode 0503. 下一个更大元素 II
503. 下一个更大元素 II给定一个循环数组 nums ( nums[nums.length - 1] 的下一个元素是 nums[0] ),返回 nums 中每个元素的 下一个更大元素 。 数字 x 的 下一个更大的元素 是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1 。 示例 1: 12345输入: nums = [1,2,1]输出: [2,-1,2]解释: 第一个 1 的下一个更大的数是 2;数字 2 找不到下一个更大的数; 第二个 1 的下一个最大的数需要循环搜索,结果也是 2。 示例 2: 12输入: nums = [1,2,3,4,3]输出: [2,3,4,-1,4] 解题思路核心思路是单调栈 + 数组翻倍模拟循环,通过将数组复制一份拼接在末尾(模拟循环),利用单调栈高效找到每个元素的下一个更大元素: 模拟循环数组: 将原数组 nums 复制一份并拼接在末尾(如 [1,2,1] 变为 [1,2,1,1,2,1]),这样无需额外处理循环边界,可直接线性遍历。 单调栈求解: 使用单调栈存储...
Leetcode 0496. 下一个更大元素 I
496. 下一个更大元素 Inums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。 给你两个 没有重复元素 的数组 nums1 和 nums2 ,下标从 0 开始计数,其中nums1 是 nums2 的子集。 对于每个 0 <= i < nums1.length ,找出满足 nums1[i] == nums2[j] 的下标 j ,并且在 nums2 确定 nums2[j] 的 下一个更大元素 。如果不存在下一个更大元素,那么本次查询的答案是 -1 。 返回一个长度为 nums1.length 的数组 ans 作为答案,满足 ans[i] 是如上所述的 下一个更大元素 。 示例 1: 123456输入:nums1 = [4,1,2], nums2 = [1,3,4,2].输出:[-1,3,-1]解释:nums1 中每个值的下一个更大元素如下所述:- 4 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。- 1 ,用加粗斜体标识,nums2 = [1,3,4,2...
Leetcode 0003. Longest Substring Without Repeating Characters (python)
3. Longest Substring Without Repeating Characters题目Given a string s, find the length of the longest substring without repeating characters. Example 1: 123Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3. Example 2: 123Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1. Example 3: 1234Input: s = "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3.No...
Leetcode 0491. Non-decreasing Subsequences
491. Non-decreasing SubsequencesGiven an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order. Example 1: 12Input: nums = [4,6,7,7]Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]] Example 2: 12Input: nums = [4,4,3,2,1]Output: [[4,4]] 题目大意给定一个整数数组 nums,返回所有不同的、长度至少为 2 的非递减子序列。子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。 例如: 输入 nums = [4,6,7,7],输出包含 [4,6]、[4,6,7...
Leetcode 0461.hamming-distance
461. 汉明距离两个整数之间的 汉明距离 指的是这两个数字对应二进制位不同的位置的数目。 给你两个整数 x 和 y,计算并返回它们之间的汉明距离。 示例 1: 1234567输入:x = 1, y = 4输出:2解释:1 (0 0 0 1)4 (0 1 0 0) ↑ ↑上面的箭头指出了对应二进制位不同的位置。 示例 2: 12输入:x = 3, y = 1输出:1 题目大意 汉明距离是指两个整数的二进制表示中,对应位置二进制位不同的数目。给定两个整数 x 和 y,计算并返回它们之间的汉明距离。 例如: 输入 x=1, y=4,二进制分别为 0001 和 0100,对应位不同的位置有 2 处,输出 2; 输入 x=3, y=1,二进制分别为 0011 和 0001,对应位不同的位置有 1 处,输出 1。 解题思路核心思路是利用异或运算 + 统计 1 的个数,步骤如下: 异或运算(XOR):异或的特性是 “相同为 0,不同为 1”。对 x 和 y 做异或操作,得到的结果 xor_result 中,每一位的 1 都对应 x 和 y 二进制位不同的位...
Leetcode 0002. Add Two Numbers
2. Add Two Numbers题目You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: 123Input: l1 = [2,4,3], l2 = [5,6,4]Output: [7,0,8]Explanation: 342 + 465 = 807. Example 2: 12Input: l1 = [0], l2 = [0]Output: [0] Example 3: 12I...
Leetcode 0455. Assign Cookies
455. Assign CookiesAssume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number. Example 1: 1...
Leetcode 0001.two sum(python)
1. Two Sum题目Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example 1: 123Input: nums = [2,7,11,15], target = 9Output: [0,1]Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2: 12Input: nums = [3,2,4], target = 6Output: [1,2] Example 3: 12Input: nums =...
Leetcode 0454. 4Sum II
454. 4Sum IIGiven four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that: 0 <= i, j, k, l < n nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 Example 1: 123456Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]Output: 2Explanation:The two tuples are:1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 02. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 +...
Leetcode 0445. Add Two Numbers II
445. Add Two Numbers IIYou are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. 1234567891011121314151617181920212223242526272829303132333435363738394041424344/** * Definition for singly-linked list. * struct ListNode { * int v...
Leetcode 0429. N-ary Tree Level Order Traversal
429. N-ary Tree Level Order TraversalGiven an n-ary tree, return the level order traversal of its nodes' values. Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples). Example 1: 12Input: root = [1,null,3,2,4,null,5,6]Output: [[1],[3,2,4],[5,6]] Example 2: 12Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]Output: [[1],[2,3,4,5],[6,7,8,9,10]...
Leetcode 0409. 最长回文串
409. 最长回文串给定一个包含大写字母和小写字母的字符串 s ,返回 通过这些字母构造成的 最长的 回文串 的长度。 在构造过程中,请注意 区分大小写 。比如 "Aa" 不能当做一个回文字符串。 示例 1: 1234输入:s = "abccccdd"输出:7解释:我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。 示例 2: 123输入:s = "a"输出:1解释:可以构造的最长回文串是"a",它的长度是 1。 解题思路核心思路是统计字符频率,利用回文串的特性计算最大长度: 回文串特性: 回文串对称位置的字符相同; 偶数长度的回文串:所有字符出现次数均为偶数; 奇数长度的回文串:仅有一个字符出现奇数次(位于中心),其余均为偶数次。 频率统计: 统计字符串中每个字符的出现次数; 累计所有字符的最大偶数次(例如,出现 5 次的字符可贡献 4 次); 若存在出现奇数次的字符,可在结果中加 1(作为中心字符)。 计算逻辑: 初始化结果 max_len...
Leetcode 0404. Sum of Left Leaves
404. Sum of Left LeavesGiven the root of a binary tree, return the sum of all left leaves. A leaf is a node with no children. A left leaf is a leaf that is the left child of another node. Example 1: 123Input: root = [3,9,20,null,null,15,7]Output: 24Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively. Example 2: 12Input: root = [1]Output: 0 题目大意给定一棵二叉树的根节点 root,返回所有左叶子的节点值之和。左叶子的定义是:既是叶子节点(没有子节点),又是其父节点的左子节点。 例如: 输入二叉树 [3,9,20,null,null,15,7],左叶子为 ...
Leetcode 0402. 移掉 K 位数字
402. 移掉 K 位数字给你一个以字符串表示的非负整数 num 和一个整数 k ,移除这个数中的 k 位数字,使得剩下的数字最小。请你以字符串形式返回这个最小的数字。 示例 1 : 123输入:num = "1432219", k = 3输出:"1219"解释:移除掉三个数字 4, 3, 和 2 形成一个新的最小的数字 1219 。 示例 2 : 123输入:num = "10200", k = 1输出:"200"解释:移掉首位的 1 剩下的数字为 200. 注意输出不能有任何前导零。 示例 3 : 123输入:num = "10", k = 2输出:"0"解释:从原数字移除所有的数字,剩余为空就是 0 。 解题思路核心思路是单调栈 + 贪心算法,通过维护一个递增的栈来确保剩余数字最小,同时控制移除的数字数量不超过 k: 单调栈逻辑: 遍历字符串中的每个数字,对于当前数字c: 若栈不为空,且栈顶数字大于 c,且仍有可移除的次数(k > 0),则弹...

