在写代码期间,把写代码过程中比较常用的一些代码片段做个记录,下面的代码段是关于C++算法之寻找丢失的数的代码,应该是对大家也有帮助。
void get_lost_number(int data[], int length)
{
int index;
assert(NULL != data && 0 != length);
for(index = 0; index < length; index ++){
if(0 == pFlag[data[index]])
pFlag[data[index]] = 1;
}
for(index = 0; index < length; index++){
if(0 == pFlag[index])
printf("%dn", index);
}
free(pFlag);
return;
}
可能朋友也看到了,上面的代码需要分配和原来数据一样length的空间。其实我们可以用bit进行访问标志的设定,所以我们申请的空间还可以减少。
void get_lost_number(int data[], int length)
{
int index;
assert(NULL != data && 0 != length);
for(index = 0; index < length; index ++){
if(0 == (pFlag[data[index] >> 3] & (1 << (data[index] % 8))))
pFl