
LeetCode
LeetCode 刷题记录
青帆1998
不系明珠系宝刀
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode 503. Next Greater Element II
题目描述Given a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums.The next greater number of a number x is the first greater number to its traversing-order next原创 2022-04-17 15:07:20 · 191 阅读 · 0 评论 -
LeetCode 209. Minimum Size Subarray Sum
题目描述Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray [numsl, numsl+1, …, numsr-1, numsr] of which the sum is greater than or equal to target. If there is no such subarray, return 0原创 2022-04-16 19:12:00 · 292 阅读 · 0 评论 -
LeetCode 496. Next Greater Element I
题目描述The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.For each 0 <= i原创 2022-04-15 18:49:36 · 271 阅读 · 0 评论 -
LeetCode 508. Most Frequent Subtree Sum
题目描述Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order.The subtree sum of a node is defined as the sum of all the node values formed by the subtree roote原创 2022-04-15 09:06:04 · 380 阅读 · 0 评论 -
LeetCode 279. Perfect Squares
题目描述Given an integer n, return the least number of perfect square numbers that sum to n.A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfe原创 2022-04-09 08:57:14 · 205 阅读 · 0 评论 -
LeetCode 338. Counting Bits
题目描述Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1’s in the binary representation of i.解题思路看到时间复杂度限制,首先想到DP。难点在于如何找到递推式子。对于一个二进制的数字比如,1001,其实我们可以看出,如果把最高位的 1 去掉,那么其就变成 001 (也就是原创 2022-04-07 16:12:14 · 205 阅读 · 0 评论 -
LeetCode 198. House Robber
题目描述You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will autom原创 2022-04-06 17:01:14 · 205 阅读 · 0 评论 -
LeetCode 153. Find Minimum in Rotated Sorted Array
题目描述Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:[4,5,6,7,0,1,2] if it was rotated 4 times.[0,1,2,4,5,6,7] if it was rotated 7 times.Notice that rota原创 2022-04-04 09:24:46 · 284 阅读 · 0 评论 -
LeetCode 763. Partition Labels
题目描述You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.Note that the partition is done so that after concatenating all the parts in order, the resultant string should b原创 2022-04-02 15:09:19 · 150 阅读 · 0 评论 -
LeetCode 409. Longest Palindrome
题目描述Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.Letters are case sensitive, for example, “Aa” is not considered a palindrome here.解题思路这道题的解题关键是不要把它想原创 2022-03-30 08:44:44 · 4874 阅读 · 0 评论 -
LeetCode 179. Largest Number
题目描述Given a list of non-negative integers nums, arrange them such that they form the largest number and return it.Since the result may be very large, so you need to return a string instead of an integer.解题思路贪心思想。两两比较选取最大的一个,我是采取的冒泡排序来解这一道题,抛砖引玉吧算是。代码原创 2022-03-29 09:06:00 · 284 阅读 · 0 评论 -
LeetCode 120. Triangle
题目描述Given a triangle array, return the minimum path sum from top to bottom.For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the n原创 2022-03-28 10:06:43 · 148 阅读 · 0 评论 -
LeetCode 236. Lowest Common Ancestor of a Binary Tree
题目描述Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q原创 2022-03-12 17:26:01 · 466 阅读 · 0 评论 -
76. Minimum Window Substring
题目描述Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string “”.The testcas原创 2022-03-06 09:36:07 · 257 阅读 · 0 评论 -
LeetCode 129. Sum Root to Leaf Numbers
题目描述You are given the root of a binary tree containing digits from 0 to 9 only.Each root-to-leaf path in the tree represents a number.For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123.Return the total sum of all root-to-lea原创 2022-02-24 09:10:40 · 8328 阅读 · 0 评论 -
LeetCode 404. Sum of Left Leaves
题目描述将所有的左叶子节点相加,返回其加和。代码# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def sumOfLeftLeav原创 2022-02-23 21:30:58 · 137 阅读 · 0 评论 -
LeetCode 114. Flatten Binary Tree to Linked List
题目描述将二叉树转换成列表解题思路递归拆分。对于一个根节点 root,其左子树的列表一定在右子树列表上方,故若知道 root 的左右子成分,那么最后的结果也就出来了。代码# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left#原创 2022-02-22 10:11:49 · 287 阅读 · 0 评论 -
LeetCode 108. Convert Sorted Array to Binary Search Tree
题目描述将有序列表转化为平衡二叉搜索树思路任务是构建树,第一想到就是递归创建。然后,平衡二叉搜索树,因为拿到的列表是有序的 (无序排一下序即可),列表的中间位置就是 root,左子树与右子树节点数近乎相同,故满足平衡性质。代码# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val#原创 2022-02-19 22:56:38 · 8284 阅读 · 0 评论 -
LeetCode 98. Validate Binary Search Tree
题目描述判断二叉搜索树是否有效。解题思路题目分析应该使用递归解法。然需要考虑一个问题,在判断子树的有效性时,不要忘记其父亲节点的性质,即应该给出一个区间约束。代码# Definition for a binary tree node.import sysclass TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left原创 2022-02-10 10:39:11 · 218 阅读 · 0 评论 -
96. Unique Binary Search Trees
题目描述Given an integer n, return the number of structurally unique BST’s (binary search trees) which has exactly n nodes of unique values from 1 to n.解题思路动态规划。找动态规划方程,我们知道当加入第 i 个节点时,他的位置只能为:(左上) -> i -> (左下)。那么这道题目就很简单了,左上和右下的数目都是已知的。代码class Solu原创 2022-01-31 16:54:47 · 868 阅读 · 0 评论 -
LeetCode 64. Minimum Path Sum
题目描述Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any point in time.结题思路动态规划。找到最优子结构,即因为当前的最优值取决于上一步原创 2022-01-23 14:17:50 · 125 阅读 · 0 评论 -
LeetCode 61. Rotate List
题目描述Given the head of a linked list, rotate the list to the right by k places.解题思路题目思路很简单。但是注意输入规模,输入规模将其变成了一道找规律的题目。规模为 n 的链表,其反转周期也为n。代码# Definition for singly-linked list.class ListNode: def __init__(self, val=0, next=None): self.val =原创 2022-01-11 10:49:34 · 95 阅读 · 0 评论 -
LeetCode 62. Unique Paths
题目描述There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in ti原创 2022-01-10 09:26:56 · 345 阅读 · 0 评论 -
LeetCode 56. Merge Intervals
题目描述Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.解题思路按照 start 将intervals 排序,从头开始,两两合并,遇到不可并,保存当前结果。代码cl原创 2022-01-09 10:16:12 · 3292 阅读 · 0 评论 -
LeetCode 55. Jump Game
题目描述You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.Return true if you can reach the last index, or false otherwise.解题思路请移步原创 2022-01-08 15:01:24 · 2567 阅读 · 0 评论 -
LeetCode 54. Spiral Matrix
题目描述Given an m x n matrix, return all elements of the matrix in spiral order.结题思路解题思路如图,此题本质上是一个循环问题,找出循环关系即可解出。每次循环圈的维度减少 2,看代码。代码class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: ans = [] cnt = 0原创 2022-01-07 11:01:00 · 206 阅读 · 0 评论 -
LeetCode 51. N-Queens
题目描述The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.Each solution co原创 2022-01-06 10:11:48 · 2781 阅读 · 0 评论 -
LeetCode 46. Permutations
题目描述Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.1 <= nums.length <= 6-10 <= nums[i] <= 10All the integers of nums are unique.代码class Solution: def permute(self原创 2022-01-02 09:31:48 · 685 阅读 · 0 评论 -
LeetCode 45. Jump Game II
题目描述Given an array of non-negative integers nums, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of j原创 2021-12-31 13:02:54 · 520 阅读 · 0 评论 -
LeetCode 36. Valid Sudoku
题目描述Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits 1-9 without repetition.Each column must contain the digits 1-9 without repetition.Each of the n原创 2021-12-22 10:54:35 · 139 阅读 · 0 评论 -
LeetCode 39. Combination Sum
题目描述Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.The same number may be chosen from原创 2021-12-24 11:05:44 · 1412 阅读 · 0 评论 -
LeetCode 40. Combination Sum II
题目描述Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.Each number in candidates may only be used once in the combination.Note: The solu原创 2021-12-25 22:24:44 · 1204 阅读 · 0 评论 -
LeetCode 41. First Missing Positive
题目描述Given an unsorted integer array nums, return the smallest missing positive integer.You must implement an algorithm that runs in O(n) time and uses constant extra space.解题思路首先我觉得这道题目非常值得一做且值得思考。O(n) 的时间复杂度我首先想到的是用字典,存储所有的正数然后利用hash的常数查找时间的优势控制时间复杂度。原创 2021-12-27 13:05:45 · 179 阅读 · 0 评论 -
LeetCode 43. Multiply Strings
题目描述Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.代码 (Python3)cla原创 2021-12-28 16:53:04 · 202 阅读 · 0 评论