笨狗的算法备忘录
文章平均质量分 82
bagainu
这个作者很懒,什么都没留下…
展开
-
二叉树相关算法
1. 二叉树最低公共父节点 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, Tree原创 2015-09-13 20:03:52 · 336 阅读 · 0 评论 -
字符串相关算法
1. 最大上升子序列 template vector longestIncreasingSubsequence(vector &vec) { vector res; //res中只保存递增数列 if (vec.empty()) return res; for (auto iter = vec.begin(); iter != vec.end(); ++iter) { if (res.原创 2015-09-10 16:39:41 · 325 阅读 · 0 评论 -
约瑟夫环
1. 循环 #include using namespace std; int joseph(const int n, const int m) { int* arr = new int[n]; for (int i = 0; i < n; ++i) arr[i] = 1; int i = 0; int j = 0; int count = n; while (count > 1原创 2015-09-08 21:42:53 · 349 阅读 · 0 评论 -
C++中的多态
Brief 定义:同样的消息被不同类型的对象接收时产生不同的行为。 原理:1.编译时多态(静态绑定) 2.运行时多态(动态绑定) 分类:1.重载:包括函数重载,运算符重载 (静态绑定) 2.覆盖:包括子类和父类间虚函数、虚析构函数和纯虚函数 (动态绑定) 1. 重载 要求:1.函数名相同;2.参数不同 栗子: #include原创 2015-08-03 17:36:28 · 304 阅读 · 0 评论 -
优先级队列和堆的应用
优先级队列可以通过数组自己维护优先级,也可以用STL自带的优先级队列priority_queue #include 大根堆:priority_queue que 或 priority_queue, less > que 小根堆:priority_queue, greater > que 关于优先级队列的应用: 1. 取中位数 #include #include #incl原创 2015-07-16 17:01:32 · 372 阅读 · 0 评论 -
二叉树三种遍历方式
#include #include #include #include #include using namespace std; #define ITE struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int v) : val(v), left(NULL), right(NULL) {原创 2015-05-04 21:17:35 · 560 阅读 · 2 评论 -
映射法数据深度复制
问题一: Copy List with Random Pointe A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the lis原创 2015-04-29 16:58:36 · 359 阅读 · 0 评论 -
Longest Palindromic Substring Part II
http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html Note: This is Part II of the article: Longest Palindromic Substring. Here, we describe an algorithm (Manacher’s algorit转载 2015-01-13 13:37:52 · 371 阅读 · 0 评论 -
各类排序算法
1. 快速排序 #include #include using namespace std; // 把数组分为两部分,轴temp左边的部分都小于轴右边的部分 int partition(int* nums, int low, int high) { int temp = nums[low]; //任选元素作为轴,这里选首元素 while (low < high) { while原创 2015-08-26 22:41:21 · 357 阅读 · 0 评论