随笔分类 -  算法

记录算法
摘要:题目链接 **思路:**通过一个队列来记录每一层节点的情况,使用队列的节点大小来记录。 未优化代码 public List<List<Integer>> levelOrder(TreeNode root) { Queue<TreeNode> q = new LinkedList<>(); Queue 阅读全文
posted @ 2020-09-16 09:07 Bears9 阅读(104) 评论(2) 推荐(0)
摘要:[题目链接](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/) 思路一 // 思路一,hashmap记录hashcode,没有重写的hashcode都与内存地址有关,所以这样是可以的。 public ListNod 阅读全文
posted @ 2020-09-15 15:30 Bears9 阅读(148) 评论(0) 推荐(0)
摘要:题目链接 代码来自 public int lengthOfLongestSubstring(String s) { if (s.length() == 0) return 0; HashMap<Character, Integer> map = new HashMap<Character, Inte 阅读全文
posted @ 2020-09-07 09:05 Bears9 阅读(94) 评论(0) 推荐(0)
摘要:题目链接 代码 /** * 滑动窗口的写法 */ public int longestOnes(int[] A, int K) { // 滑动窗口的左边界 // 统计滑动窗口中零的的个数 int left = 0,countZero = 0; //i即窗口的有边界 for (int i = 0; i 阅读全文
posted @ 2020-09-05 08:42 Bears9 阅读(123) 评论(0) 推荐(0)
摘要:测试 阅读全文
posted @ 2020-09-01 14:44 Bears9 阅读(98) 评论(0) 推荐(0)
摘要:剑指 Offer 33. 二叉搜索树的后序遍历序列 递归写法 题目链接 代码来自 public boolean verifyPostorder(int[] postorder) { return recur(postorder, 0, postorder.length - 1); } boolean 阅读全文
posted @ 2020-08-24 21:18 Bears9 阅读(96) 评论(0) 推荐(0)
摘要:题目意思: 输入N,M。 N表示,有一个序列,1,2,3,4,5...N(N为偶数,但是测试样例好像有奇数TAT...下面的代码也没有过,只是提供一个思路) M表示接下来输入M个数字,输入的数字只能是1,2 1 表示将序列首元素插入序列尾部 2 表示将序列上将奇偶位置上的元素对调 样例 4 3 1 阅读全文
posted @ 2020-08-23 07:46 Bears9 阅读(156) 评论(0) 推荐(0)
摘要:题目链接 **思路 : **使用方向键,或者模拟(模拟太容易出问题,建议使用方向键)。 代码 public List<Integer> spiralOrder(int[][] matrix) { List<Integer> list = new ArrayList<>(); if(matrix == 阅读全文
posted @ 2020-08-22 09:00 Bears9 阅读(107) 评论(0) 推荐(0)
摘要:题目链接 AC代码 /** * 经典贪心题目,挑选结束时间早的(放到这题就是挑选右区间更小的),这样能尽可能多的选择任务数量 * (放到这题就是能选择更多的不重叠的区间) */ public int eraseOverlapIntervals(int[][] intervals) { if(inte 阅读全文
posted @ 2020-08-21 14:29 Bears9 阅读(117) 评论(0) 推荐(0)
摘要:题目链接 思路:比较标准的深搜,没什么好讲的。 代码 class Solution { public static int []A; public static int []vis; public static int N; public static int []source; public Li 阅读全文
posted @ 2020-08-18 09:13 Bears9 阅读(91) 评论(0) 推荐(0)
摘要:题目链接 Java代码 import java.util.*; public class Main{ // N表示路口 M表示路口之间的路线数量 public static int N, M; // 用来装节点 public static int [][] graph; // 用来挑出最小花费的节点 阅读全文
posted @ 2020-08-17 10:25 Bears9 阅读(89) 评论(0) 推荐(0)
摘要:题目链接 /** * 简单单调栈 * 题目思路是:用栈来读取字符串(从前往后读),如果当前堆顶数字比字符串中的数字大,则抛出(至于为什么能成立,贪心,细节都在代码中) * 来个例子 * stack String * 3,3 2 此时抛出 3 (抛出3之后,堆顶元素依然3大于2,接着抛出) * */ 阅读全文
posted @ 2020-08-17 09:18 Bears9 阅读(172) 评论(0) 推荐(0)
摘要:(5)归并排序 递归代码, 代码来自 public int[] sort(int[] sourceArray) throws Exception { // 对 arr 进行拷贝,不改变参数内容 int[] arr = Arrays.copyOf(sourceArray, sourceArray.le 阅读全文
posted @ 2020-08-16 08:43 Bears9 阅读(157) 评论(0) 推荐(0)
摘要:参考大神博客 原理什么的直接看大神博客吧,我这里只做一个记录。 最小生成树 Kruskal思想 将边通过边权进行升序排序,每次选取最小的边(贪心),如果当前的边, 属于另外一组,则将其加入到另外一组,如果属于任何一组,则自成一组。 (算法能成立的核心是,排序 + 贪心 + 并查集合并) (1)连接两 阅读全文
posted @ 2020-08-14 13:30 Bears9 阅读(94) 评论(0) 推荐(0)
摘要:这个算法可以用来解决分组,合并等问题。 代码 1 public static int MAXN = 10010; 2 // 作为承载并查集的数组 3 public static int []f = new int[MAXN]; 4 // 初始化并查集 5 public static void ini 阅读全文
posted @ 2020-08-13 21:28 Bears9 阅读(102) 评论(0) 推荐(0)
摘要:第二题 题目 给一个n*m的表格染色,相邻(左右上下,紧挨着)的格子颜色必须不同,每个颜色染的格子数量必须相同,问最少需要多少种颜色。 思路 输入 输出 1,1 1 2,3 2 10,10 2 3,3 3 15,27 3 77,49 7 自己画个图推一推,发现了什么?仔细想想应该知道怎么做了。 第三 阅读全文
posted @ 2020-08-13 10:55 Bears9 阅读(224) 评论(0) 推荐(0)