- 博客(45)
- 资源 (4)
- 收藏
- 关注
原创 Z栈的压入,弹出序列
#includeusing namespace std;bool IsPopOrder(const int* pPush,const int* pPop,int nLength){ bool bPossible=false; if(pPush!=NULL&&pPop!=NULL&&nLength>0) { const int* pNextPush=pPush; const
2016-04-05 21:20:52
344
原创 带min函数的栈
#include#includeusing namespace std;templateclass StackWithMin{private: stack m_data; stack m_min;public: void pop(); void push(const T& value); const T& min()const;};templatevoid Sta
2016-04-05 16:13:49
298
原创 二叉树的镜像
#include"BinaryTree.h"void MirrorRecursively(BinaryTreeNode* pNode){ if(pNode==NULL||pNode->m_pLeft==NULL&&pNode->m_pRight==NULL) return; BinaryTreeNode *pTemp=pNode->m_pLeft; pNode->m_pLeft=p
2016-04-05 15:20:46
306
原创 寻找子树
#include"BinaryTree.h"bool DoesTree1HasTree2(BinaryTreeNode* pRoot1,BinaryTreeNode* pRoot2){ if(pRoot2==NULL) return true; if(pRoot1==NULL) return false; if(pRoot1->m_nValue!=pRoot2->m_nValu
2016-04-05 11:39:21
411
原创 链表中倒数第k个节点
为了实现只遍历一次链表的目标,使用两个指针,第一个指针从链表头开始向前遍历走k-1步,第二个指针保持不动,从第k步开始,两个指针一起走,两个指针之间的距离保持在k-1,当第一个指针到达链表的尾部时,第二个指针正好在链表的倒数第k个节点位置。#includeusing namespace std;struct ListNode{ int m_nKey; ListNode *m_pNe
2016-03-13 22:08:07
299
原创 调整数组顺序使奇数在前
用一个单独的函数来判断数字是否符合标准,利用函数指针进行操作。#includeusing namespace std;void Reorder(int *pData, unsigned int length, bool(*func)(int)){ if (pData == NULL || length == 0) return; int *pBegin = pData; in
2016-03-13 21:39:40
261
原创 在O(1)时间删除链表节点
将下一个节点的内容复制到要删除的那个节点上覆盖原来的内容,再把下一个节点删除,就相当于将当前要删除的节点删除了。#includeusing namespace std;struct ListNode{ int m_nValue; ListNode* m_pNext;};void DeleteNode(ListNode** pListHead, ListNode* pToBeD
2016-03-12 19:38:59
277
原创 打印1到最大的n位数
考虑大数存储,最方便的是递归打印#includeusing namespace std;void PrintNumber(char *number){ bool isBeginning0 = true; int nLength = strlen(number); for (int i = 0; i < nLength; i++) { if (isBeginning0&&nu
2016-03-12 17:09:11
207
原创 数值的整数次方
考虑到指数是负数的情况,考虑到底数为0的情况#includeusing namespace std;bool g_InvalidInput = false;bool equal(double num1, double num2){ if (((num1 - num2) > -0.0000001) && ((num1 - num2) < 0.00000001)) return
2016-03-12 11:59:56
190
原创 二进制中1的个数
可以通过移位实现,不过有更简单的方法。将一个整数减去1之后,最右边的1变为0,其后面的0都变成了1,如果将结果与原来的整数相与,会把原整数最后一个1变为0,这样有多少个1只需要进行几次这样的操作。#includeusing namespace std;int NumberOf1(int n){ int count = 0; while (n) { ++count; n =
2016-03-12 11:03:22
204
原创 旋转数组的最小数字
用二分法可以实现O(logn)的效率。用两个指针分别指向数组第一个元素和最后一个元素,第一个元素应该是大于等于最后一个元素的。找到数组中间的元素,如果该中间元素位于前面的递增子数组,那么它应该大于等于第一个元素,最小元素应该位于中间元素的后面,可以把第一个指针指向该中间元素,这样可以缩小寻找范围。如果中间元素位于后面的递增子数组,那么它应该小于最后一个元素,此时最小元素应该位于中间元素的前面,此时
2016-03-11 22:24:53
253
原创 快速排序小规模数据
#include#include#includeusing namespace std;static int CutOff = 10;void Swap(int *x, int * y){ int tmp = *x; *x = *y; *y = tmp;}int Median(int *a, int left, int right){ int center = (l
2016-03-11 17:52:15
430
原创 快速排序的非递归写法
#include#includeusing namespace std;struct node{ int left; int right;};void Swap(int *x, int *y){ int tmp = *x; *x = *y; *y = tmp;}int Median(int *a, int left, int right){ int cente
2016-03-11 16:59:58
415
原创 快速排序递归算法
#includeusing namespace std;void Swap(int* x, int *y){ int tmp = *x; *x = *y; *y = tmp;}int Median(int* a, int left, int right){ int center = (left + right) / 2; if (a[left] > a[center])
2016-03-11 16:06:00
291
原创 两个队列实现栈
#include#includeusing namespace std;templateclass CStack{public: void push(const T& element); T pop();private: queue queue1; queue queue2;};templatevoid CStack::push(const T& element)
2016-03-09 21:20:28
243
原创 两个栈实现队列
插入元素时压入stack1,删除元素时将stack1中的元素逐个弹出并压入stack2,元素在stack2中的顺序正好和原来在stack1中相反。stack2中的栈顶元素是最先进入的元素。#include#includeusing namespace std;templateclass CQueue{public: void appendTail(const T& node);
2016-03-09 20:53:23
228
原创 重建二叉树
现根据前序遍历序列中的第一个数字创建根节点,接下来在中序遍历中找到根节点的位置,这样就能确定左右子树节点的数量。在前序遍历和中序遍历的序列中划分了左右子树节点的值之后,就可以递归地进行。#includeusing namespace std;struct BinaryTreeNode{ int m_nValue; BinaryTreeNode* m_pLeft; BinaryTr
2016-03-09 20:01:54
235
原创 从尾到头打印链表
典型的后进先出,可以用栈实现这种顺序,每经过一个节点的时候,把该节点放到栈中,当遍历完整个链表后,再从栈顶开始逐个输出节点的值。#include#includeusing namespace std;struct ListNode{ int m_nKey; ListNode *m_pNext;};void AddToTail(ListNode** pHead,int val
2016-03-07 23:34:37
227
原创 替换空格
从字符串的后面开始复制和替换,首先准备两个指针,P1和P2,P1指向原始字符串的末尾,而P2指向替换后的字符串的末尾,向前移动指针P1,逐个把它指向的字符复制到P2指向的位置,知道碰到第一个空格为止,碰到第一个空格之后,把P1向前移动1格,在P2之前插入字符串“%20”,再接着向前复制。#includeusing namespace std;void ReplaceBlank(char
2016-03-05 23:50:26
242
原创 二维数组中的查找
每一次选取数组查找范围内右上角的那个数,如果该数字等于要查找的数,查找过程结束;如果该数字大于要查找的数,那么该列所有数都大于要查找的数字,剔除这个数字所在的列;如果该数字小于要查找的数字,那么该行所有数都小于要查找的数字,剔除这个数字所在的行。这样每一步都可以缩小查找范围,直到找到要查找的数字,或者查找范围为空。#includeusing namespace std;bool Find
2016-03-05 16:42:01
340
原创 适配器模式
适配器模式将一个类接口,转换成客户期望的另一个接口,让原本接口不兼容的类可以合作无间。#ifndef __ADAPTER_H#define __ADAPTER_H#includeusing namespace std;class Duck{public: virtual void quack() = 0; virtual void fly() = 0;};class M
2016-02-21 21:31:48
261
原创 单件模式
有一些对象我们只需要一个,比如线程池、缓存、对话框等等,如果制造出多个实例,就会导致许多问题产生。单件模式利用一个静态变量来记录Singleton类的唯一实例;把构造器声明为私有的,只有自Singleton类内部才可以调用构造器;用getInstance方法实例化对象,并返回这个实例。单件模式确保只有一个实例,并提供一个全局访问点。#ifndef __SINGLETON_H#defi
2016-02-19 22:05:11
386
原创 工厂方法模式
工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个,工厂方法让类把实例化推迟到子类。#ifndef __FACTORY_H#define __FACTORY_H#include#include#includeusing namespace std;class Pizza{private: string name; string dough; st
2016-02-19 16:42:07
411
原创 装饰者模式
利用继承设计子类行为,是在编译时静态决定的,而且所有的子类都会继承到相同的行为,而如果能利用组合的做法扩展对象的行为,就可以在运行时动态地进行扩展。通过动态地组合对象,可以写新的代码添加新功能,而无须修改现有的代码,这样可以使得引入bug或产生意外副作用的机会将大幅减少。设计原则:类应该对扩展开放,对修改关闭1.装饰者和被装饰对象有相同的超类型2.可以用一个或多个装饰者包装一个对象
2016-02-19 13:53:32
275
原创 观察者模式
观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。观察者依赖于此主题,只要主题状态一有变化,观察者就会被通知,根据通知的风格,观察者可能因此新值而更新。关于观察者的一切,主题只知道观察者实现了某个接口,主题不需要知道观察者的具体类是谁、做了些什么工作或其他任何细节。任何时候我们都可以增加新的观察者,因为主题唯一依赖的东西是一个实现Obser
2016-02-18 20:14:48
252
原创 策略模式
针对接口编程,而不是针对实现编程。找出应用中可能需要变化的地方,把它们独立出来,不要和那些需要变化的代码混在一起(把会变化的部分取出并“封装”起来,好让其它部分不受影响)。在下面的实例中,将fly和quack方法用独立的接口实现,比如FlyBehavior类和QauckBehavior类,所以Duck类不再负责实现Flying和Quacking接口,反而由我们制造一组其它类专门实现FlyBeh
2016-01-28 10:44:01
308
原创 SLT之算法实现
#ifndef _ALGOBASE_H#define _ALGOBASE_H#include"heap.h"templateinline OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result){ return __copy_dispatch()(first, last, re
2016-01-06 17:47:52
1588
原创 STL之numeric实现
#ifndef __NUMERIC_H#define __NUMERIC_H#include"iterator_traits.h"templateT accumulate(InputIterator first, InputIterator last, T init){ for (; first != last; ++first) init += *first; return i
2016-01-06 17:46:52
524
原创 STL之仿函数,配接器实现
#ifndef __FUNCTIONAL_H#define __FUNCTIONAL_Htemplatestruct unary_function{ typedef Arg argument_type; typedef Result result_type;};templatestruct binary_function{ typedef Arg1 first_argum
2016-01-06 17:45:42
387
原创 STL之迭代器实现
#ifndef __ITERATOR_H#define __ITERATOR_H#include"iterator_traits.h"#includetemplateclass back_insert_iterator{protected: Container* container;public: typedef output_iterator_tag_h iterator_c
2016-01-06 17:44:38
307
原创 STL之智能指针
#ifndef __MEMORY_H#define __MEMORY_H#include"Allocator.h"#include"construct.h"#include"uninitialized.h"templateclass __auto_ptr{private: _Tp *_M_ptr;public: typedef _Tp element_type; expl
2016-01-06 17:43:23
273
原创 STL之hashset实现
#ifndef __HASHSET_H#define __HASHSET_H#include"hash_fun.h"#include"hashtable.h"#includetemplate,class EqualKey=std::equal_to,class Alloc=alloc>class hash_set{private: typedef hashtable, Equa
2015-10-24 11:48:52
507
原创 STL之hashtable实现
#ifndef __HASH_FUN_H#define __HASH_FUN_Htemplatestruct __hash{};inline size_t __stl_hash_string(const char* s){ unsigned long h = 0; for (; *s; ++s) h = 5 * h + *s; return size_t(h);}te
2015-10-18 22:52:45
389
原创 STL之map实现
#ifndef __MAP_H#define __MAP_H#include"rbt.h"#include"pair.h"#includetemplate,class Alloc=alloc>class map{public: typedef Key key_type; typedef T data_type; typedef T mapped_type; typedef
2015-10-17 15:55:12
265
原创 STL之set实现
#ifndef __SET_H#define __SET_H#include"rbt.h"#includetemplate,class Alloc=alloc>class set{public: typedef Key key_type; typedef Key value_type; typedef Compare key_compare; typedef Compare
2015-10-17 08:52:25
307
原创 STL之红黑树实现
#include"Allocator.h"#include"iterator_traits.h"#includetypedef bool __rb_tree_color_type;const __rb_tree_color_type __rb_tree_red = false;const __rb_tree_color_type __rb_tree_black = true;stru
2015-10-11 23:35:27
409
原创 STL之priority_queue实现
#ifndef __PRIORITY_QUEUE_H#define __PRIORITY_QUEUE_H#include#include"heap.h"template, class Compare = less>class priority_queue{public: typedef typename Sequence::value_type value_type; typed
2015-10-11 23:34:26
264
原创 STL之queue实现
template>class queue{ friend bool operator==(const queue&, const queue&); friend bool operator<(const queue&, const queue&);public: typedef typename Sequence::value_type value_type; typedef typ
2015-10-11 23:25:17
354
原创 STL之statck实现
template>class stack{ friend bool operator==(const stack&, const stack&); friend bool operator<(const stack&, const stack&);public: typedef typename Sequence::value_type value_type; typedef typ
2015-10-11 23:24:10
606
原创 STL之heap实现
#ifndef __HEAP_H#define _HEAP_H#include"iterator_traits.h"templateinline void mypush_heap(RandomAccessIterator first, RandomAccessIterator last){ __push_heap_aux(first, last, distance_type(first
2015-10-11 23:22:59
310
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人