二叉树的镜像问题 //来源于剑指offer typedef char DataType;//二叉树的镜像void MirrorRecursively(TreeNode* tree){ if(tree==NULL) { return; } if(tree->leftchild==NULL && tree->rightchild==NULL) { return; } TreeNode
判断一棵树是否为平衡二叉树 typedef char DataType;typedef struct TNode{ DataType value; struct TNode* leftchild; struct TNode* rightchild;}TreeNode;//求解树的高度 int TreeDepth(TreeNode* tree){ if(tree==NULL) { return
判断两棵树是否相等 typedef struct TNode{ DataType value; struct TNode* leftchild; struct TNode* rightchild;}TreeNode;int CompTree(TreeNode* tree1,TreeNode* tree2){ if(tree1==NULL&&tree2==NULL) return 1; if
简单的函数指针问题 #includeusing namespace std;int max(int a,int b){ return (a>b)?a:b;}int main(){ int (*f)(int,int) = &max; int a,b,c; cin >> a >> b >> c; cout << (*f)((*f)(a,b),c) << endl; return 0;}
一个简单的链表实现 #include#include#includeusing namespace std;typedef struct student{ int data; struct student* next; }node;node* create(){ node*head,*p,*s; int x,cycle = 1; head = (node*)malloc(sizeo
数组重排问题 #includeusing namespace std;void Arrange(int a[],int len){ int i,j = len-1; for(i=len-1;i>=0;--i) { if(a[i]!=0) { if(a[j]==0) { a[j] = a[i]; a[i] = 0; } --j; } }}
旋转数组的最小值问题 #include using namespace std;//旋转数组的最小数字 int Min(int* numbers, int len){ if(numbers==NULL || len<=0) { return -1; } int index1 = 0; int index2 = len-1; int indexMid = index1
逆序输出链表元素 关键代码:struct ListNode{ int m_nKey; ListNode* m_pNext;};//法一使用栈的方法 void PrintListReversingly_Iteratively(ListNode* pHead){ std::stack nodes; ListNode* pNode = pHaed; while(pNode!=NULL) {
linux 下怎么编译生成a.out文件? 编辑保存源文件后 在shell中执行 gcc test.c 如果没有编译错误,gcc会在当前目录下生成一个a.out文件, 然后可以 ./a.out 结果出来了当然你也可以使用 –o 选项给生成的文件起一个别的名字,像 gcc test.c –o test , 则gcc会生成一个名为test的可执行文件点击打开链接
编程之美 - 求数组的子数组之和的最大值 #includeusing namespace std;int max(int a,int b){ return (a>b)?a:b;}int MaxSum(int A[],int n){ int nStart = A[n-1]; int nAll = A[n-1]; for(int i=n-2; i>=0; --i) { nStart = max(A[i],nSta
找出数组之中只出现一次的数字 /* *N+2个数,N个数出现了偶数次, *2个数出现了奇数次(这两个数不相等),问用O(1)的空间复杂度, *找出这两个数,不需要知道具体位置,只需要知道这两个值。 */ #include using namespace std; //判断indexBit位上是否为1 bool IsBit1(int num, unsigned int indexBit) {
逆序替换空格 -- 剑指offer #include using namespace std;void ReplaceBlank(char str[], int length){ if(str==NULL || length <= 0) { return; } int originalLength = 0; int numberOfBlank = 0; int i = 0; while(str[i]!=
全排列与组合问题 全排列问题:#include using namespace std; void permutation(char* a,int k,int m) { int i,j; static int count = 1; if(k == m) { cout << "第" << count++ << "种方法是: "; for(i=0;i
Linux命令面试问题及答案 1. 如何暂停一个正在运行的进程,把其放在后台(不运行)? 答案:为了停止正在运行的进程,让其在后台运行,我们可以使用组合键 Ctrl+Z。 2. 什么是安装Linux所需的最小分区数量,以及如何查看系统启动信息? 答案:单独一个/root分区足以执行所有的系统任务,但是强烈建议安装Linux时,需要至少三个分区:/root,/boot,/swap。一个IDE硬盘驱动
学习C/C++编程,Linux平台的优势与劣势 劣势:1. 熟悉Linux的人不多,熟悉它需要较长时间。2. 中文资源不够多,往往不全或者不够新。3. 过去学的很多windows的东西,到Linux下行不通。4. 充满Linux风味的vim/emacs上手难,需大量使用才能熟练。5. 门槛高,初学时学习难度曲线陡峭。优势:1. 物以稀为贵,掌握Linux的人,比windows的少很多,而Linux
sizeof和strlen比较 #include #include #includeusing namespace std; int main() { char s[] = "123456"; cout << strlen(s) << endl; cout << sizeof(s)/sizeof(*s) << endl; cout << sizeof(s)/sizeof(char) <<
寻找和为定值的多个数 编程求解:输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来。#include #include using namespace std; //全局变量 listlist1; //从1,2....n找到和为sum的数字 void find_factor(int sum, int
字符串逆序 #include#include#includeusing namespace std;/* *Author: http://blog.csdn.net/godwin_q *程序环境: C—Free5.0 *语言:C++ *功能:字符串逆序 */ void ReverseOrder(char* s){ char temp; int len = strlen
快速排序程序代码 #includeusing namespace std;/* *Author: http://blog.csdn.net/godwin_q *程序环境: C—Free5.0 *语言:C++ */void Swap(int& a, int& b){ int temp = a; a = b; b = temp; }int Partion(int a[], i