![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Java数据结构与算法
unspoken0714
这个作者很懒,什么都没留下…
展开
-
二分法
适用场景 一般适用场景是有序数组中查找指定元素。 模板 大佬的模板 目标值包含在我们设置的范围中(通过mid来判定)、 # Input: vector<int> nums, targer int left = 0; int right = nums.size()-1; while(left <=right){ # 为了防止溢出 int mid = left + (right - left) /2; if(nums[mid] == target){ return mid; }原创 2021-02-01 07:02:35 · 84 阅读 · 0 评论 -
分治算法
基本思想 将复杂问题分解成小问题 将子问题进行求解 将子问题的解合并 难点: 将 复杂问题分解成小问题 分治算法的基本步骤 分解:将原问题分解为若干个规模较小,相互独立,与原问题形式相同的子问题 解决:若子问题规模较小而容易被解决则直接解,否则递归地解各个子问题 合并:将各个子问题的解合并为原问题的解 案例: 汉诺塔问题 汉诺塔:汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具。大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。大梵天命令婆罗门把圆原创 2020-08-12 23:34:29 · 97 阅读 · 0 评论 -
二分查找(非递归)
二分查找只适用于从有序的数列中进行查找 运行时间为对数时间O(log2n\log_2{n}log2n), 即最多只需要log2n\log_2{n}log2n步。 [0,99]中100个数进行查找,需要26<100<27步,最多需要7步 思路: 确定出归条件,找到了 或者 start>end 找到中间的数,如果比中间数大,则向左找 end = mid-1; 如果比中间数大,则向右找,知道找到位置; start = mid +1; public class erfen { .原创 2020-08-12 20:48:40 · 98 阅读 · 0 评论 -
二叉排序树
规则: 左子节点值 < 父节点值 右子节点值>=父节点值 如果中序遍历二叉排序树,则会升序排列 节点的添加和遍历 public class BinarySortTree { private Node root; public void add(Node node){ if (this.root==null){ this.root = node; }else { this.root.add(nod原创 2020-08-10 21:55:03 · 282 阅读 · 0 评论 -
堆排序(升序,降序,迭代,递归写法)
堆排序 是一种选择排序。 平均时间复杂度为0(nlogn),是不稳定排序 大顶堆: 完全二叉树,每个节点的值都大于或等于其左右子节点的值。(左右子节点的值不作要求) 小顶堆:完全二叉树,每个节点的值都小于或等于其左右子节点的值 大顶堆: 小顶堆: 堆排序的基本思想 将待排序序列构造层一个大顶堆 (左节点2n+1; 右节点2n+2; 父节点(i-1)/2) 序列的最大值处于,顶部的根节点 先将数组变成大顶堆形式 将大顶堆堆顶与最小叶子节点交换,使大顶堆位于最小叶子节点除,将其移除(后面不考虑) 在对原创 2020-08-10 20:53:09 · 1317 阅读 · 0 评论 -
二叉树
二叉树可以按左边小,右边大,构建 二叉树分左右两个两个指针,分别指向左右两个分支。 其遍历方式可以分为三种: 前序遍历:先访问根节点,然后访问左节点,最后右节点 中序遍历:先访问左节点,然后访问根节点,最后右节点 后续遍历:先访问左节点,然后访问右节点,最后根节点 public class node { int num; node left; node right; public node(int num){ this.num = num;原创 2020-08-01 22:26:42 · 230 阅读 · 0 评论 -
稀疏数组+动态内存分配生成二维数组
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> using namespace std; int main() { //int chess[2][2] = {0}; //chess[1][2] = 1; //chess[1][2] = 2; //cout << "原始棋盘" << endl; int a[10][10] = { 0 }; a原创 2020-07-27 11:42:41 · 127 阅读 · 0 评论 -
回溯(n皇后问题+组合总和 I+II+III)
问题描述: 任意两个皇后都不能处于同一行,同一列或者同一斜线上,请问有多少种排列方法 public class MyEightQueen { /** * @ param max: 皇后的数量 * @ param array[]: 储存皇后的摆放方式; * @ param count: 总的摆放数量; */ public int max; public int[]array; public int count; /**原创 2020-07-28 00:02:55 · 240 阅读 · 0 评论 -
递归-迷宫问题(java+c++)
public class migong { int[][] map; /** * * @param row: 迷宫的行 * @param col: 迷宫的列 * 迷宫初始化: 0:表示未走过; 1:表示墙; 2: 走过; 3: 不可行 ; 4: 终点 */ public migong(int row, int col){ map = new int[row][col]; for (int i = 0;原创 2020-07-27 10:11:26 · 96 阅读 · 0 评论 -
Graph+DFS+BFS
Formal definition G: 表示Graph, G = (V, E) V: 表示节点集(nodes set) E: 表示边集(edges set) G1 =(V 1 ,E 1 ), V1 ={0,1,2}, E1 =∅ G2 =(V 2 ,E 2 ), V2 ={0,1,2}, E2 ={{0,1},{0,2}} G3 =(V 3 ,E 3 ), V3 ={0,1,2,3}, E3 ={{0,1},{0,2},{1,2},{1,3}} ...原创 2020-05-15 07:58:01 · 213 阅读 · 0 评论 -
List
List List和array相似,但List可以改变大小和动态编辑 it extends the Collection interface. The new method: E set(int index, E element) E get(int index) int indexOf(Object obj). 获得第一个出现的obj的index,如果不存在,返回-1 int lastIndexOf(Object obj) 获得最后一个出现的obj的index,如果不存在,返回-1 List subl原创 2020-05-14 08:26:43 · 135 阅读 · 0 评论 -
Collection and Collections
Collection and Collections Collection interface int size() returns the number of elements in this collection boolean isEmpty() returns true if this collection contains no elements boolean contains(Object o) returns true if this collection contains the spe原创 2020-05-12 19:00:32 · 181 阅读 · 0 评论 -
Queue, Stack and Deque
Queue, Stack and Deque 队列:先进先出 FIFO enqueue: 进队 dequeue: 出队 复杂度 In case of using a linked list or a classic array (non-resizable) as an internal structure 进队和出队的复杂度都为常数 O(1),不取决于队中有多少个Element。因此非常快。 Java中队列由Queue接口表示,扩展了Collection,添加了新方法, boolean offer(E原创 2020-05-12 18:57:25 · 169 阅读 · 0 评论