随笔分类 -  LeetCode

上一页 1 ··· 7 8 9 10 11 12 下一页
摘要:优先队列 import java.util.Comparator; import java.util.PriorityQueue; class Solution { public String[] findRelativeRanks(int[] score) { /** * 使用优先队列从小到大存储 阅读全文
posted @ 2021-11-01 21:58 振袖秋枫问红叶 阅读(43) 评论(0) 推荐(0)
摘要:class KthLargest { int k; PriorityQueue<Integer> pq; public KthLargest(int k, int[] nums) { this.k = k; pq = new PriorityQueue<>(); for (int i = 0; i 阅读全文
posted @ 2021-11-01 21:18 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)
摘要:优先队列 import java.util.Comparator; import java.util.PriorityQueue; class Solution { public int[] kWeakestRows(int[][] mat, int k) { /** * 使用优先队列 * 如果两行 阅读全文
posted @ 2021-11-01 21:06 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要:优先队列 import java.util.Collections; import java.util.PriorityQueue; class Solution { public int maxProduct(int[] nums) { /** * 使用优先队列 */ PriorityQueue< 阅读全文
posted @ 2021-11-01 21:06 振袖秋枫问红叶 阅读(29) 评论(0) 推荐(0)
摘要:优先队列 import java.util.Collections; import java.util.PriorityQueue; class Solution { public int lastStoneWeight(int[] stones) { /** * 使用优先队列创建最大堆 * Col 阅读全文
posted @ 2021-11-01 16:55 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)
摘要:优先队列 import java.util.PriorityQueue; class KthLargest { /** * 使用优先队列创建最小堆,最多存放三个元素 * 每次添加新元素时和堆顶元素进行比较,堆顶元素就一直是第k大的元素 */ PriorityQueue<Integer> pq = n 阅读全文
posted @ 2021-11-01 16:42 振袖秋枫问红叶 阅读(42) 评论(0) 推荐(0)
摘要:import java.util.TreeSet; public class Algorithm { public static void main(String[] args) { String[] words = {"gin", "zen", "gig", "msg"}; System.out. 阅读全文
posted @ 2021-10-28 21:33 振袖秋枫问红叶 阅读(45) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { /** * 如果根节点值大于两者,说明在根节点的左子树,小于则说明在右子树 * 在中间则说明根节 阅读全文
posted @ 2021-10-28 11:11 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public TreeNode invertTree(TreeNode root) { if (root == null){ return null; } /** * 后序遍历 */ TreeNode left = invertTree(root.le 阅读全文
posted @ 2021-10-28 10:44 振袖秋枫问红叶 阅读(28) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { List list = new LinkedList(); public List<Integer> postorderTraversal(TreeNode root) { if (root == null){ return list; } posto 阅读全文
posted @ 2021-10-27 20:26 振袖秋枫问红叶 阅读(62) 评论(0) 推荐(0)
摘要:深度优先搜索 import java.util.ArrayList; import java.util.List; class Solution { List list = new ArrayList(); public List<Integer> preorderTraversal(TreeNod 阅读全文
posted @ 2021-10-27 20:19 振袖秋枫问红叶 阅读(42) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public boolean hasPathSum(TreeNode root, int targetSum) { /** * 递归终止条件 * 节点为空时返回false * 节点没有孩子,剩余的数值等于节点值时返回true */ if (root = 阅读全文
posted @ 2021-10-27 20:09 振袖秋枫问红叶 阅读(28) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public int minDepth(TreeNode root) { if (root == null){ return 0; } Queue<TreeNode> queue = new LinkedList<>(); int depth = 1; 阅读全文
posted @ 2021-10-27 18:57 振袖秋枫问红叶 阅读(43) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public boolean isSymmetric(TreeNode root) { return isSymmetric(root.left, root.right); } /** * 后序遍历 * 从根节点的左右孩子开始递归 */ public 阅读全文
posted @ 2021-10-27 18:36 振袖秋枫问红叶 阅读(28) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { /** * 除非p和q共同为null或者都不为null,否则返回false */ if (p == null || q == null){ retu 阅读全文
posted @ 2021-10-27 17:03 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { List list = new LinkedList(); public List<Integer> inorderTraversal(TreeNode root) { if (root == null){ return list; } inorder 阅读全文
posted @ 2021-10-27 16:42 振袖秋枫问红叶 阅读(47) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public int maxDepth(TreeNode root) { if (root == null){ return 0; } /** * 根节点的高度就是二叉树的最大深度,使用前序遍历求的就是深度,使用后序遍历求的是高度 * 本题可以通过后序 阅读全文
posted @ 2021-10-27 16:29 振袖秋枫问红叶 阅读(41) 评论(0) 推荐(0)
摘要:二分查找 class Solution { public int shipWithinDays(int[] weights, int days) { /** * 最小运输量为所有货物中最重的那个,否则大于它的货物永远不能运载 */ int minLoad = Arrays.stream(weight 阅读全文
posted @ 2021-10-25 22:50 振袖秋枫问红叶 阅读(36) 评论(0) 推荐(0)
摘要:二分查找 class Solution { public int minEatingSpeed(int[] piles, int h) { /** * Arrays.stream(arr).max().getAsInt()方法获取数组最大值 */ int minSpeed = 1; int maxS 阅读全文
posted @ 2021-10-25 21:52 振袖秋枫问红叶 阅读(55) 评论(0) 推荐(0)
摘要:public class Algorithm { public static void main(String[] args) { Integer[] arr = {1,2,3,4,5,6,7}; System.out.println(new BinarySearch().search(arr, 4 阅读全文
posted @ 2021-10-24 16:05 振袖秋枫问红叶 阅读(22) 评论(0) 推荐(0)

上一页 1 ··· 7 8 9 10 11 12 下一页