![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
剑指Offer
gfgdsg
这个作者很懒,什么都没留下…
展开
-
重建二叉树
/*//Rebuild #include #include using namespace std; typedef struct BinaryNode { char data; BinaryNode *lchild,*rchild; }*BinaryTree; void Rebuild(char *PreOrder,char *InOrder,BinaryTree *T,int TreeLe转载 2014-07-23 11:58:11 · 428 阅读 · 1 评论 -
二叉树的子结构
bool Recursive(Tree tree1, Tree tree2) { if (tree2 == NULL) return true; if (tree1 == NULL) return false; if (tree1->value != tree2->value) return false; return (Recursive(tree1->left, tree2原创 2014-10-10 11:56:04 · 436 阅读 · 0 评论 -
二叉树的镜像
void MirrorTree(Tree t) { if (t == NULL || (t->left == NULL&& t->right == NULL)) { return; } Tree temp; temp = t->left; t->left = t->right; t->right = temp; if (t->left) MirrorTree(t->left原创 2014-10-10 11:55:18 · 292 阅读 · 0 评论 -
把数组排成最小的数
#include #include #include #include using namespace std; bool compare(const string& str1, const string &str2) { string s1 = str1 + str2; string s2 = str2 + str1; return s1<s2; } void ComArray原创 2014-10-10 12:07:57 · 349 阅读 · 0 评论 -
字符串排列组合的应用
// String Combination字符串的排列 void Combination(char *string, int number, vector &result); void Combination(char *string) { assert(string != NULL); vector result; int i, length = strlen(string); for原创 2014-10-10 11:45:03 · 366 阅读 · 0 评论 -
和为s的两个数字 VS 和为s的连续正数序列
//和为s的两个数字 VS 和为s的连续正数序列 void TwoNumSum(int num[],int s,int len) { int i, j; for (i = 0; i < len; i++) { for (j = i + 1; j < len; j++) { if (num[i] + num[j] == s) { cout << num[i] <<原创 2014-10-10 11:45:55 · 421 阅读 · 0 评论 -
连续字数组中的最大和 分治法VS动态规划
//连续字数组中的最大和 //分治法 int MaxArray(int num[],int left,int right) { int sum = 0; if (left == right) { sum = num[left] > 0?num[left]:0; } int center = (left + right) / 2; int leftsum = MaxArray(num原创 2014-10-10 11:47:02 · 365 阅读 · 0 评论 -
二分查找解决数组中出现数字的次数
//find the num of the array times ; //BinarySearch int GetFirstK(int data[], int len, int k, int start, int end) { if (start>end) return -1; int midindex = (start + end) / 2; int middata = data[m原创 2014-10-10 11:49:11 · 399 阅读 · 0 评论 -
删除字符串中重复的元素
//delete the repeat char char* DeleteRepeat(char str[]) { if (str == NULL) return NULL; char *key,*temp,*result; key = str; result = str; const int tablesize = 256; bool hashTable[tablesize] =原创 2014-10-10 11:59:23 · 416 阅读 · 0 评论 -
扑克片的顺子
#include #include #include #include using namespace std; void transform(string str, vector&num) { if (str == "A") { str = "1"; } else if (str == "J") { str= "11"; } else if (str == "Q") {原创 2014-10-10 12:07:14 · 361 阅读 · 0 评论 -
字符串的排列(去除重复项)
//The Arrange Of String abc ----abc,acb,bac,bca,cab,cba //Delete Repeat Arrange http://blog.csdn.net/hackbuteer1/article/details/7462447 //Q:here i use the recursion way to solove this question.What原创 2014-10-10 11:41:33 · 383 阅读 · 0 评论 -
Find只出现一次的元素
void FindNumOfOne(char str[]) { int i,len; if (str == NULL) return; len = strlen(str); const int tableSize = 256; unsigned int hash[tableSize]; for (i = 0; i < tableSize; i++) { hash[i] = 0原创 2014-10-10 11:54:36 · 345 阅读 · 0 评论 -
旋转数组中最小的数字
int MinInOrder(int num[],int first,int end) { int result; result = num[first]; int i; for (i = first; i <= end; i++) { if (num[i] < result) result = num[i]; } return result; } int MinVocat原创 2014-10-13 19:22:26 · 387 阅读 · 0 评论 -
丑数
bool IsUglyNum(int num) { while (num % 2 == 0) num /= 2; while (num % 3 == 0) num /= 3; while (num % 5 == 0) num /= 5; return (num == 1) ? true : false; } int GetUglyNum(int index) { if (in原创 2014-10-14 09:37:04 · 271 阅读 · 0 评论 -
两个栈实现队列
template class CQueue { public: CQueue(); ~CQueue(); void appendTail(const T&node); T deleteHead(); private: stackstack1; stackstack2; }; template CQueue::CQueue() { } template CQueue::~CQueue()原创 2014-10-14 09:35:41 · 324 阅读 · 0 评论 -
atoi函数
//3.3 atoi //1.+.-符号。2.空格注意。3.溢出数据。4.无效输入。 int change(char *str) { int i,sum,temp; i=0;sum=0;temp=1; const int len=strlen(str); while(str[i]==' '&&i<len) { i++; } if(str[i]=='+') { i++;原创 2014-10-14 09:39:25 · 315 阅读 · 0 评论 -
两个字符串A,B,在A中删除B的元素
//we are students --- aeiou === w r stdnts char* DeleteTwoInOne(char first[],char second[]) { if (first == NULL || second == NULL) return NULL; int i; const int TableSize = 256; unsigned int ha原创 2014-10-10 11:58:28 · 1399 阅读 · 0 评论 -
数组中最小的k个数
//Find The least K-num in the array void TheLeastKnum(int num[], int first, int end, int k) { int index,i; index = partition(num, first, end); while (k != index) { if (index < k) { first =原创 2014-10-10 11:48:26 · 408 阅读 · 0 评论 -
回溯法解决八皇后问题
//Eight Queen Problem Recall Way回溯法解决八皇后问题 #define max 8 int queen[max], sum = 0; int check(int n) { int i; for (i = 0; i < n; i++) { if (queen[i] == queen[n] || abs(queen[i] - queen[n]) == (n原创 2014-10-10 11:39:48 · 423 阅读 · 0 评论 -
字符串左旋旋转
#include #include #include #include using namespace std; void reverse(char *start,char *end) { if(start==NULL||end==NULL) return; else { while(start<end) { char temp; temp=*end;原创 2014-07-23 14:43:17 · 341 阅读 · 0 评论 -
二叉树中和为某一值的路径
//二叉树中和为某一值的路径 #include #include #include #include #include #include using namespace std; typedef struct Binode { int data; struct Binode *lchid; struct Binode *rchild; }Binode,*Bitree; void Bitre原创 2014-07-23 14:41:42 · 379 阅读 · 0 评论 -
包含min函数的栈
//包含min函数的栈 #include #include #include using namespace std; template class StackMin { public: StackMin(void){}; ~StackMin(void){}; void MinPush(const T& value); void Minpop(); const T&min(); priv原创 2014-07-23 14:45:02 · 308 阅读 · 0 评论 -
调整数组顺序使奇数位于偶数前面
//二分查找,栈实现运算,调整数组顺序使奇数位于偶数前面 #include using namespace std; void Recorder(int *arr, int len, bool (*func)(int)) { if(arr==NULL||len==0) return ; int *left=arr; int *right=arr+len-1; while(原创 2014-07-23 14:46:24 · 314 阅读 · 0 评论 -
树中两个结点的最低公共祖先
#include"stdafx.h" #include #include #include using namespace std; struct TREE{ int v; TREE *left,*right; }*rhead; int shu[10009],all=0; void bulid(TREE *head){ int temp原创 2014-07-22 17:13:17 · 319 阅读 · 0 评论 -
替换空格和清除空格
替换空格和清除空格原创 2014-07-22 17:34:31 · 390 阅读 · 0 评论 -
链表的倒数第K个数
#include using namespace std; typedef struct Node { int data; Node *next; }lnode,*list; list listinit() { list L; L=(lnode*)malloc(sizeof(list)); if(L==NULL) cout L->next=NULL; return L;原创 2014-07-24 21:34:23 · 360 阅读 · 0 评论 -
循环链表解决约瑟夫环问题
#include using namespace std; typedef struct Node { int data; Node *next; }lnode,*list; list listcreateJoseph() //循环链表 { int i,len,data; list L,p,r; L=(lnode*)malloc(sizeof(lnode)); L->next=原创 2014-07-25 15:04:28 · 496 阅读 · 0 评论 -
链表的反转
#include using namespace std; typedef struct Node { int data; Node *next; }lnode,*list; list listinit() { list L; L=(lnode*)malloc(sizeof(list)); if(L==NULL) cout<<"没有足够的内存空间!"<<endl; L->next=原创 2014-07-24 21:35:43 · 320 阅读 · 0 评论 -
两个链表的第一个公共节点
#include using namespace std; typedef struct Node { int data; Node *next; }lnode,*list; list listinit() { list L; L=(lnode*)malloc(sizeof(list)); if(L==NULL) cout<<"没有足够的内存空间!"<<endl; L->next=原创 2014-07-25 09:35:56 · 368 阅读 · 0 评论 -
二叉树的前序中序后序遍历,非递归遍历 层次遍历
#include #include #include using namespace std; typedef struct Binode { char data; struct Binode *lchild,*rchild; }Binode,*Bitree; Bitree BitreeCreate() { Bitree T; char ch; cin>>ch; if(ch=='#')原创 2014-07-25 20:33:39 · 437 阅读 · 0 评论 -
二叉搜索树的后序判断
#include using namespace std; typedef struct Binode { int data; struct Binode *lchild,*rchild; }Binode,*Bitree; bool BitreeVerify(int str[],int length) { if(str==NULL||length<=0) return false;原创 2014-07-25 22:12:04 · 482 阅读 · 0 评论 -
二叉搜索树转化为双向链表
#include using namespace std; typedef struct Binode { int data; struct Binode *lchild,*rchild; }Binode,*Bitree; Bitree phead=NULL; Bitree pindex=NULL; void SortTree(Bitree &T,int element) { if(T==原创 2014-07-27 20:20:17 · 414 阅读 · 0 评论 -
append函数 两非递减链表的合并
#include using namespace std; typedef struct Node { int data; Node *next; }lnode,*list; list listinit() { list L; L=(lnode*)malloc(sizeof(list)); if(L==NULL) cout<<"没有足够的内存空间!"<<endl; L->next=原创 2014-07-27 20:23:47 · 447 阅读 · 0 评论 -
单词反转
#include #include #include #include using namespace std; void reverse(char str[],int start,int end) { int len,i; char temp; len=end-start+1; for(i=0;i<(len/2);i++) { temp=str[start+i]; st原创 2014-07-23 14:38:23 · 345 阅读 · 0 评论 -
数组中出现次数超过一半的数字
//1,2,3,2,2,2,5,4,2 len=9 return 2;出现次数超过一半 //元素在数组中出现的次数STL //各种排序 void BubleSort(int num[],int len) { int i, j,temp; for (i = 0; i < len; i++) { for (j = 0; j < len -1- i; j++) { if (nu原创 2014-10-10 11:46:42 · 352 阅读 · 0 评论