微软等数据结构+算法面试100题(18)--百度面试题

/*
2010 年3 道百度面试题[相信,你懂其中的含金量]
1.a~z 包括大小写与0~9 组成的N 个数, 用最快的方式把其中重复的元素挑出来。
*/
void RepeatChars(char *str)
{
	int tmp[256];
	memset(tmp,0,sizeof(tmp));
	for(int i=0;str[i]!='\0';i++)
		tmp[str[i]]++;
	for(int i=0;i<256;i++)
		if(tmp[i]>1)
			cout<<static_cast<char>(i)<<endl;
}

void RepeatCharsTest()
{
	char str[]="azszz00943erdfSSJJJKKqo93";
	cout<<"str"<<str<<endl;
	RepeatChars(str);
}


 

 

/*
1.今年百度的一道题目
百度笔试:给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数。
要求:空间复杂度O(1),时间复杂度为O(n)。
ANSWER
Have done this.
2.百度笔试题
用C 语言实现函数void * memmove(void *dest, const void *src, size_t n)。
memmove 函数的功能是拷贝src 所指的内存内容前n 个字节到dest 所指的地址上。
*/

void * MemMove(void *dest, const void *src, size_t n)
{
	if(dest==NULL||src==NULL)
		return NULL;

	void *res=dest;
	char* pdest=(char*)dest;
	char* psrc=(char*)src;
	if(pdest>=psrc&&pdest<psrc+n)
	{
		char* pdestend=pdest+n-1;
		char* psrcend=psrc+n-1;
		while(n)
		{
			*pdestend=*psrcend;
			pdestend--;psrcend--;
			n--;
		}
	}
	else
	{
		while(n)
		{
			*pdest=*psrc;
			pdest++;psrc++;
			n--;
		}
	}
	return res;
}

void EvenAndOdd(int *p,int len)
{
	int even=-1,odd=-1;
	for(int i=0;i<len;i++)
	{
		if(odd==-1)
		{
			if(even==-1&&p[i]%2==0)
				even=i;
			if(even>=0&&p[i]%2!=0)
				odd=i;
		}
		if(odd>=0&&even>=0)
		{
			int tmp=p[odd];
			for(int k=odd;k>even;k--)
				p[k]=p[k-1];
			p[even]=tmp;
			even++;
			odd=-1;
		}
	}
}

void EvenAndOddTest()
{
	int p[]={1,2,3,4,5,6,7,8,9,10};
	int len=sizeof(p)/sizeof(int);
	cout<<"the array : ";
	ShowArray(p,len);
	EvenAndOdd(p,len);
	cout<<"the array : ";
	ShowArray(p,len);
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值