随笔分类 -  算法与数据结构

摘要:题&例子 例题5-1 大理石在哪儿 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 10000; int main() { int n, q, x, a[maxn], kase = 0; whi 阅读全文
posted @ 2020-04-06 21:00 xkfx 阅读(191) 评论(0) 推荐(0)
摘要:例子 #include<cstdio> #include<stack> using namespace std; int main() { stack<int> my_stack; printf("my_stack.empty(): %d\n", my_stack.empty()); // prin 阅读全文
posted @ 2020-04-02 20:55 xkfx 阅读(138) 评论(0) 推荐(0)
摘要:题 例题6-12 油田。完整题目见参考[1]。 #include<cstdio> #include<cstring> const int MAXN = 100 + 5; char pic[MAXN][MAXN]; // 存原图 int m, n, idx[MAXN][MAXN]; // m 和 n 阅读全文
posted @ 2020-03-26 21:36 xkfx 阅读(140) 评论(0) 推荐(0)
摘要:题 二叉树两个结点之间的最短路径长度。完整题目见参考[1]。 #include <cstdio> #include <cstring> #define MAXN 20 int pre[MAXN]; // 存每个结点的父结点 int floor[MAXN]; // 存每个结点所在的层数,根结点在第1层 阅读全文
posted @ 2020-03-24 17:11 xkfx 阅读(558) 评论(0) 推荐(0)
摘要:题 例题6-6 小球下落 - 模拟全过程,这里有几个关于二叉树的重要结论 如果把结点从上到下从左到右编号为1,2,3……,则结点k的左右子结点编号分别为2k和2k+1 (书上原话) 将满二叉树的根结点定义第1层,则由第1层到第n层各层的结点数量,是一个首项为1,公比为2的等比数列。 #include 阅读全文
posted @ 2020-03-23 22:13 xkfx 阅读(168) 评论(0) 推荐(0)
摘要:例子 #include <stdio.h> #include <string.h> // 左移运算符 int main() { for (int i = 0; i != 10 ; ++i) { printf("1 << %d: %d \n", i, 1 << i); } return 0; } /* 阅读全文
posted @ 2020-03-20 21:51 xkfx 阅读(193) 评论(0) 推荐(0)
摘要:题 例题3-3 蛇形填数 #include <stdio.h> #include <string.h> #define maxn 20 int a[maxn][maxn]; int main() { int n, x, y, tot = 0; scanf("%d", &n); // 表示方阵大小 m 阅读全文
posted @ 2020-03-19 20:48 xkfx 阅读(172) 评论(0) 推荐(0)
摘要:题 例题3-1 逆序输出 #include<stdio.h> #define maxn 105 int a[maxn]; int main() { int x, n = 0; while (scanf("%d", &x) == 1) { a[n++] = x; } // n++是事后增加,所以这里a 阅读全文
posted @ 2020-03-18 23:42 xkfx 阅读(187) 评论(0) 推荐(0)
摘要:【参考书籍】 [1] 算法竞赛入门经典,刘汝佳(第2版) [2] 算法导论(第3版) [3] 数据结构,陈越(第2版) [4] C语言程序设计_现代方法(第2版) 阅读全文
posted @ 2020-03-18 17:39 xkfx 阅读(240) 评论(0) 推荐(0)
摘要:索引 思路1:分治策略 思路2:Brute Force - O(n^3) 思路3 问题描述:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 思路1:分治策略 按照算法导论上的分治算法框架强行写 阅读全文
posted @ 2019-01-07 20:42 xkfx 阅读(301) 评论(0) 推荐(0)
摘要:索引 思路1:基本加法规则 思路2:移花接木法。。。 索引 思路1:基本加法规则 思路2:移花接木法。。。 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法规则。。。。。我们需要将两个数以最低位为基准对 阅读全文
posted @ 2019-01-05 20:43 xkfx 阅读(228) 评论(0) 推荐(0)
摘要:索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 根据排列组合原理,列举Cn取2对数 阅读全文
posted @ 2019-01-05 10:15 xkfx 阅读(225) 评论(0) 推荐(0)
摘要:留着备用。 题目描述和代码参考:https://www.geeksforgeeks.org/8-queen-problem/ NQueenProblem(js代码): / mColoringProblem(js代码): 阅读全文
posted @ 2018-11-21 22:15 xkfx 阅读(285) 评论(0) 推荐(0)
摘要:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script type="text/javascript"> let log = console.log.bind(console 阅读全文
posted @ 2018-11-20 09:51 xkfx 阅读(280) 评论(0) 推荐(1)
摘要:栈与队列js实现版本: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script type="text/javascript"> let log = console.log. 阅读全文
posted @ 2018-11-17 10:16 xkfx 阅读(354) 评论(0) 推荐(0)
摘要:快排和归并排序一样采用经典的分治思想,不同的地方在于,归并排序的关键步骤在于“合并”,而快速排序的关键步骤在于“分解”。快速排序的最坏运行时间虽然是O(n^2),不过在实际中基本都比归并排序快,并且可以通过随机选择主元来防止极端的输入。它的具体步骤如下↓ 分解:快排的核心步骤,其结果是数组被分成以某 阅读全文
posted @ 2018-11-16 17:15 xkfx 阅读(450) 评论(0) 推荐(0)
摘要:6.5-1 略 6.5-2 略 6.5-3 #005# 优先队列 6.5-4 为了正确复用Heap-increase-key中的代码 6.5-5 初始化:显然满足 保持:如果在迭代之前循环不变式为真,那么要么不再进行迭代(A[parent(i)]>A[i]),此时已经保持了循环不变式,要么进行一次交 阅读全文
posted @ 2018-10-15 09:48 xkfx 阅读(657) 评论(0) 推荐(1)
摘要:留着备用。 “first-in largest-out”最大优先队列: 便于copy版最大优先队列: class PriorityQueue { constructor() { this.A = []; Heap.buildMaxHeap(this.A); } isEmpty() { return 阅读全文
posted @ 2018-10-14 15:17 xkfx 阅读(223) 评论(0) 推荐(0)
摘要:留着备用。 Heap是一种满足特定性质的数组,如下: 阅读全文
posted @ 2018-10-13 15:01 xkfx 阅读(177) 评论(0) 推荐(1)
摘要:原址排序,且时间复杂度是O(nlgn)的一种排序算法。 js代码: 阅读全文
posted @ 2018-10-10 15:52 xkfx 阅读(145) 评论(0) 推荐(0)