算法常用模板
Justdoforever
读书是为了更出色的人生!
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
并查集模板 统计连通分量
并查集模板 统计连通分量package com.company;public class bingchaji { int count; // 联通分量个数 int[] parent; //记录根节点 int[] cnt;// 记录以根节点记录该树的节点数量,只有一个树的根节点的 // 记录才是准确的,cnt[parent] public bingchaji(int n) { parent = new int[n+1]; cnt =原创 2021-04-26 20:21:39 · 204 阅读 · 0 评论 -
冒泡排序 插入排序 选择排序 快速排序 归并排序 堆排序计数排序实现 Java
冒泡排序 插入排序 选择排序 快速排序 归并排序 堆排序计数排序实现import java.util.Arrays;public class maopao { public static void main(String[] args) { int[] a = {7, 8,9,6,3,5,6,7}; maopao maopao = new maopao();// maopao.sort(a);// maopao.insertS原创 2021-04-26 20:20:10 · 168 阅读 · 0 评论 -
滑动窗口模板
滑动窗口模板 while (right < n) { // 当前窗口最右边元素 char c = s.charAt(right); // 窗口增加 right++; // 窗口更新 ...... // 满足窗口缩小条件时 while ( 满足窗口缩小条件时) {原创 2021-03-28 16:09:26 · 206 阅读 · 0 评论 -
前序中序后序实现二叉树遍历(迭代、非递归)java
前序中序后序实现二叉树遍历(迭代、非递归)二叉树先序、中序、后序遍历超级简单的实现(Java)前序遍历迭代 public void preOrder(TreeNode root) { Deque<TreeNode> stack = new LinkedList<>(); TreeNode cur = root; while (cur != null || !stack.isEmpty()) {原创 2021-03-13 20:35:02 · 184 阅读 · 0 评论 -
有关反转链表模板(汇总)
有关反转链表模板反转整个链表(迭代与递归实现)// 迭代实现 ListNode reverse(ListNode head) { ListNode pre = null, cur = head, next; while (cur != null) { next = cur.next; cur.next = pre; pre = cur; cur = next;原创 2021-03-05 10:04:14 · 265 阅读 · 1 评论 -
快慢指针模板
快慢指针模板链表找中间节点偶数个数节点中点为中间靠左,奇数节点中间 ListNode findMidNode(ListNode head) { ListNode slow = head, fast = head; while (fast.next != null && fast.next.next != null) { // 改为fast != null && fast.next != null则为偶数个数节点中点为中间靠右,奇数节原创 2021-03-05 09:51:56 · 320 阅读 · 2 评论 -
快速幂模板
快速幂模板public long myPow(int a, int b) { long res = 1, x = a; // x初始化为底数 while(b != 0) { if((b & 1) == 1) { res = res * x % 1000000007; } x *= x; // x变为x的平方并取余,因为指数变2倍,则底数为底数的平方原创 2021-02-28 17:22:41 · 121 阅读 · 0 评论 -
归并排序模板
归并排序模板void guiBing(int[] nums, int left, int right) { if(left >= right) {return ;} int mid = (left + right) / 2; guiBing(nums, left, mid); guiBing(nums, mid + 1, right); merge(nums, left, mid, right); }原创 2021-02-23 11:25:11 · 101 阅读 · 0 评论 -
数状数组模板
数状数组模板树状数组原理详解: https://zhuanlan.zhihu.com/p/25185969class BIT { int n; int[] tree; public BIT(int n) { this.n = n; this.tree = new int[n + 1]; }// 更新tree[i]到tree[n]的值 public void update(int i, int delta) { w原创 2021-02-23 11:23:12 · 136 阅读 · 0 评论 -
快速排序模板
快速排序模板 void qSort(int[] arr, int left, int right) { if(left >= right) { return ; } while(left < right) { int i = left, j = right, tmp = 0, partion = arr[left]; while(i < j) {原创 2021-02-19 12:04:20 · 122 阅读 · 0 评论 -
二分查找模板
二分查找模板 // 普通二分查找 int binary_search(int[] nums, int target) { int left = 0, right = nums.length - 1; while(left <= right) { int mid = left + (right - left) / 2; if (nums[mid] < target) { left = mid +原创 2021-02-12 19:22:59 · 132 阅读 · 0 评论
分享