摘要: private void buildHeap(int[] h) { int l = h.length; //这里需要从下向上调整,因为从上向下调整只能让最小值下到底端,只有从下向上调整可以让最大值上到顶端 //shiftDown需要递归,如果不递归,这里只能保证最大的元素在堆顶,但是不能保证子分支都 阅读全文
posted @ 2022-11-10 00:34 yanher 阅读(21) 评论(0) 推荐(0)
摘要: 堆是 完全二叉树, 不一定是满二叉树, 可以用数组存放(普通二叉树只能用带指针的链表) i 节点的父节点 下标为 i/2 , 子节点下标为 2i , 2i+1 , 二叉树高度或深度为 logN+1(计算机领域底数为2) 阅读全文
posted @ 2022-11-09 11:20 yanher 阅读(30) 评论(0) 推荐(0)
摘要: PriorityQueue的底层实现是 堆, 默认为最小堆: PriorityQueue<Integer> q = new PriorityQueue<>(); 最大堆初始化: PriorityQueue<Integer> q = new PriorityQueue<>((o1, o2)->o2.c 阅读全文
posted @ 2022-11-08 20:12 yanher 阅读(24) 评论(0) 推荐(0)
摘要: stack底层为数组, foreach和iterator迭代时遍历数组输出, public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializabl 阅读全文
posted @ 2022-11-06 08:30 yanher 阅读(92) 评论(0) 推荐(0)
摘要: 1. ArrayList[] result = new ArrayList[len]; 数组泛型类型erase, 不能定义泛型数组 2. Arrays.asList(result) 返回的不是ArrayList类型,而是一个内部类 所以需要用ArrayList包装 new ArrayList<>(A 阅读全文
posted @ 2022-11-01 23:22 yanher 阅读(154) 评论(0) 推荐(0)
摘要: add 添加, remove 删除和返回, element 检索和返回 会抛出异常 offer 添加, poll 删除和返回, peek 检索和返回 会返回值 阅读全文
posted @ 2022-10-31 19:54 yanher 阅读(15) 评论(0) 推荐(0)
摘要: public String LCS (String str1, String str2) { int max = 0; //存储中间值 int[][] p = new int[str1.length()][str2.length()]; StringBuilder temp = new String 阅读全文
posted @ 2022-10-29 18:53 yanher 阅读(23) 评论(0) 推荐(0)
摘要: 方法一 递归 import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param cost int整型一维数组 * @return int整型 */ public in 阅读全文
posted @ 2022-10-29 14:58 yanher 阅读(47) 评论(0) 推荐(0)
摘要: import java.util.*; public class Solution { private int res; public int Nqueen (int n) { res = 0; //下标为行号,元素为列号,记录皇后位置 int[] pos = new int[n]; Arrays. 阅读全文
posted @ 2022-10-26 21:28 yanher 阅读(27) 评论(0) 推荐(0)
摘要: import java.util.*; import java.math.*; public class Solution { HashSet<Integer> res = new HashSet<Integer>(); //已经摆放几个 int leng = 0; //多少种摆法 int coun 阅读全文
posted @ 2022-10-26 15:52 yanher 阅读(29) 评论(0) 推荐(0)