数据结构
文章平均质量分 94
暗黑独角兽
这个作者很懒,什么都没留下…
展开
-
比较两个二叉树是否相同
1、二叉树定义2、比较两个二叉树结构是否相同,不涉及存储的数据(1)递归方式如果两个二叉树pRoot都为空树,则自然相同,返回true;如果两个二叉树pRoot一个为空树,另一个不为空树,则不相同,返回false;如果两个二叉树都不为空树,则需要分别比较左右子树后,根据比较结果共同判定,只要有一个为false,则返回false。bool转载 2016-03-05 15:56:56 · 1042 阅读 · 0 评论 -
C++ STL 容器、迭代器、适配器
1、容器vector,list,deuqe是C++STL中三种基本容器实现,它们不可能互为实现同时又不损失效率2、stack和queue则都可以在这三种基本容器序列基础上实现,所以没有定义为独立的容器,而只作为基本容器适配器3、容器适配器所提供的是原来容器的一个受限的界面,适配器不提供迭代器4、所有stack和queue是用deque基本容器作为实现方式的原创 2016-03-31 15:36:58 · 3834 阅读 · 0 评论 -
图的实现、无向图的最小生成树、有向图的最短路径
graph.h#ifndef __GRAPH__#define __GRAPH__#include #include using namespace std;class DisjointSet;template class Graph {public: virtual bool insert(int u, int v, TypeOfEdge weight) = 0;原创 2016-05-06 23:08:59 · 4253 阅读 · 1 评论 -
STL map的遍历与pair的使用
#include #include using namespace std;int main(){ //map的遍历 unordered_map a; a[1] = 4; a[3] = 5; a[2] = 6; for (unordered_map::iterator it = a.begin(); it != a.end(); ++it){ cout fi原创 2016-04-24 17:26:19 · 2517 阅读 · 1 评论 -
C++ STL 排序函数
//快速排序//qsort在中//函数原型 void qsort(void * _Base, int _NumOfElements, int _SizeOfElements, int (* _PtFuncCompare)(const void *, const void *));#include #include using namespace std;int compare(c原创 2016-03-31 16:19:30 · 2123 阅读 · 0 评论 -
最近共同祖先
#include #include using namespace std;// 参考 http://blog.csdn.net/cxllyg/article/details/7635992// 参考 《剑指offer》P256struct node{ int data; node *left; node *right; node(const int &x, nod原创 2016-10-10 15:24:09 · 624 阅读 · 0 评论 -
快速排序
未测试边界条件#include using namespace std;#define METOHD 0void swap(int &a, int &b){ int tmp = a; a = b; b = tmp;}int partion(int a[], int left, int right){#if METOHD int tmp = a[le原创 2016-03-30 14:27:01 · 196 阅读 · 0 评论 -
二叉查找树转化为双向链表
#include #include using namespace std;struct node{ int data; node *left; node *right; node *prev; node *next; node(const int &x, node *l = NULL, node *r = NULL, node *p = NULL, node原创 2016-04-01 13:50:18 · 197 阅读 · 0 评论 -
打印1到最大的n位数
#include #include using namespace std;// 参考《剑指offer》P94bool increment_number(char *number){ int take_over = 0; int len = strlen(number); for (int i = len - 1; i >= 0; --i){ int sum = n原创 2016-10-11 14:44:39 · 303 阅读 · 0 评论 -
背包问题
#include using namespace std;// 参考 http://baike.baidu.com/link?url=kOn5J1ionfV40_l4SagPK3aO6vu6TAB4r9iPociRXEm6R0Er8JEb4wpE2_53GJe8vPQ-KLb8-Au6r3N4K8h6PDpxqMhyEkxWxZoFWcqlqfZhZJCjuFTVixTVbZx1Fzu7原创 2016-10-12 12:38:30 · 336 阅读 · 0 评论 -
二叉树的非递归遍历
参考http://www.cnblogs.com/dolphin0520/archive/2011/08/25/2153720.html 的5楼回复#include #include using namespace std;struct Node { int data; Node *left; Node *right; Node(int d, Node *l转载 2016-05-13 23:30:05 · 270 阅读 · 0 评论 -
归并排序链表
时间复杂度O(N*logN),空间复杂度O(1)参考 http://www.cnblogs.com/TenosDoIt/p/3666585.html#include using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL)原创 2016-04-09 17:17:32 · 332 阅读 · 0 评论 -
反转链表
未测试边界条件#include using namespace std;struct node{ int data; node *next; node(int m_data, node *m_next = NULL){ data = m_data; next = m_next; }};node *reverse_list_circularly(n原创 2016-03-30 20:22:16 · 305 阅读 · 0 评论 -
一棵二叉树是否为另一棵二叉树的子树
未测试边界条件#include using namespace std;struct node{ int data; node *left; node *right; node(const int &x, node *l = NULL, node *r = NULL){ data = x; left = l; right = r; }原创 2016-03-31 11:32:07 · 281 阅读 · 0 评论 -
用两个队列实现栈
未测试边界条件MyStack.h#ifndef __MYSTACK__#define __MYSTACK__#pragma once//若出现链接错误无法解析的外部符号 "public: int __thiscall MyStack::pop(void)" (?pop@?$MyStack@H@@QAEHXZ),该符号在函数 _main 中被引用//请将MyStack.cpp的代码移入原创 2016-03-30 11:54:13 · 282 阅读 · 0 评论 -
归并排序
未测试边界条件#include using namespace std;void merge(int a[], int left, int mid, int right){ int *tmp = new int[right - left + 1]; int i = left, j = mid + 1, k = 0; while(i <= mid && j <= right){ i原创 2016-03-30 14:27:55 · 241 阅读 · 0 评论 -
全排列问题、八皇后问题、组合问题的递归解法
全排列问题#include using namespace std;void swap(char *a, char *b){ char tmp = *a; *a = *b; *b = tmp;}void find_core(char* col, char* begin){ if (*begin == '\0'){ cout << col << endl;原创 2016-04-01 19:49:44 · 1054 阅读 · 0 评论 -
打印回形矩阵
打印一个回形矩阵如下1 2 3 412 13 14 511 16 15 610 9 8 7#include //当n=4时,分别打印1-3, 4-6, 7-9, 10-12, 每次打印(n-1)个数,第一圈打完后打第二圈//n为奇数时,可刚好打完;n为偶数时剩最后一个数int a[10][10]; //最大支持边长10的矩阵,其他情况可用动态原创 2016-04-04 10:41:46 · 2750 阅读 · 0 评论 -
堆排序
这里基于循环实现,还可以基于递归实现,递归实现可参考《算法导论》#include using namespace std;void percolate_down(int *a, int hole, int size){ int tmp = a[hole]; int child; while (hole * 2 + 1 < size){ child = ho原创 2016-04-09 15:38:27 · 276 阅读 · 0 评论 -
数字字符串是否为合法IP地址
以下代码亟待改进#include #include using namespace std;bool check_range(string s){ int i = stoi(s); if (i >= 0 && i <= 255) return true; return false;}void find_valid(string s, string re原创 2016-04-09 18:08:05 · 547 阅读 · 0 评论 -
快速排序链表
时间复杂度O(N*logN),空间复杂度O(1)参考 http://www.cnblogs.com/TenosDoIt/p/3666585.html//依据快速排序METOHD_3的思想//取第一个元素作为枢纽元//链表范围是[low, high)#include using namespace std;struct ListNode { int val;原创 2016-04-10 11:49:21 · 247 阅读 · 0 评论 -
循环有序数组/旋转数组的二分查找
转载于 http://blog.sina.com.cn/s/blog_a2498b5b01014bsg.html类似问题:循环有序数组/旋转数组的最小值 《剑指offer》P66#include using namespace std;int binarySearch(int a[],int low,int high,int value) //二分查找{ if(low转载 2016-10-18 21:44:42 · 732 阅读 · 0 评论