自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(45)
  • 资源 (4)
  • 收藏
  • 关注

原创 Z栈的压入,弹出序列

#include using 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 310

原创 带min函数的栈

#include #include using namespace std; template class StackWithMin { private: stack m_data; stack m_min; public: void pop(); void push(const T& value); const T& min()const; }; template void Sta

2016-04-05 16:13:49 265

原创 二叉树的镜像

#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 268

原创 寻找子树

#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 377

原创 链表中倒数第k个节点

为了实现只遍历一次链表的目标,使用两个指针,第一个指针从链表头开始向前遍历走k-1步,第二个指针保持不动,从第k步开始,两个指针一起走,两个指针之间的距离保持在k-1,当第一个指针到达链表的尾部时,第二个指针正好在链表的倒数第k个节点位置。 #include using namespace std; struct ListNode { int m_nKey; ListNode *m_pNe

2016-03-13 22:08:07 267

原创 调整数组顺序使奇数在前

用一个单独的函数来判断数字是否符合标准,利用函数指针进行操作。 #include using 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 235

原创 在O(1)时间删除链表节点

将下一个节点的内容复制到要删除的那个节点上覆盖原来的内容,再把下一个节点删除,就相当于将当前要删除的节点删除了。 #include using namespace std; struct ListNode { int m_nValue; ListNode* m_pNext; }; void DeleteNode(ListNode** pListHead, ListNode* pToBeD

2016-03-12 19:38:59 250

原创 打印1到最大的n位数

考虑大数存储,最方便的是递归打印 #include using 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 181

原创 数值的整数次方

考虑到指数是负数的情况,考虑到底数为0的情况 #include using 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 165

原创 二进制中1的个数

可以通过移位实现,不过有更简单的方法。将一个整数减去1之后,最右边的1变为0,其后面的0都变成了1,如果将结果与原来的整数相与,会把原整数最后一个1变为0,这样有多少个1只需要进行几次这样的操作。 #include using namespace std; int NumberOf1(int n) { int count = 0; while (n) { ++count; n =

2016-03-12 11:03:22 183

原创 旋转数组的最小数字

用二分法可以实现O(logn)的效率。用两个指针分别指向数组第一个元素和最后一个元素,第一个元素应该是大于等于最后一个元素的。找到数组中间的元素,如果该中间元素位于前面的递增子数组,那么它应该大于等于第一个元素,最小元素应该位于中间元素的后面,可以把第一个指针指向该中间元素,这样可以缩小寻找范围。如果中间元素位于后面的递增子数组,那么它应该小于最后一个元素,此时最小元素应该位于中间元素的前面,此时

2016-03-11 22:24:53 230

原创 快速排序小规模数据

#include #include #include using 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 390

原创 快速排序的非递归写法

#include #include using 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 384

原创 快速排序递归算法

#include using 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 257

原创 两个队列实现栈

#include #include using namespace std; template class CStack { public: void push(const T& element); T pop(); private: queue queue1; queue queue2; }; template void CStack::push(const T& element)

2016-03-09 21:20:28 216

原创 两个栈实现队列

插入元素时压入stack1,删除元素时将stack1中的元素逐个弹出并压入stack2,元素在stack2中的顺序正好和原来在stack1中相反。stack2中的栈顶元素是最先进入的元素。 #include #include using namespace std; template class CQueue { public: void appendTail(const T& node);

2016-03-09 20:53:23 205

原创 重建二叉树

现根据前序遍历序列中的第一个数字创建根节点,接下来在中序遍历中找到根节点的位置,这样就能确定左右子树节点的数量。在前序遍历和中序遍历的序列中划分了左右子树节点的值之后,就可以递归地进行。 #include using namespace std; struct BinaryTreeNode { int m_nValue; BinaryTreeNode* m_pLeft; BinaryTr

2016-03-09 20:01:54 208

原创 从尾到头打印链表

典型的后进先出,可以用栈实现这种顺序,每经过一个节点的时候,把该节点放到栈中,当遍历完整个链表后,再从栈顶开始逐个输出节点的值。 #include #include using namespace std; struct ListNode { int m_nKey; ListNode *m_pNext; }; void AddToTail(ListNode** pHead,int val

2016-03-07 23:34:37 200

原创 替换空格

从字符串的后面开始复制和替换,首先准备两个指针,P1和P2,P1指向原始字符串的末尾,而P2指向替换后的字符串的末尾,向前移动指针P1,逐个把它指向的字符复制到P2指向的位置,知道碰到第一个空格为止,碰到第一个空格之后,把P1向前移动1格,在P2之前插入字符串“%20”,再接着向前复制。 #include using namespace std; void ReplaceBlank(char

2016-03-05 23:50:26 199

原创 二维数组中的查找

每一次选取数组查找范围内右上角的那个数,如果该数字等于要查找的数,查找过程结束;如果该数字大于要查找的数,那么该列所有数都大于要查找的数字,剔除这个数字所在的列;如果该数字小于要查找的数字,那么该行所有数都小于要查找的数字,剔除这个数字所在的行。这样每一步都可以缩小查找范围,直到找到要查找的数字,或者查找范围为空。 #include using namespace std; bool Find

2016-03-05 16:42:01 237

原创 适配器模式

适配器模式将一个类接口,转换成客户期望的另一个接口,让原本接口不兼容的类可以合作无间。 #ifndef __ADAPTER_H #define __ADAPTER_H #include using namespace std; class Duck { public: virtual void quack() = 0; virtual void fly() = 0; }; class M

2016-02-21 21:31:48 230

原创 单件模式

有一些对象我们只需要一个,比如线程池、缓存、对话框等等,如果制造出多个实例,就会导致许多问题产生。 单件模式利用一个静态变量来记录Singleton类的唯一实例;把构造器声明为私有的,只有自Singleton类内部才可以调用构造器;用getInstance方法实例化对象,并返回这个实例。单件模式确保只有一个实例,并提供一个全局访问点。 #ifndef __SINGLETON_H #defi

2016-02-19 22:05:11 362

原创 工厂方法模式

工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个,工厂方法让类把实例化推迟到子类。 #ifndef __FACTORY_H #define __FACTORY_H #include #include #include using namespace std; class Pizza { private: string name; string dough; st

2016-02-19 16:42:07 384

原创 装饰者模式

利用继承设计子类行为,是在编译时静态决定的,而且所有的子类都会继承到相同的行为,而如果能利用组合的做法扩展对象的行为,就可以在运行时动态地进行扩展。通过动态地组合对象,可以写新的代码添加新功能,而无须修改现有的代码,这样可以使得引入bug或产生意外副作用的机会将大幅减少。 设计原则:类应该对扩展开放,对修改关闭 1.装饰者和被装饰对象有相同的超类型 2.可以用一个或多个装饰者包装一个对象

2016-02-19 13:53:32 247

原创 观察者模式

观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。观察者依赖于此主题,只要主题状态一有变化,观察者就会被通知,根据通知的风格,观察者可能因此新值而更新。关于观察者的一切,主题只知道观察者实现了某个接口,主题不需要知道观察者的具体类是谁、做了些什么工作或其他任何细节。任何时候我们都可以增加新的观察者,因为主题唯一依赖的东西是一个实现Obser

2016-02-18 20:14:48 223

原创 策略模式

针对接口编程,而不是针对实现编程。找出应用中可能需要变化的地方,把它们独立出来,不要和那些需要变化的代码混在一起(把会变化的部分取出并“封装”起来,好让其它部分不受影响)。 在下面的实例中,将fly和quack方法用独立的接口实现,比如FlyBehavior类和QauckBehavior类,所以Duck类不再负责实现Flying和Quacking接口,反而由我们制造一组其它类专门实现FlyBeh

2016-01-28 10:44:01 284

原创 SLT之算法实现

#ifndef _ALGOBASE_H #define _ALGOBASE_H #include"heap.h" template inline OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result) { return __copy_dispatch()(first, last, re

2016-01-06 17:47:52 1528

原创 STL之numeric实现

#ifndef __NUMERIC_H #define __NUMERIC_H #include"iterator_traits.h" template T accumulate(InputIterator first, InputIterator last, T init) { for (; first != last; ++first) init += *first; return i

2016-01-06 17:46:52 487

原创 STL之仿函数,配接器实现

#ifndef __FUNCTIONAL_H #define __FUNCTIONAL_H template struct unary_function { typedef Arg argument_type; typedef Result result_type; }; template struct binary_function { typedef Arg1 first_argum

2016-01-06 17:45:42 365

原创 STL之迭代器实现

#ifndef __ITERATOR_H #define __ITERATOR_H #include"iterator_traits.h" #include template class back_insert_iterator { protected: Container* container; public: typedef output_iterator_tag_h iterator_c

2016-01-06 17:44:38 272

原创 STL之智能指针

#ifndef __MEMORY_H #define __MEMORY_H #include"Allocator.h" #include"construct.h" #include"uninitialized.h" template class __auto_ptr { private: _Tp *_M_ptr; public: typedef _Tp element_type; expl

2016-01-06 17:43:23 250

原创 STL之hashset实现

#ifndef __HASHSET_H #define __HASHSET_H #include"hash_fun.h" #include"hashtable.h" #include template,class EqualKey=std::equal_to,class Alloc=alloc> class hash_set { private: typedef hashtable, Equa

2015-10-24 11:48:52 465

原创 STL之hashtable实现

#ifndef __HASH_FUN_H #define __HASH_FUN_H template struct __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 351

原创 STL之map实现

#ifndef __MAP_H #define __MAP_H #include"rbt.h" #include"pair.h" #include template,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 237

原创 STL之set实现

#ifndef __SET_H #define __SET_H #include"rbt.h" #include template,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 269

原创 STL之红黑树实现

#include"Allocator.h" #include"iterator_traits.h" #include typedef 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 363

原创 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 234

原创 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 326

原创 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 551

原创 STL之heap实现

#ifndef __HEAP_H #define _HEAP_H #include"iterator_traits.h" template inline void mypush_heap(RandomAccessIterator first, RandomAccessIterator last) { __push_heap_aux(first, last, distance_type(first

2015-10-11 23:22:59 277

Kmeans的c++程序

用c++编写的Kmeans算法,对图像像素进行聚类,使用opencv读取和显示图像,程序能清楚Kmeans的原理

2015-07-01

SVM分类程序

SVM分类程序,matlab实现,没有使用工具箱,能够了解SVM分类的步骤,有测试数据

2015-06-17

手写数字识别,神经网络

神经网络实现手写数字识别,带数字图片,用MATLAB实现

2015-06-12

enc文件wavelet

enc文件wavelet

2013-01-30

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除