LC.P2583[二叉树中的第K大层和]
LC.P2583[二叉树中的第K大层和]
方法一:BFS+排序123456789101112131415161718192021222324252627282930313233343536/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */class Solution { ...
LC.P889[根据前序和后序遍历构造二叉树]
LC.P889[根据前序和后序遍历构造二叉树]
方法一:DFS+哈希表123456789101112131415161718192021222324252627282930313233343536373839/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */class Soluti ...
LC.P106[从中序与后序遍历序列构造二叉树]
LC.P106[从中序与后序遍历序列构造二叉树]
方法一:DFS+哈希表12345678910111213141516171819202122232425262728293031323334353637383940/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */class Sol ...
LC.P105[从前序与中序遍历序列构造二叉树]
LC.P105[从前序与中序遍历序列构造二叉树]
方法一:DFS+哈希表12345678910111213141516171819202122232425262728293031323334353637383940/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */class Sol ...
LC.P2641[二叉树的堂兄弟节点II]
LC.P2641[二叉树的堂兄弟节点II]
方法一:BFS12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } ...
LCP.P30[魔塔游戏]
LCP.P30[魔塔游戏]
方法一:贪心+优先队列123456789101112131415161718192021222324252627class Solution { public int magicTower(int[] nums) { long sum = 0; for (int num : nums) { sum += num; } if (sum < 0) { return -1; } int ans = 0; long hp = 1; PriorityQueue<Integer> q = new PriorityQueue<>(); for (int num : nums) { if (num < 0) { q.offer(num ...
LC.P1686[石子游戏VI]
LC.P1686[石子游戏VI]
方法一:贪心+排序123456789101112131415class Solution { public int stoneGameVI(int[] a, int[] b) { int n = a.length; Integer[] index = new Integer[n]; for (int i = 0; i < n; i++) { index[i] = i; } Arrays.sort(index, (i, j) -> a[j] + b[j] - a[i] - b[i]); int diff = 0; for (int i = 0; i < n; i++) { diff += i % 2 == 0 ? a[index[i]] : -b[index[i]]; } return Integer.c ...
CompletableFuture异步多任务最佳实践
CompletableFuture异步多任务最佳实践简介在项目中需要执行异步任务时,通常可以考虑到可以用线程池Executor创建。
如果不需要有返回值,任务实现Runnable接口
如果需要有返回值,任务实现Callable接口,调用Executor的submit方法,再使用Future获取即可
如果多个线程存在依赖组合,可使用同步组件CountDownLatch、CyclicBarrier以及使用起来较为简单的CompeletableFuture等
在Java 8中引入了CompletableFuture,其对Java 5中的Future类提供了非常强大的拓展,可以帮助我们简化异步编程的步骤,并且提供了函数式编程的能力,可以通过回调的方式处理计算结果,也提供了转换和组合CompletableFuture的方法,适用于处理并发的IO密集型任务。
CompletableFuture类图
回顾Future因为CompletableFuture实现了Future接口,因此先回顾Future的用法。
Future是Java 5新加的一个接口,它提供了一种异步并行计算的功能。如果主线程 ...
LC.P2670[找出不同元素数目差数组]
LC.P2670[找出不同元素数目差数组]
方法一:暴力123456789101112131415161718192021222324class Solution { public int[] distinctDifferenceArray(int[] nums) { Set<Integer> set = new HashSet<>(); int n = nums.length; int[] ans = new int[n]; for (int i = 0; i < n; ++i) { for (int j = 0; j <= i; ++j) { if (!set.contains(nums[j])) { set.add(nums[j]); ans[i]++; } ...
LC.P2859[计算K置位下标对应元素的和]
LC.P2859[计算K置位下标对应元素的和]
方法一:模拟(调包)1234567891011121314151617181920class Solution { public int sumIndicesWithKSetBits(List<Integer> nums, int k) { int ans = 0; for (int i = 0; i < nums.size(); ++i) { if (bicCount(i) == k) { ans += nums.get(i); } } return ans; } private int bicCount(int x) { int cnt = 0; while (x > 0) { x &= x - 1; ...