- 博客(52)
- 收藏
- 关注
原创 括号匹配判断
#include #include #include using namespace std;bool BrMa(const string& s){ bool res=true; stack BrStack; for(int i=0;i<s.length();++i){ if(s[i]=='('){ BrStack.push(s[i]); }//if else
2015-01-22 10:48:44 356
原创 十进制数转换为二进制数
#include #include using namespace std;void DTB(int num){ if(num<=0) return; stack numstack; while(num>0){ numstack.push(num%2); num/=2; } while(!numstack.empty()){ cout<<(int)numstack.t
2015-01-22 10:31:25 525
原创 vector及其迭代器
#include #include #include //srand() rand()#include //time(NULL)#include //accumulateusing namespace std;int main(){ //define instance and iterator vector grade; vector::iterator iter; //
2015-01-15 15:29:40 351
原创 类的声明及定义
employee1.h文件#ifndef EMPLOYEE#define EMPLOYEE#include using namespace std;class Employee{ public: Employee(); void readInfo(); bool isSentinel() const; void printOut() const; void get
2015-01-09 14:09:56 410
原创 冒泡排序 快速排序 选择排序 堆排序 直接插入排序 希尔排序 归并排序
#include using namespace std;void Swap(int* a,int* b){ int tmp=*a; *a=*b; *b=tmp;}void BubbleSort(int* num,int len){ if(!num||len<=0) return; bool flag=true; for(int i=0;i<len && flag;++i){
2015-01-07 23:19:50 494
原创 二叉排序树
#include using namespace std;struct BiTreeNode{ int data; BiTreeNode* left; BiTreeNode* right; BiTreeNode* parent;};void InOrderTraverse(BiTreeNode* Root){ if(Root==NULL) return; InOrderTr
2015-01-07 10:57:38 355
转载 二叉排序树的构造、查找、插入及删除算法的实现(C++)
二叉排序树的构造、查找、插入及删除算法的实现(C++)转自这里#include using namespace std;class BsTree {public: // 构造函数初始化树根为NULL,表示空树 BsTree (void) : m_pRoot (NULL), m_uSize (0) {} // 析构函数清空树 ~BsTree (void) { Clear
2015-01-06 13:39:15 3075
原创 折半查找
#include using namespace std;int BiSearch(int* num,int len,int key){ if(!num || len<=0) return -1; int start=0,end=len-1; int mid; while(start<=end){ //折半查找|二分查找 //mid=start+(end-start)/2;
2015-01-06 12:37:52 290
原创 串的匹配算法
朴素算法#include #include using namespace std;int FindSubString(char* s,char* t){ int sflag=0,tflag=0; int slen=strlen(s); int tlen=strlen(t); while(sflag<slen&&t[tflag]!='\0'){ if(s[sflag]==t[
2015-01-04 13:40:43 313
原创 链栈的实现
#include using namespace std;#define error 0#define ok 1typedef int Status;typedef int Elemtype;struct Node{ Elemtype data; Node* next;};struct LinkStack{ Node* top; int count;};Statu
2015-01-04 10:40:14 290
原创 顺序栈的实现
#include using namespace std;#define MaxSize 20#define Error 0#define Ok 1typedef int Elemtype;typedef int Status;struct SqStack{ Elemtype data[MaxSize]; int top;}; Status InitStack(SqSta
2015-01-03 17:04:49 281
翻译 剑指offer 面试题44 扑克牌顺子
#include #include int compare(const void* a,const void* b){ return *(int*)a-*(int*)b; }bool IsContinuous(int* num,int len){ if(!num||len<=0) return false; qsort(num,len,sizeof(int),compare); i
2014-12-30 21:30:41 348
翻译 剑指offer 面试题42 翻转单词顺序 | 左旋转字符串
#include #include using namespace std;void Reverse(char* str,int start,int end){ if(!str) return; for(int i=start,j=end;i<j;++i,--j){ char tmp=str[i]; str[i]=str[j]; str[j]=tmp; }}char*
2014-12-30 11:59:14 373
翻译 剑指offer 面试题41 递增数组中和为s的两个数字 | 和为s的连续整数序列
#include using namespace std;void TwoNum(int* num,int len,int s){ if(!num||len<2) return; int *small=num,*large=num+len-1; while(small<large){ if(*small+*large==s){ cout<<*small<<' '<<*lar
2014-12-30 10:27:34 464
翻译 剑指offer 面试题43 n个骰子点数和的分布律
#include using namespace std;//get base^expint Power(int base,int exp){ int result=1; while(exp>0){ result*=base; --exp; } return result;}//recursion all the combinition and calcu sumvoi
2014-12-29 23:39:20 410
翻译 剑指offer 面试题40 数组中出现一次的两个数
#include using namespace std;bool InvalidInput=false;void FindSingleNum(int* num,int len,int* single1,int* single2){ InvalidInput=false; if(!num||len<2||!single1||!single2){ InvalidInput=true;
2014-12-29 21:01:12 311
翻译 剑指offer 面试题39 求二叉树深度|判断是否为平衡二叉树
struct BinaryTreeNode{ int data; BinaryTreeNode* left; BinaryTreeNode* right;};//get depth of a binary treeint Depth(BinaryTreeNode* pRoot){ if(!pRoot) return 0; if(Depth(pRoot->left)>Depth(pR
2014-12-29 15:35:21 355
翻译 剑指offer 面试题38 排序数组中数字出现的次数
#include using namespace std;int GetFirstK(int* num,int len,int start,int end,int k){ //no k exist in num if(start>end) return -1; //modify start/end or just return the result int mid=(start+end
2014-12-29 14:47:44 278
翻译 剑指offer 面试题37 两个链表的第一个公共结点
struct ListNode{ int data; ListNode* next;};ListNode* FirstShareNode(ListNode* pHead1,ListNode* pHead2){ int len1=0,len2=0; ListNode* pNode1=pHead1; ListNode* pNode2=pHead2; //get List1 length
2014-12-29 13:31:41 282
翻译 剑指offer 面试题36 数组逆序对个数
归并排序的演进#include using namespace std;//src is seperatly sorted [start,flag] and [flag+1,end]//merge 2 parts into 1 sorted array resint Merge(int* src,int* res,int start,int end,int flag){ int i=
2014-12-29 11:33:19 268
翻译 剑指offer 归并排序
#include using namespace std;//src is seperatly sorted [start,flag] and [flag+1,end]//merge 2 parts into 1 sorted array resvoid Merge(int* src,int* res,int start,int end,int flag){ int i=start,j=
2014-12-29 10:48:39 333
翻译 剑指offer 面试题35 字符串中第一个出现一次的字符
#include #include using namespace std;char FirstNotRepeat(char *str){ if(str==NULL) return '\0'; int hash[256]={0}; //cout<<hash[90]; char res='\0'; for(int i=0;str[i]!='\0';++i){ ++hash[(in
2014-12-27 20:58:28 318
翻译 剑指offer 面试题34 丑数
#include using namespace std;//function to get the min numberint Min(int num1,int num2,int num3){ int min=(num1<num2)?num1:num2; min=(min<num3)?min:num3; return min;}//function to return the i
2014-12-27 16:50:11 282
翻译 剑指offer 面试题33 数组组合的最小数
#include #include #include //#include int compare(const void *num1,const void *num2){ char comb1[21],comb2[21]; strcpy(comb1,*(const char**)num1); strcat(comb1,*(const char**)num2); strcpy(com
2014-12-27 15:04:12 334
翻译 剑指offer 面试题32 计算1~n中1出现的次数
#include //to use function atoi()#include //to use sprintf/printf/scanf#include //to use funtion strlen()//solution1:O(n)*******************************//function to get 10^nint powerbaseten(
2014-12-27 10:50:25 418
翻译 剑指offer 面试题31 求连续子数组的最大和
#include using namespace std;//solution1bool InvalidInput=false;int MaxSumOfSubArray(int *number,int length){ InvalidInput=false; if(number==NULL||length<=0){ InvalidInput=true; return 0; }
2014-12-26 13:38:35 322
翻译 剑指offer 面试题30 找出数组中最小的k个数
#include #include #include using namespace std;//******************************************************void Swap(int *a,int *b){ int tmp=*a; *a=*b; *b=tmp;}int RandomInRange(int start,int en
2014-12-26 12:18:05 368
翻译 剑指offer 面试题29 寻找数组中出现次数超一半的数字
#include #include //use time(NULL) in line8#include //use srand() in line8using namespace std;//*****************************************************//used by partition,get rand number in [star
2014-12-26 10:58:25 339
翻译 剑指offer 面试题28 字符串全排列
#include using namespace std;void Permutation_core(char *str,char *flag){ if(*flag=='\0') cout<<str<<' '; else{ for(int i=0;flag[i]!='\0';++i){ char tmp=*flag; *flag=flag[i]; flag[i]
2014-12-25 13:25:33 356
翻译 剑指offer 面试题27 二叉搜索树转换为排序双向链表
struct BinaryTreeNode{ int data; BinaryTreeNode *left; BinaryTreeNode *right;};void Convert_core(BinaryTreeNode *pNode,BinaryTreeNode **pLastNode){ //handle left subtree if(pNode->left) Conve
2014-12-25 11:30:28 324
翻译 剑指offer 面试题26 克隆复杂链表
struct ComplexListNode{ int data; ComplexListNode *next; ComplexListNode *sibling;};void CloneNode(ComplexListNode *pHead){ ComplexListNode *pNode=pHead; while(pNode){ ComplexListNode *pClone
2014-12-25 10:49:44 306
翻译 剑指offer 面试题25 输出和为某值的路径
struct BinaryTreeNode{ int data; BinaryTreeNode *left; BinaryTreeNode *right;};void FindPath_core(BinaryTreeNode *pNode,int sum,int currentsum,std::vector path){ currentsum+=pNode->data; path.p
2014-12-25 09:35:15 259
翻译 剑指offer 面试题24 判断二叉搜索树后续遍历序列
bool IsBSTSquence(const int *sequence,int len){ bool result=false; if(sequence==NULL||len<=0) return result; const int *flag=sequence; int rootdata=*(sequence+len-1); while(*flag<rootdata)){
2014-12-24 15:53:17 277
翻译 剑指offer 面试题23 按层遍历二叉树
struct BinaryTreeNode{ int data; BinaryTreeNode *left; BinaryTreeNode *right;};void PrintTreeInLayerOrder(BinaryTreeNode *pRoot){ if(pRoot==NULL) return; std::deque TreeNodeDeque; TreeNodeDequ
2014-12-24 15:02:18 277
翻译 剑指offer 面试题22 栈的压入弹出序列
#include #include using namespace std;bool IsPopOrder(const int *pPush,const int *pPop,int len){ bool result=false; if(pPush==NULL||pPop==NULL||len<=0) return result; std::stack DataStack; co
2014-12-24 14:38:45 278
翻译 剑指offer 面试题21 实现带有min函数的栈
template void StackWithMin::push(const T& data){ MainStack.push(data); if(MinStack.size()==0||data<MinStack.top()) MinStack.push(data); else MinStack.push(MinStack.top());}template void Sta
2014-12-24 13:45:13 277
翻译 剑指offer 面试题20 顺时针打印二维数组
待解决问题:将打印函数的数组形参写成二重指针,可以打印任何列数的数组,现在只能打印指定列数#include using namespace std;void PrintCircle(int matrix[][4],int begin,int row,int col){ for(int j=begin;j<col-begin;++j) cout<<matrix[begin][j]<<'
2014-12-24 10:44:21 448
翻译 剑指offer 面试题19 二叉树镜像
struct BinaryTreeNode{ int data; BinaryTreeNode *left; BinaryTreeNode *right;};void Mirror(BinaryTreeNode *pRoot){ if(pRoot==NULL||pRoot->left==NULL||pRoot->right==NULL) return; BinaryTreeNo
2014-12-24 09:29:58 307
翻译 剑指offer 面试题18 判断二叉树B是否是A的子结构
struct BinaryTreeNode{ int data; BinaryTreeNode *left; BinaryTreeNode *right;};//find a A-node that equals B-root,send it to the core func//judge:root--left--right,and recursionbool SubTree(Bin
2014-12-23 16:44:48 356
翻译 剑指offer 面试题17 合并两个有序链表
struct LinkNode{ int data; LinkNode *next;};LinkNode* Merge(LinkNode *pHead1,LinkNode *pHead2){ if(pHead1==NULL){ return pHead2; } else if(pHead2==NULL){ return pHead1; } LinkNode *pMerge
2014-12-23 12:48:48 345
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人