- 博客(20)
- 收藏
- 关注
原创 【二叉树】后续遍历二叉树
void PostTraversal(BTNode *root){ BTNode *cur = NULL; BTNode *pre = NULL; stack s; s.Push(root); while(!s.IsEmpty()) { cur = s.GetTop(); if((cur->lchild == NU
2013-10-30 21:17:26 759
原创 【二叉树】求二叉树中两个节点的最近公共父节点
BTNode *pResult = null;int GetFirstCommonFather(BTNode *root,BTNode *p1,BTNode *p2){ if(pResult != null) return 0; //如果result所指向的不为null,说明已经找到。 if(root == null) return 0; int sum = 0;
2013-10-30 15:03:24 1074
原创 【二叉树】中序遍历二叉树
//递归中序遍历二叉树void InOrder1(BinTree *root){ if(root != NULL) { InOrder1(root->lchild); printf("%c",root->data); InOrder1(root->rchild); }}//非递归中序遍历二叉树 --- 用栈实现vo
2013-10-29 20:11:05 898
原创 【二叉树】先序遍历二叉树
//递归先序遍历二叉树void PreOrder1(BinTree *root){ if(root != NULL) { printf("%c",root->data); PreOrder1(root->lchild); PreOrder2(root->rchild); }}//非递归先序遍历二叉树 --- 用栈实现
2013-10-29 20:07:01 898
原创 PHP中的单例模式
class Singleton{ private static $_instance = null; //变量为私有、静态、初始化为null private function __construct(){} //构造函数私有化 public static function getInstance(){
2013-10-29 18:19:42 738
原创 堆排序
void HeapAdjust(int a[],int s,int t){ int rc = a[s]; int j; for(j=2*s+1;j<=t;j=2*j+1) { if(j<t && a[j] < a[j+1]) j=j+1; if(rc >= a[j]) break; a[s] = a[j];
2013-10-29 18:01:03 623
原创 希尔排序
void ShellSort(int a[],int n){ int i,j,d,tmp; d = n/2; while(d>=1) { for(i=d;i<n;i++) { tmp = a[i]; j = i-d; while(j>=0 && tmp < a[j]) {
2013-10-29 16:48:46 681
原创 选择排序
void SelectSort(int a[],int n){ int i,j,k,tmp; for(i=0;i<n-1;i++) { k = i; for(j=i+1;j<n;j++) { if(a[j] < a[k]) { k = j;
2013-10-29 15:54:56 584
原创 直接插入排序
void InsertSort(int a[],int n){ int i,j,tmp; for(i=1;i<n;i++) { j = i; tmp = a[j]; while(j > 0 && tmp < a[j-1]) { a[j] = a[j-1]; j--;
2013-10-29 15:12:22 670
原创 冒泡排序
void BubbleSort(int a[],int n){ int i,j,tmp,exchange; for(i=0;i<n;i++) { exchange = 0; //本趟排序开始前,交换标志应为假 for(j=n-1;j>i;j--) //采用自下向上扫描的方法,让小数冒出来 { if(a
2013-10-29 11:21:29 659
原创 KMP算法
串匹配先来回忆一下串匹配场景,不外乎是给定两个字符串 S 和 T ,然后在 S 串中查找 T 串,如果查找成功就是匹配,否则就是不匹配。比如:S = "abababaababacb";T = “ababacb”; C = “abcc” ;那么结果就是 T 匹配 S ,而 C 不能匹配 S 。朴素的串匹配算法再来回顾一下朴素的匹配思想,从 S[i] 开始,逐个检查 S[i+pos]
2013-10-28 17:50:21 756
原创 数组中只出现1次的两个数字
// 一个整型数组里除了2个数字分别出现1次之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。// 要求时间复杂度是O(n),空间复杂度是O(1)。//如果我们能把问题中的数组分成2个子数组,使得每个子数组中都只有一个唯一的元素以及很多成对的元素,那么我们就可以求出每个子数组中唯一的元素,最终就可以得到原数组中2个出现次数唯一的元素。方法是这样的:1. 首先数组中所有元素依
2013-10-24 13:50:40 769
原创 求整型数组中只出现1次的数字
//一个整型数组里除了1个数字之外,其他的数字都出现了两次。请写程序找出只出现一次的数字。//要求时间复杂度是O(n),空间复杂度是O(1)。int FindOne(int a[],int len){ /* 异或的基本性质: 1. 2个相同的数异或等于0 2. 任何数与0异或都等于其本身 */ int ret = 0; in
2013-10-24 13:25:06 704
原创 辗转相除法求两个数的最大公约数
int gcd(int a,int b){ int m = a > b ? a : b; int n = a > b ? b : a; int r = m % n; while(r != 0) { m = n; n = r; r = m % n; } return n;}
2013-10-16 16:44:10 656
原创 字符串 --- 循环移位问题
字符串循环移位问题是面试中比较容易遇到的,就是输入一个字符串和一个整数,原地输出移位后的字符串。不同的考官可能对程序的具体要求不同,这里要求空间复杂度为O(1)。这里给出两种解答方法。(1)将移动n位看做“每次移动一位,共操作n次”,这是一种化整为零的思维方法。只要能想到这一步,相信下面的代码就不难写出了: 1 void shift_1(char* str)
2013-10-15 16:50:25 909
原创 【二叉树】判断一棵二叉树是否是平衡二叉树
//是否是平衡二叉树int IsBalanceTree(bnode *root){ if(root == NULL) { return 1; } int leftDepth = GetDepth(root->pLeft); int rightDepth = GetDepth(root->pRight); int distanc
2013-10-15 12:32:55 1054
原创 【二叉树】求树的高度(深度)
//求二叉树的深度int GetDepth(bnode *root){ if(root == NULL) { return 0; } int leftDepth = GetDepth(root->pLeft); int rightDepth = GetDepth(root->pRight); return (leftDepth
2013-10-15 12:31:35 4128
原创 一些字符串及内存操作函数的实现
#include char *_strcpy(char *dest, const char *src){ //assert(dest != NULL && src != NULL); char *p = dest; while(*src != '\0') { *dest++ = *src++; } return p;}
2013-10-12 17:11:54 908
原创 二路归并排序
#include void MergeArray(int a[],int s,int m,int t){ int i = s; int j = m+1; int tmp[t+1]; int k=0; while(i<=m && j<=t) { if(a[i] <= a[j]) { tm
2013-10-10 10:47:49 684
原创 数组实现队列(C语言版)
#include #define QUEUE_SIZE 50typedef struct SeqQueue{ int data[QUEUE_SIZE]; int front; int rear;}Queue;Queue *InitQueue(){ Queue *q = (Queue *)malloc(sizeof(Queue)); if(
2013-10-09 13:41:08 5000 1
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人