leetcode
爱打篮球的憨憨
这个作者很懒,什么都没留下…
展开
-
leetcode84柱状图中最大的矩形
单调栈的应用:用一个栈维护顺序,按照单调增或者减的顺序,一旦遇到不按照单调规律的值,就不断的弹出,直至栈顶元素和要入栈的元素满足单调关系。在这个过程中进行某种计算,最终得到想要的值。public int largestRectangleArea(int[] heights){ int len = heights.length; if (len == 0) { return 0; } if(len == 1){原创 2022-04-05 19:57:14 · 335 阅读 · 0 评论 -
leetcode247
找规律,递归操作public class test { public List<String> number(int n){ List<String> tmp = dfs(n); // 对0的除去操作 } public List<String> dfs(int n){ if(n == 0){ List<String> list = new ArrayList&l原创 2021-09-01 21:58:06 · 100 阅读 · 0 评论 -
leetcode1059
主要能保证所有路的终点都是同一个,并且无环(visited)尝试所有情况,并回溯public class test { public boolean leadsToDestination(int n, List<List<Integer>> list, int source, int target){ boolean[] visit = new boolean[n]; List<List<Integer>> m...原创 2021-09-01 15:33:10 · 437 阅读 · 0 评论 -
2021-09-01
最小生成树,按照权重把边排好序,然后从小到大的构建。实时的判断是否出现环的情况,如果不存在,就把这个边加进去(用到了并查集里面的find函数)。public class test { public int[] parent; public int minimunCost(int N, int[][] num){ if(num.length < N-1){ return -1; } Arrays.sort(num原创 2021-09-01 11:43:48 · 57 阅读 · 0 评论 -
leetcode694
用BFS和DFS进行搜索关键点在于如何记录岛屿的图形信息?用数组存储相对位置信息。参考:https://michael.blog.csdn.net/article/details/107419494原创 2021-08-31 21:28:37 · 114 阅读 · 0 评论 -
leetcode294
递归解决。通过map进行优化。public class test { private Map<String, Boolean> map = new HashMap<>(); public boolean canWin(String s){ if(map.containsKey(s)){ return map.get(s); } for(int i=1; i<s.length(); i.原创 2021-08-31 20:11:41 · 95 阅读 · 0 评论 -
leetcode250
public class test { public int res = 0; public int checkTree(TreeNode root){ dfs(root); return res; } public boolean dfs(TreeNode root){ if(root == null){ return false; } if(root.left =.原创 2021-08-31 17:40:48 · 115 阅读 · 0 评论 -
leetcode681
暴力解法public class test { public String nextClosedTime(String s){ int[] a = new int[4]; int[][] result = new int[256][4]; int[] time1 = new int[256]; int count = 0; int index = Integer.MAX_VALUE; int resu.原创 2021-08-31 16:47:12 · 254 阅读 · 0 评论 -
leetcode1167
需要实时的排序,然后取出长度最短的两个进行拼接。因此需要用到优先队列。这里说一下优先队列的使用public class test { static Comparator<Integer> cmp = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; }.原创 2021-08-31 14:54:30 · 126 阅读 · 0 评论 -
leetcode750
遍历即可,找出对应的两行之间的都为1的个数,然后看能组成多少个矩形public class test { public int count(int[][] num){ int row = num.length; int col = num[0].length; int res = 0; for(int i=0; i<row-1; i++){ List<Integer> tmp = new A.原创 2021-08-31 09:49:17 · 177 阅读 · 0 评论 -
leetcode666
计算和的过程就是dfs。dfs的过程中传输到当前节点的和,判断出传递到叶节点的时候,将当前和加入到sum中。需要用map存储每个位置对应的值。此处就涉及到堆中的一个概念:当前节点与它的左右子节点之间的索引的计算关系。以及当前节点的索引与它的深度和所在深度的位置关系。id = 2^(depth-1) + p -1public class test { Map<Integer, Integer> map = new HashMap<>(); int sum = .原创 2021-08-30 21:54:04 · 116 阅读 · 0 评论 -
leetcode490
注意点:遇到墙壁才能停。所以要写一个获取下个落脚点的函数。使用BFS,关键点是判断该点是否遇到过。public class test { int[][] maze; int[][] dir = {{0,1},{1,0},{0,-1},{-1,0}}; public boolean hasPath(int[][] maze, int[] start, int[] destination){ Deque<int[]> queue = new Arra...原创 2021-08-28 12:16:25 · 131 阅读 · 0 评论 -
二叉树前中后序的非递归遍历
// 前序遍历 while(root != null || !stack.isEmpty()){ while(root != null){ res.add(root.val); stack.push(root); root = root.left; } TreeNode cur = stack.pop(); root = cur.right; }...原创 2021-08-20 09:11:22 · 62 阅读 · 0 评论 -
leetcode555
题意是按顺序拼接,每个字符串可翻转,遍历,比较每种情况,public class test { public String splitLoopedString(String[] strs){ int len = strs.length; for(int i=0; i<len; i++){ String rev = new StringBuilder(strs[i]).reverse().toString(); .原创 2021-08-18 10:46:29 · 334 阅读 · 0 评论 -
leetcode536
递归:首先确定出根节点然后确定出左右节点的字符串,把这两个字符串交给递归处理public class test { public TreeNode str2tree(String s){ if(s==null || s.length()==0){ return null; } if(s.charAt(0)=='(' && s.charAt(s.length()-1)==')'){ .原创 2021-08-17 10:48:02 · 364 阅读 · 0 评论 -
leetcode548
暴力求解即可,三层遍历循环优化的方法:前缀数组:缩短了求和的时间i、j、k将j作为外层循环,i和k并行的作为内部循环,减少了循环的层数,缩短了时间public class test { public boolean splitArray(int[] nums){ int len = nums.length; int[] prefixSum = new int[len]; for(int i=0; i<len; i++){ .原创 2021-08-16 09:27:47 · 401 阅读 · 0 评论 -
leetcode356
首先确定出一个中间的轴线,那么就通过左右两侧的点来确定。以此为标杆,观察其他点是否有对称点。public class test { public boolean isReflected(int[][] points){ int minX = Integer.MAX_VALUE; int maxX = Integer.MIN_VALUE; HashMap<String, Integer> map = new HashMap<>.原创 2021-08-10 09:33:29 · 144 阅读 · 0 评论 -
leetcode1055
暴力查询即可,外面一层遍历target字符串,内层对source进行遍历,直到当前的内层遍历source中已无可匹配字符,在进行下一次source遍历,直到外层遍历结束。最终统计进行了几次内层遍历。优化的方法:将内层遍历改为二分查找,进而衍生的需要用map存储source的每个字符对应的下标的列表。public class test { public int shortestWay(String source, String target){ Map<Character..原创 2021-08-09 09:37:47 · 302 阅读 · 0 评论 -
leetcode484
可以使用DFS暴力回溯但是也可以先把数字从小到大排序好,然后确定出递减区间在哪,最终把递减区间对应的位置翻转即可,这里用到了栈来进行翻转。public class test { public int[] findPermutation(String s){ int len = s.length(); int[] res = new int[len+1]; int index = 0; Deque<Integer> sta原创 2021-08-06 09:35:04 · 136 阅读 · 0 评论 -
leetcode314
所谓的垂直遍历。就是从左到右,从上到下。采用BFS实现从上到下。那么从左到右如何实现呢?自定义MyNode记录偏移量,用TreeMap存储每个偏移量的列表,最终可以按顺序输出。public class test { List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> verticalOrder(TreeNode root){原创 2021-08-04 21:49:56 · 207 阅读 · 0 评论 -
leetcode333
当前节点作为根节点的话,对应的子树是否为BST,需要判断他的左子树的状态和它的右子树的状态,并且数值是否满足大小关系。所以需要后序遍历。那么如何记录数字排序的状态呢?通过自己封装一层MyNode,记录所在子树的最大值和最小值。走到每个点的时候,都会判断左子树的最大值,右子树的最小值和当前节点的值进行大小比较。public class test { int res = 0; public int largeestBSTSubTree(TreeNode root){ po.原创 2021-08-03 10:21:03 · 197 阅读 · 1 评论 -
leetcode261
两种特殊的情况:有环的存在,有孤独的点的存在对这两种情况单独存在时候,有个粗暴的判断方式 那就是边的数量为点的数量减1采用BFS,一层一层的向外扩,用一个集合存储经过的点,最终判断经过点的数量是否为npublic class test { public boolean validTree(int n, int[][] edges){ if(n != edges.length+1){ return false; } Arr.原创 2021-08-02 14:58:22 · 199 阅读 · 0 评论 -
leetcode1062
暴力解法,遍历,找相同采用二分查找,查找的是相同的长度public class test { public boolean isRepeat(String s, int len){ for(int i=0; i<s.length()-len; i++){ for(int j=i+1; j<s.length()-len; j++){ if(s.substring(i,i+len).equals(s.substri.原创 2021-07-31 22:23:45 · 184 阅读 · 0 评论 -
leetcode723
当前状态清完之后如果还有满足条件的,还需要继续清理。因此需要递归。记录需要清理的状态,然后清理,用一个flag进行状态标识,决定是否需要继续进行递归清理public class test { public int[][] candyCrush(int[][] board){ if(board == null || board.length == 0 || board[0].length == 0){ return board; }...原创 2021-07-31 21:46:29 · 104 阅读 · 0 评论 -
leetcode320
经典的DFS加回溯,列举出全部的情况public class test { private List<String> res; public List<String> generateAbbreviations(String word){ res = new ArrayList<>(); char[] chars = word.toCharArray(); backtrack(chars, 0); .原创 2021-07-31 17:38:33 · 114 阅读 · 0 评论 -
leetcode1101
并查集,排序数组,当集合只为一个的时候,返回当前的时间public class test { public int earliestAcq(int[][] logs, int N){ Arrays.sort(logs, Comparator.comparingInt(a -> a[0])); //Arrays.sort(logs, (a, b) -> (a[0] - b[0])); Friend f = new Friend(N);...原创 2021-07-31 12:01:24 · 124 阅读 · 0 评论 -
leetcode776
采用递归:public class test { public TreeNode[] splitBST(TreeNode root, int v){ if(root == null){ return new TreeNode[]{null, null}; } else if(root.val <= v){ TreeNode[] bns = splitBST(root.right, v); ...原创 2021-07-31 10:09:21 · 90 阅读 · 0 评论 -
leetcode651
动态规划,两种情况:1.基于上一步的最大数量+12.基于距离前面两步的最大数量,CA,CC,CV,public class test { public int maxA(int N){ int[] dp = new int[N+1]; dp[0] = 0; for(int i=1; i<=N; i++){ dp[i] = dp[i-1] + 1; for(int j=2; j<=i...原创 2021-07-28 22:16:05 · 230 阅读 · 0 评论 -
leetcode625
public class test { public int smallestFactorization(int a){ if(a <= 1){ return a; } long b = 0; long tmp = 1; for(int i=9; i>1; i--){ while(a % i == 0){ a /= i; .原创 2021-07-28 21:42:27 · 63 阅读 · 0 评论 -
leetcode351
并查集 + DFS并查集的作用就是维护某种类型的群体:相隔的群体,(1,3,7,9)(2,8)(4,6)DFS的作用就是列举出各种情况 ,深度的遍历。在列举的过程中,也要根据题意进行判断。public class test { private WeightedUnionFind wuf = new WeightedUnionFind(10); private int res; public int numberOfPatterns(int m, int n){ .原创 2021-07-20 11:50:37 · 173 阅读 · 0 评论 -
leetcode271
编码的形式就是:字符串的长度 + 字符串字符串的长度转化为byte数组,四位的数组public class test { public String encode(List<String> strs){ StringBuilder sb = new StringBuilder(); for(String s : strs){ sb.append(intToString(s)); sb.append(s).原创 2021-07-19 20:30:31 · 129 阅读 · 0 评论 -
leetcode253
最直接的想法,自己定义个node,这个node下维护个数组,存储重叠的时间段,但是需要实施的排序好,按照结束的时间排序,因此这个数组的size就代表会议室的数量。遍历题目提供的数组,与维护的数组进行一一比较,找到合适的数组内的元素进行合并,然后再将维护的数组进行排序。因此这个过程需要的操作:排序,找到合适的会议室插入此时,就体现了数据结构的重要性,采用PrioriytQueue,就完美的解决了这个问题。public class test { public int minMeetingRoom.原创 2021-07-19 12:05:44 · 93 阅读 · 0 评论 -
leetcode161
根据字符串的长度分为三种情况,区分为增删改三种情况处理public class test { public boolean isOneEditDistance(String s, String t){ int lens = s.length(); int lent = t.length(); if(lens==0 && lent==0){ return false; } boo.原创 2021-07-19 11:04:10 · 142 阅读 · 0 评论 -
leetcode267
首先需要判断,该字符串是否具备拼接成回文串的潜质。然后就是开启拼接的过程。即尝试所有的可能。public class test { public List<String> generate(String s){ List<String> res = new ArrayList<>(); if(s == null || s.length() == 0){ return res; } .原创 2021-07-19 09:38:32 · 243 阅读 · 0 评论 -
leetcode616
遍历dict,找寻s中匹配的字段,并用mark数组记录匹配的情况。然后遍历mark数组,决定在哪添加东西。public class test { public String addBoldTag(String s, String[] dict){ if(s==null || s.length()==0 || dict==null || dict.length == 0){ return s; } boolean[] mar.原创 2021-07-17 21:20:02 · 107 阅读 · 0 评论 -
leetcode549
只需要输出序列的长度,不需要打印出具体的序列是什么。因此需要维护对于每个node的一个数组,这个数组存储着递增的长度和递减的长度。public class test { int maxRes = 0; public int longestConsecutive(TreeNode node){ postOrder(root); return maxRes; } private int[] postOrder(TreeNode node){.原创 2021-07-16 17:04:08 · 143 阅读 · 0 评论 -
leetcode124
https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/就是递归,向上逐渐的累加值。我们不需要知道具体的路径是什么样子的,只需要知道当前节点能取得最大值。这道题有两个值最重要:一个全局得记录值,一个局部的最大值。public class test { int maxRes = Integer.MIN_VALUE; public int maxPathSum(TreeNode node){ postOrd原创 2021-07-16 11:07:39 · 55 阅读 · 0 评论 -
leetcode439
采用递归从头处理或者是采用stack从尾部处理。public class test { public String parseTenary(String expression){ if(expression.length() == 1){ return expression; } int index = 1; int count = 0; while(index < expressi.原创 2021-07-15 17:42:18 · 103 阅读 · 0 评论 -
leetcode325
1.最简单的暴力方法,三层遍历,外面两层分别确定子数组的头和尾,最内的一层为相加的操作2.利用数组前缀和(对数组进行累加,维护出一个代表数组前缀和的数组),然后就类似方法1,确定头尾,只不过是省去了相加的操作,直接相减就可算出和3.利用map进行前缀和、当前数组的尾坐标的存储,从尾到头的遍历前缀和数组。直接从map中取出sum【i】-target的值,即可快速的确定出数组的头尾。public class test { public int maxSubArrayLen(int[] nums,.原创 2021-07-15 16:58:06 · 270 阅读 · 0 评论 -
leetcode156
最终结果的形式是,node.left变为根,node.right变为左节点,node变为右节点public class test { public TreeNode upsideDownBinaryTree(TreeNode root){ if(root == null){ return root; } if(root.left == null && root.right == null){ .原创 2021-07-15 16:27:06 · 172 阅读 · 0 评论