![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode
莱安纳德
这个作者很懒,什么都没留下…
展开
-
Leetcode 1262. Greatest Sum Divisible by Three(一题容易错的DP题)
Leetcode 1262. Greatest Sum Divisible by Three题目链接: Greatest Sum Divisible by Three难度:Medium题目大意:选取数组中的若干个元素,使得这些元素的和能被3整除且尽可能得大。思路:01背包的变形,注意细节。代码class Solution { public int maxSumDivThree(int[] nums) { int n=nums.length; int原创 2021-11-29 16:58:28 · 109 阅读 · 0 评论 -
Leetcode 784. Letter Case Permutation(DFS)
Leetcode 784. Letter Case Permutation题目链接: Letter Case Permutation难度:Medium题目大意:给出一个只包含数字和字母的字符串,可以将字母的大小写改变,也可以不变,求所有可能生成的字符串。思路:用DFS进行求解,遍历字符串,如果碰到字母,该字母可以变成大写字母或者小写字母,然后进行DFS遍历。代码class Solution { public List<String> letterCasePermuta原创 2021-08-10 11:02:16 · 76 阅读 · 0 评论 -
回溯法求解Leetcode经典排列问题(Leetcode 46、47、996)
写在前面:求解排列问题常用的方法是回溯,回溯类似于DFS(深度优先遍历),实际是一个暴力搜索的过程,把所有的排列都列举出来,找出符合题意的排列。组合问题与元素顺序无关,而排列问题与元素的顺序有关。1、46. Permutations题意:给出一个不含重复元素的数组,求所有可能的排列。代码:class Solution { public List<List<Integer>> permute(int[] nums) { List<List<原创 2021-08-10 10:54:38 · 106 阅读 · 0 评论 -
Leetcode 300. Longest Increasing Subsequence(动态规划、贪心+二分)
Leetcode 300. Longest Increasing Subsequence题目链接: Count of Smaller Numbers After Self难度:Medium题目大意:给出一个数组,求最长递增子序列的长度思路:参考leetcode官方题解,有动态规划和贪心+二分两种思路。代码动态规划class Solution { public int lengthOfLIS(int[] nums) { int n=nums.length;原创 2021-08-10 09:11:50 · 79 阅读 · 0 评论 -
回溯法求解Leetcode经典组合问题(Leetcode17、39、40、216、77、78、90、131)
写在前面:求解组合问题常用的方法是回溯,回溯类似于DFS(深度优先遍历),实际是一个暴力搜索的过程,把所有的组合都列举出来,找出符合题意的组合。1、Leetcode 17. Letter Combinations of a Phone Number思路:参考高赞回答。class Solution { public List<String> letterCombinations(String digits) { List<String> res=new原创 2021-08-06 17:11:20 · 252 阅读 · 0 评论 -
Leetcode 315. Count of Smaller Numbers After Self(分治法)
Leetcode 315. Count of Smaller Numbers After Self题目链接: Count of Smaller Numbers After Self难度:Hard题目大意:给出一个数组,求各个元素的右边有多少个比自己小的元素。思路:参考高赞回答1和高赞回答2,求解过程实际上是模拟归并排序的过程。代码class Solution { int[] count; public List<Integer> countSmaller(int原创 2021-07-26 11:24:40 · 130 阅读 · 0 评论 -
Leetcode 912. Sort an Array(归并排序)
Leetcode 912. Sort an Array题目链接: Sort an Array难度:Medium题目大意:对数组进行排序。思路:用分治法对数组进行归并排序,将数组分成两部分,分别排好序后再进行合并。代码class Solution { public int[] sortArray(int[] nums) { mergeSort(nums); return nums; } public void mergeSort(int原创 2021-07-26 11:13:38 · 81 阅读 · 0 评论 -
Leetcode 154. Find Minimum in Rotated Sorted Array II(分治)
Leetcode 154. Find Minimum in Rotated Sorted Array II题目链接: Find Minimum in Rotated Sorted Array II难度:Hard题目大意:将一个不递减的数组循环移位若干次,求这个数组的最小元素。时间复杂度要求O(logn)。思路:与Leetcode 153. Find Minimum in Rotated Sorted Array类似,分治,找出乱序出现的位置即可找到最小值。代码class Solution原创 2021-07-26 11:07:07 · 68 阅读 · 0 评论 -
Leetcode 153. Find Minimum in Rotated Sorted Array(分治和二分)
Leetcode 153. Find Minimum in Rotated Sorted Array题目链接: Find Minimum in Rotated Sorted Array难度:Medium题目大意:将一个严格升序的数组循环移位若干次,求这个数组的最小元素。时间复杂度要求O(logn)。思路1:分治,找出乱序出现的位置即可找到最小值。代码class Solution { public int findMin(int[] nums) { int n=num原创 2021-07-26 11:01:38 · 70 阅读 · 0 评论 -
Leetcode 169. Majority Element(找数组的主元素)
Leetcode 169. Majority Element题目链接: Majority Element难度:Easy题目大意:找出整形数组中的主元素,即出现次数超过半数的元素。思路1:统计数组中各个元素的个数,出现次数最多的就是主元素。代码class Solution { public int majorityElement(int[] nums) { Map<Integer,Integer> map=new HashMap<>();原创 2021-07-12 16:39:27 · 179 阅读 · 0 评论 -
Leetcode 392. Is Subsequence(用双指针判断子序列)
Leetcode 392. Is Subsequence题目链接: Is Subsequence难度:Easy题目大意:判断一个字符串是不是另一个字符串的子序列。思路:参考高赞回答,使用双指针进行求解。代码class Solution { public boolean isSubsequence(String s, String t) { int i=0,j=0; if(s.length()==0){ return true;原创 2021-06-13 19:57:54 · 58 阅读 · 0 评论 -
Leetcode 31. Next Permutation
Leetcode 31. Next Permutation题目链接: Next Permutation难度:Medium题目大意:详见题目。思路:参考官方题解。代码class Solution { //参考官方题解 public void nextPermutation(int[] nums) { int i=nums.length-2; while(i>=0&&nums[i]>=nums[i+1]){原创 2021-05-21 19:06:52 · 95 阅读 · 0 评论 -
Leetcode 979. Distribute Coins in Binary Tree(DFS)
Leetcode 979. Distribute Coins in Binary Tree题目链接: Distribute Coins in Binary Tree难度:Medium题目大意:二叉树有n个节点,每个节点上的值表示该节点上有几个硬币,整棵二叉树共有n个硬币,求需要经过多少次移动使得每个节点上都是一个硬币。思路:参考官方题解与高赞回答。代码/** * Definition for a binary tree node. * public class TreeNode {原创 2021-05-02 09:42:35 · 77 阅读 · 0 评论 -
Leetcode 337. House Robber III(递归)
Leetcode 337. House Robber III题目链接: House Robber III难度:Medium题目大意:详见题目。思路:参考官方题解,对是否偷窃当前节点分情况讨论。代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {原创 2021-05-02 09:34:34 · 91 阅读 · 0 评论 -
Leetcode 968. Binary Tree Cameras(动态规划与递归)
Leetcode 968. Binary Tree Cameras题目链接: Binary Tree Cameras难度:Medium题目大意:详见题目。思路:参考官方题解。代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} *原创 2021-05-02 09:27:20 · 87 阅读 · 0 评论 -
Leetcode 687. Longest Univalue Path(递归)
Leetcode 687. Longest Univalue Path题目链接: Longest Univalue Path难度:Medium题目大意:详见题目。思路:本题思路与Leetcode543类似,只是增加了路径上所有节点的值要相同这一条件。代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNo原创 2021-04-30 19:59:46 · 72 阅读 · 1 评论 -
Leetcode 124. Binary Tree Maximum Path Sum(递归)
Leetcode 124. Binary Tree Maximum Path Sum题目链接: Most Frequent Subtree Sum难度:Medium题目大意:详见题目。思路:本题思路与Leetcode543类似。参考高赞回答。代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode r原创 2021-04-30 19:52:08 · 137 阅读 · 1 评论 -
Leetcode 543. Diameter of Binary Tree(递归)
Leetcode 543. Diameter of Binary Tree题目链接: [Diameter of Binary Tree](https://leetcode.com/problems/Diameter of Binary Tree/)难度:Medium题目大意:详见题意。思路:利用递归求二叉树的最深深度,在这个过程中再求二叉树的直径。代码/** * Definition for a binary tree node. * public class TreeNode {原创 2021-04-30 19:18:22 · 83 阅读 · 1 评论 -
Leetcode 508. Most Frequent Subtree Sum(递归)
Leetcode 508. Most Frequent Subtree Sum题目链接: Most Frequent Subtree Sum难度:Medium题目大意:给出二叉树,统计二叉树中所有子树的和的数量,返回数量最多的和。思路:参考高赞回答,利用递归来求二叉树中所有子树的和,用HashMap来统计次数。最后Integer的List通过for循环来转化成int类型的数组。代码/** * Definition for a binary tree node. * public cla原创 2021-04-30 11:25:29 · 88 阅读 · 0 评论 -
Leetcode 449. Serialize and Deserialize BST(递归)
Leetcode 449. Serialize and Deserialize BST题目链接: 449. Serialize and Deserialize BST难度:Hard题目大意:给出二叉搜索树,将其转成String,再从String中构建出二叉搜索树。本题与 Leetcode 297. Serialize and Deserialize Binary Tree类似。思路:参考高赞回答,利用递归思想,对二叉搜索树采用前序遍历,各个节点之间用“,”分隔存储到StringBuilder原创 2021-04-30 11:10:14 · 101 阅读 · 0 评论 -
Leetcode 257. Binary Tree Paths(迭代与递归)
Leetcode 257. Binary Tree Paths题目链接: Binary Tree Paths难度:Easy题目大意:输出二叉树中所有根节点到叶子节点的路径。思路:思路1:BFS,层级遍历。思路2:参考高赞回答下的评论,递归,并使用了回溯来降低时间复杂度。这种方法比思路快不少。代码思路1代码:/** * Definition for a binary tree node. * public class TreeNode { * int val; *原创 2021-04-28 16:06:27 · 81 阅读 · 0 评论 -
Leetcode 129. Sum Root to Leaf Numbers(迭代)
Leetcode 129. Sum Root to Leaf Numbers题目链接: Sum Root to Leaf Numbers难度:Medium题目大意:详见题目。思路:递归。可以根据二叉搜索树的特点来简化查找过程。代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; *原创 2021-04-28 15:57:21 · 71 阅读 · 0 评论 -
Leetcode 235. Lowest Common Ancestor of a Binary Search Tree(递归)
Leetcode 235. Lowest Common Ancestor of a Binary Search Tree题目链接: Lowest Common Ancestor of a Binary Search Tree难度:Easy题目大意:求二叉搜索树中两个节点的最近公共父亲节点。这题是Leetcode 236. Lowest Common Ancestor of a Binary Tree的简化版。思路:递归。可以根据二叉搜索树的特点来简化查找过程。代码/** * Definit原创 2021-04-28 15:36:19 · 51 阅读 · 0 评论 -
Leetcode Weekly Contest 238(详细解析)
题目链接: Leetcode Weekly Contest 238写在前面:本次只做出了第一道,第三道一直到比赛结束也没有做对。1、1837. Sum of Digits in Base K难度:Easy题目大意:将十进制数n转成k进制,然后返回k进制数每一位数字相加之和。思路:按题意模拟即可。代码class Solution { public int sumBase(int n, int k) { int res=0; while(n>0原创 2021-04-27 12:05:22 · 77 阅读 · 0 评论 -
Leetcode 437. Path Sum III(递归与回溯)
Leetcode 437. Path Sum III题目链接: Path Sum III难度:Medium题目大意:找出二叉树中所有从上往下路径和为target的路径,路径中可以只含有一个节点。思路:参考高赞回答,用到了递归与回溯。回溯比较难理解,看了好久才弄懂代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * T原创 2021-04-21 10:24:46 · 94 阅读 · 0 评论 -
Leetcode 113. Path Sum II(递归与回溯)
Leetcode 113. Path Sum II题目链接: Path Sum II难度:Medium题目大意:找出二叉树中所有从根节点到叶子节点路径和为target的路径。思路:参考高赞回答,用到了递归与回溯。代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * Tre原创 2021-04-21 10:20:12 · 71 阅读 · 0 评论 -
Leetcode 1325. Delete Leaves With a Given Value(递归)
Leetcode 1325. Delete Leaves With a Given Value题目链接: Delete Leaves With a Given Value难度:Medium题目大意:将二叉树中值为target的叶子节点去掉,注意去掉叶子节点后父节点可能也会变成叶子节点。思路:递归。本题思路与leetcode814基本一样。代码/** * Definition for a binary tree node. * public class TreeNode { *原创 2021-04-20 10:16:12 · 150 阅读 · 0 评论 -
Leetcode 669. Trim a Binary Search Tree(递归)
Leetcode 669. Trim a Binary Search Tree题目链接: Trim a Binary Search Tree难度:Medium题目大意:将二叉树中节点值不在范围内的节点去除掉,保留二叉树的相对结构不变。思路:递归。代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode原创 2021-04-20 10:09:57 · 79 阅读 · 0 评论 -
Leetcode 3. Longest Substring Without Repeating Characters(暴力法与滑动窗口法)
Leetcode 3. Longest Substring Without Repeating Characters题目链接: Longest Substring Without Repeating Characters难度:Medium题目大意:求字符串中没有重复字符字串的最长长度。思路:思路1:暴力,两层for循环来遍历所有可能的字串,碰到有重复字符的子串就提前结束。思路2:滑动窗口,如果字串中不包含这个字符,则将字串的结束位置向后移一位继续遍历,否则将字串的开始位置向后移一位。代码原创 2021-04-19 15:39:28 · 63 阅读 · 0 评论 -
Leetcode 814. Binary Tree Pruning(详细解析)
Leetcode 814. Binary Tree Pruning题目链接: Binary Tree Pruning难度:Medium题目大意:二叉树的节点要么是0要么是1,将二叉树中只含有0的子树去除掉。思路:递归,判断子树是否只含有0,如果是则去除掉。然后对左子树和右子树进行相同操作。代码自己写的/** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree原创 2021-04-12 11:45:24 · 81 阅读 · 0 评论 -
Leetcode 50. Pow(x, n)(x的n次幂)
Leetcode 50. Pow(x, n)题目链接: Pow(x, n)难度:Medium题目大意:求pow(x,n),x是double类型,n可正可负。思路:参考高赞回答,数学题。注意Integer.MIN_VALUE这个特例。代码class Solution { public double myPow(double x, int n) { long N=n; if(N>=0){ return quickPow(x,N原创 2021-04-03 16:41:27 · 59 阅读 · 0 评论 -
Leetcode 343. Integer Break(整数拆分)
Leetcode 343. Integer Break题目链接: Integer Break难度:Medium题目大意:将一个整数拆分成若干个数的和,至少拆分成两个数,求拆分后这些数乘积的最大值。思路:参考高赞回答,数学题。代码class Solution { public int integerBreak(int n) { if(n<=3){ return n-1;//n=1+(n-1) } int r原创 2021-04-03 16:10:35 · 140 阅读 · 0 评论 -
Leetcode 1563. Stone Game V(动态规划+memo)
Leetcode 1563. Stone Game V题目链接: Stone Game V难度:Hard题目大意:详见题目。思路:参考高赞回答,动态规划,用memo来记录以前的结果,避免重复的计算。代码class Solution { public int stoneGameV(int[] stoneValue) { //思路:遍历所有结果 int n=stoneValue.length; int[] prefixSum=new in原创 2021-03-28 10:05:19 · 167 阅读 · 0 评论 -
Leetcode 214. Shortest Palindrome(暴力求解)
Leetcode 214. Shortest Palindrome题目链接: Shortest Palindrome难度:Hard题目大意:在字符串的开头增添一些字符,使得字符串为回文串。思路:参考高赞回答,寻找原始字符串中从第一个字符开始的最长回文子串,然后将不是回文串的部分翻转添加到字符串头部。代码class Solution { public String shortestPalindrome(String s) { int n=s.length();原创 2021-03-23 11:42:36 · 86 阅读 · 0 评论 -
Leetcode 516. Longest Palindromic Subsequence(动态规划)
Leetcode 516. Longest Palindromic Subsequence题目链接: Longest Palindromic Subsequence难度:Medium题目大意:求字符串的最长回文子序列的长度,子序列是原字符串删除一些字符得到的。思路:动态规划。代码class Solution { public int longestPalindromeSubseq(String s) { int n=s.length(); int[]原创 2021-03-23 11:25:26 · 112 阅读 · 0 评论 -
Leetcode 647. Palindromic Substrings(字符串中回文子串的个数)
Leetcode 647. Palindromic Substrings题目链接: 647. Palindromic Substrings难度:Medium题目大意:求字符串的回文子串的个数。思路:动态规划。如果一个字符串左右两端的字符相等,并且除去这两个字符后的字符串是回文串,则这个字符串是回文串。代码class Solution { public int countSubstrings(String s) { int n=s.length(); i原创 2021-03-23 11:19:59 · 99 阅读 · 0 评论 -
Leetcode 5. Longest Palindromic Substring(动态规划求最长回文串)
Leetcode 5. Longest Palindromic Substring题目链接: Longest Palindromic Substring难度:Medium题目大意:求字符串的最长回文字串的长度。思路:动态规划。如果一个字符串左右两端的字符相等,并且除去这两个字符后的字符串是回文串,则这个字符串是回文串。代码class Solution { public String longestPalindrome(String s) { int n=s.leng原创 2021-03-23 10:45:59 · 101 阅读 · 0 评论 -
Leetcode 72. Edit Distance(动态规划)
Leetcode 72. Edit Distance题目链接: Edit Distance难度:Hard题目大意:给出两个字符串s1和s2,每次操作可以对s1删除、替代或增加一个字符,求最少经过多少次操作,可以使得s1与s2相等。思路:动态规划。代码class Solution { public int minDistance(String s1, String s2) { int m=s1.length(),n=s2.length(); int[][原创 2021-03-20 15:18:30 · 100 阅读 · 0 评论 -
Leetcode 712. Minimum ASCII Delete Sum for Two Strings(动态规划)
Leetcode 712. Minimum ASCII Delete Sum for Two Strings题目链接: Minimum ASCII Delete Sum for Two Strings难度:Mdium题目大意:给出两个字符串,每次操作可以删除一个字符串中的一个字符,使得两个字符串相等,计算删去字符的ASCII值之和的最小值。思路:动态规划。代码class Solution { public int minimumDeleteSum(String s1, String原创 2021-03-20 15:00:46 · 76 阅读 · 0 评论 -
Leetcode 583. Delete Operation for Two Strings(动态规划)
583. Delete Operation for Two Strings题目链接: Delete Operation for Two Strings难度:Mdium题目大意:给出两个字符串,每次操作可以删除一个字符串中的一个字符,求至少经过多少次操作可以使得两个字符串相等。思路:动态规划。代码class Solution { public int minDistance(String word1, String word2) { int m=word1.length原创 2021-03-20 14:54:33 · 107 阅读 · 0 评论