上一页 1 ··· 23 24 25 26 27 28 29 30 31 ··· 36 下一页
摘要: 在普通堆排序中,原始的数组必须要先添加进一个maxHeap堆,再将堆中的元素取出来放到一个temp数组,这个堆和数组都需要消耗额外的空间,是非原地排序,复杂度都是O(nlogn) 因此可以对这两个过程进行优化: 不用将原始数组添加进一个额外的堆,而是直接将原始数组看成一个堆,用heapify()方法 阅读全文
posted @ 2021-10-31 15:56 振袖秋枫问红叶 阅读(72) 评论(0) 推荐(0)
摘要: 动态数组实现二叉堆 二叉堆的定义 二叉堆是一颗完全二叉树,元素按层级从左到右排列成树。空的分支只能在右下方 堆中每个节点的值都大于等于其孩子的值(最大堆) 根节点索引从1开始,从上到下、从左到右依次用数组存储,父节点索引为n时,子节点为2n和2n + 1;子节点为n时,父节点为1/2n 根节点索引从 阅读全文
posted @ 2021-10-31 15:55 振袖秋枫问红叶 阅读(57) 评论(0) 推荐(0)
摘要: 基于二分搜索树的集合实现(Set) 不能添加重复元素 集合是有序的,效率高 interface Set<E>{ void add(E e); void remove(E e); boolean contains(E e); int getSize(); boolean isEmpty(); } cl 阅读全文
posted @ 2021-10-29 14:59 振袖秋枫问红叶 阅读(38) 评论(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 振袖秋枫问红叶 阅读(31) 评论(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 振袖秋枫问红叶 阅读(27) 评论(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)
上一页 1 ··· 23 24 25 26 27 28 29 30 31 ··· 36 下一页