摘要:给你一个由n-1个整数组成的未排序的序列,其元素都是1到n中的不同的整数。请写出一个寻找序列中缺失整数的线性时间算法。 分析:只要通过异或算法就可实现,由于1^1=0,2^2=0,0^n = 0;因此,数组中的所有数据与1-n一起做异或,缺失的数据就会显现出来, 代码如下所示:
阅读全文
摘要:2.1 给定三个整数a,b,c,实现 int median(int a, int b, int c),返回三个数的中位数,不可使用sort,要求整数操作(比较,位运算,加减乘除等)次数尽量少,并分析说明程序最坏和平均情况下使用的操作次数。 分析:中位数的意思是一个有序列中间的一个(奇数个数情况)或者
阅读全文
摘要:#include using namespace std; //链表结构体 struct ListNode { int m_Value; ListNode *next; }; //创建一个单链表 ListNode *CreateList(int *a,int n) { ListNode *pHead = NULL; ListNode *pTemp = NULL; int i = ...
阅读全文
摘要:#include #include using namespace std; char *ReplaceSpace(char *str,int MaxLen) { int SpaceNum = 0; int srcIdx = 0; int srcLen = 0; int dstIdx = 0; int dstLen = 0; char *strTmp = str; if(NU...
阅读全文
摘要:#include #include #include using namespace std; struct ListNode { int m_Value; ListNode *next; }; void ReversePrint(ListNode* pHead) { std::stack s; ListNode *pTemp = pHead; if(NULL == pHead...
阅读全文