算法类
江哥不是我
这个作者很懒,什么都没留下…
展开
-
c++堆排序的实现
今天重温了一下各大排序。打算写一下堆排序,首先要了解它能干什么,有什么区别,定位,怎么实现1.能干什么 对数据进行排序。(废话了)2.和别的排序有什么区别比插入,冒泡,选择排序效率高,但是在性能低于快排,它属于不稳定排序3.定位适合于大量的数据的排序。时间复杂度为O(nlgn)即使在最好和最差情况下。4.怎么实现a.第一步了解他的原理,就是用数组模拟一个二叉树来原创 2014-03-09 20:22:51 · 588 阅读 · 0 评论 -
深搜算法与广搜算法总结
学习深搜和广搜在阿哈算法中有挺好的例子:应用的主要有树,图的遍历,迷宫寻路,还有全排序这类变换过的搜索。深搜的模型:原创 2015-03-26 09:30:43 · 5239 阅读 · 0 评论 -
输出单向链表的倒数第K个元素
#include using namespace std;struct Node{ int data; Node *next;}*PNode;PNode findRerKElem(Node *L,int n){ if( n == 0)return L; Node *k = L, *s = L; int i = 1; while(s != NULL && s-原创 2014-12-13 13:22:37 · 456 阅读 · 0 评论 -
将一颗搜索树转换成它的镜像
#include #include typedef struct tagNode{ int data; tagNode *lchild; tagNode *rchild;} *PNode; void BinaryTreeMirror(PNode T){ if( T == NULL)return; swap(T->lchild,原创 2014-12-13 12:58:34 · 397 阅读 · 0 评论 -
求一颗二叉树中最远的两个节点的距离
#include using namespace std;typedef struct tagNode{ char data; tagNode *lchild; tagNode *rchild;}*PNode;void createtree(PNode &T){ char c; cin >> c; if(c == ',')return; else { T = new原创 2014-12-09 16:37:00 · 1264 阅读 · 0 评论 -
判断排序二叉树的后序遍历是否正确(对递归算的总结)
是#include using namespace std;//排序二叉树的性质/* *.终止条件:1.开始>=结尾返回真 *. 2.s >= e 因为不出现问题的话,一定能到达 s>=e的情况。知道到达了s >= e即为真 *. 1.最后一个节点是root 2.在root之前的节点 连续的大于root的是其右子树, 再之前连续小于root的原创 2014-12-09 16:07:51 · 680 阅读 · 0 评论 -
简单算法 简单矩阵输出
#include using namespace std;int a[100][100];void printRect(int n){ int k = n%2 ? n/2+1 : n/2; int i = 0, sum = 1; while(i < k) { for(int j = i; j <= n-i-1; j++) a[i][j] = sum++; for(in原创 2014-12-09 12:14:55 · 1320 阅读 · 1 评论 -
求重复数字
题目为在一个1001整数组成的数组,这些整数是任意排列的,但是你知道所有的整数都是1到1000(包含1000)之间,此外,除了一个数字出现两次外,其他所有数字只出现一次,假设你只能对这个数组做一次操作,用一种算法找到那个数字,如果你在运算中使用了辅助的储存方式,那么你能找到不用这种方式的算法吗?int hashmapfunc(const int a[],const int n)//使用原创 2014-12-06 15:59:18 · 416 阅读 · 0 评论 -
一个数组是由一个递减数列左移,查找一个数
一个数组是由一个递减数列左移若干位形成的,比如{4,3,2,1,6,5}是由{6,5,4,3,2,1}左移两位形成的,在这样的数组中找一个数思路是: 递归+二分查找但是边界一定要小心的考虑 不同位置的时候处理情况不一样。#include using namespace std;int helper(int a[],int s, int e, int num);int s原创 2014-12-21 17:15:05 · 1138 阅读 · 0 评论 -
找到一个字符串,优化速度,优化空间
BM算法 还没理解,先放着#include //找到一个子字符串 using namespace std;int bm_substr(char *str, char *sub){ int len = strlen(sub); int aux[256] = {0}; for(int i = 0; i < len; i++) { aux[str[i]] = i; } in原创 2014-12-05 16:56:17 · 703 阅读 · 0 评论 -
写一个函数找出连续最长的数字串
简单题。。 1 #include 2 using namespace std; 3 int continueNum( char *str, char *intput ) 4 { 5 int n = 0, maxcount = 0, count = 0; 6 char *p = str, *maxp = NULL, *indexp = NULL; 7原创 2014-12-18 23:56:51 · 414 阅读 · 0 评论 -
颠倒链接表顺序
用一种算法来颠倒一个单链表的顺序,递归和非递归#include using namespace std;struct Node{ int data; Node *next; };Node* createLinkList(int a[], int n){ if(a == NULL || n == 0)return NULL; Node *L = NULL,*t = NUL原创 2014-12-01 15:34:22 · 1045 阅读 · 0 评论 -
查找最小的k个元素
题目: 输入n个整数,输出其中最小的k个数例如: 1 2 3 4 5 6 7 8 这8个数字,则最小的4个数字为1,2,3,4,第一种:直接对其先排序,再取头几个数 这样最快是nlogn(快排或者堆排)#include using namespace std; void partsort(int a[], int l, int r); void QuickSort(int a[]原创 2014-11-30 20:36:02 · 718 阅读 · 0 评论 -
二叉树打印和为某一值的路径
//打印和为某一值的路径template void binaryTree::printPath(treeNode *ptree,int path[], int sum, int top){path[top++] = ptree->data;sum-=ptree->data;if(ptree->lchild == nullptr && ptree->rchild == nullp原创 2014-11-30 13:50:02 · 338 阅读 · 0 评论 -
字数组的最大和
#include using namespace std;int maxSubArray(int a[], int size){int max = -(1 int i = 0, j = 0;int sum = 0;int subArray[size];int start = 0, count = 0, maxstart = 0, maxcount = 0;/原创 2014-11-27 12:50:13 · 408 阅读 · 0 评论