数据结构与算法
lilillos
这个作者很懒,什么都没留下…
展开
-
vector扩容机制带来的bug
起因是我写了这样一段代码:将指向vector中元素的指针加入到另一个vector中。 //graph.edges是vector<T> graph.edges.emplace_back((&graph.map_id_node[from]), (&graph.map_id_node[to]), weight); //graph.map_id_node[from].edges是vector<T*> graph.map_id_node[from].edges.push_b原创 2021-09-30 18:11:45 · 361 阅读 · 0 评论 -
栈实现,不是用template做的.
用单链表来实现栈,插入和删除的部分很有意思。 写完这个感觉突然理解了书上说的适配器的观点,类就是这样,不管你底层实现是什么,表现成什么就好了,是一个黑盒。 头文件Mystack.h #ifndef MYSATCK_H #define MYSTACK_H struct Node { int data; Node *next; }; class LUStack { publi原创 2016-07-24 20:33:19 · 215 阅读 · 0 评论 -
优先队列(非模板函数,是int)
发现了一个可能是书里的错误,在swim函数中while检测的条件 void priorityQueue::Swim(size_t t) { inArr[0]=inArr[t]; while (inArr[t/2]<inArr[t])//这里我认为不对,我认为因是inArr[0] { inArr[t]=inArr[t/2]; t/=2; } inArr[t]=inArr原创 2016-08-14 16:11:54 · 325 阅读 · 0 评论 -
杨辉三角,一个vector实现,不复制,不用队列。
昨晚写到纸上,今天晚上调通,本来想要动态数组,但是发现这块的知识还欠缺,用着有问题。自己基础知识一直有问题,真是心急,要学习的知识好多,都不知道先学哪个。 整个代码效率应该很低,因为进行了大量的判断。 目前没有在网上搜到一样的代码,有点小虚荣。#include #include using std::cin; using std::vector; using std::cout; usi原创 2016-07-13 23:21:36 · 562 阅读 · 0 评论 -
队列简要实现,是queue,不是dequeue
#ifndef MYQUEUE_H #define MYQUEUE_H struct Node { int data; Node *next; }; class LUqueue { public: LUqueue(); ~LUqueue(); void Push(int data); int Top(); void Pop(); private: void Insert(No原创 2016-07-24 21:09:11 · 461 阅读 · 0 评论 -
二分查找(改)
二分查找,应该是面试里比较简单但是也会问道的问题。 这个代码,是我初看见二分查找的原理时写的,用的递归。好吧,有明显的错误,就是当寻找的值不在数组中时会越界。 int FindX(const vector& a,int left,int right ,int k) { int center = (left + right) / 2; if (k>a[center]) { Find原创 2016-03-25 11:43:16 · 329 阅读 · 0 评论 -
桶排序
#include #include int MaxValue(std::vectorb); void BarrelSort(int c[], std::vectorb) { int max = MaxValue(b); int* a=new int [max+1]; for (int i = 0; i < max+1; i++) { a[i] = 0; } for (std::原创 2016-04-19 20:08:33 · 402 阅读 · 0 评论 -
AVL树单选转和双旋转
看了好几天树了,也算有点心得。学树,必须要图。这里有两篇里的博客,在结合书,我用的是《数据结构与算法分析C++版》就可以了。 http://blog.csdn.net/a454042522/article/details/8591421 http://blog.csdn.net/vesper305/article/details/13614403 在AVL树中必须平衡的点叫做原创 2016-04-02 14:06:30 · 788 阅读 · 0 评论 -
采用递归求数组里面求最大子序列的算法(手绘图解)
本文改编自我在知乎的回答。http://t.cn/RqPi9FO 先上代码。 private static int maxSumRec( int [ ] a, int left, int right ) { int maxLeftBorderSum = 0, maxRightBorderSum = 0; int leftBorderSum = 0原创 2016-03-25 08:41:07 · 843 阅读 · 0 评论