微软面试题(收集)

 1、在排序数组中,找出给定数字的出现次数,比如 [1, 2, 2, 2, 3] 中2的出现次数是3次。

#include <stdio.h>

extern int low=0,high=10,mid=0;

void find(int *a,int p)//找到P的其中位置
{
	int i=0,j=0;
	while(low<high && j==0)
	{
		mid=(low+high)/2;
		if(a[mid]==p)
		{
		//	return mid;
			j=1;
		}
		else if(a[mid]>p)
		{
			high=mid;
		}
		else low=mid;
	}
}

int tosit(int *a,int p)//以P为中心向两边扩散
{
	int i,j;
	int counter=1;
	for(i=mid,j=mid;i>=low || j<=high;)
	{
		i=i-1;
		j=j+1;
		if(i>=low)
		{
			if(a[i]!=p)
			{
				i=low-1;
			}
			else
			{
				counter++;
			}
		}
		if(j<=high)
		{
			if(a[j]!=p)
			{
				j=high+1;
			}
			else
			{
				counter++;
			}
		}
		
	}
	return counter;
}
void main()
{
	int a[10]={1,2,2,2,6,6,7,8,9,9};
	int p=9;
	int k;
	find(a,p);
	k=tosit(a,p);
	printf("%d出现了%d次",p,k);	
}


算法复杂度为O(logn)。

 2、反序一个单向链表

(本题做法转自:http://www.baihowff.com/post/156.html

怎么做呢?最简单的就是在创建一个链表...不停的插入一个head就可以了...那必须用到两个额外变量...不过很好理解...BaihowFF今天发的显然不是这个方法...BaihowFF给出的方法只用增加一个临时变量p就可以实现了...怎么实现呢?

我先简单说一下算法...其实就是将p先指向head->next ,再将head->next赋为NULL,再执行循环while(p!=NULL)时交换head和p->next 以及 head和p就可以了...其实就是将p储存下一个链表元素..然后反复的往head前添加...这个算法其实和用两个的思想一样..但是写起来很巧妙...先给出完整程序吧...

 

#include <iostream> 
using namespace std; 
  
struct Node  
{ 
    Node *next; 
    int   data; 
}; 
typedef Node* pNode; 
  
pNode   Reverse(pNode head); 
void    PrintList(pNode head); 
  
int main(int argc,char* argv[])  
{    
    // 初始化一个单链表 head开始为012345,最后一个命名last 
    pNode head=new Node;    head->data=0; 
    pNode a1=new Node;      a1->data  =1;    head->next=a1; 
    pNode a2=new Node;      a2->data  =2;    a1->next=a2; 
    pNode a3=new Node;      a3->data  =3;    a2->next=a3; 
    pNode a4=new Node;      a4->data  =4;    a3->next=a4; 
    pNode last=new Node;    last->data=5;    a4->next=last; 
    last->next=NULL; 
     
    cout<<"链表翻转前: ";PrintList(head); 
    head=Reverse(head); 
    cout<<"链表翻转后: ";PrintList(head); 
  
    return 0;  
}  
  
  
pNode Reverse(pNode head){  
    // 防止传入为空链表 
    if ( head == NULL) return NULL; 
  
    // 预处理 
    pNode p=new Node; 
    p = head->next;  
    head->next = NULL; 
     
    // 交换前后..用p表示未交换头..head反转后存储 
    while (p != NULL) 
    {  
        swap(p->next, head); //系统函数swap 
        swap(head, p);  
    }  
     
    // 防止内存泄露 
    delete p; 
    p=NULL; 
     
    // 实现链式操作 
    return head;  
}  
  
void    PrintList(pNode p) 
{ 
    while (NULL != p) 
    { 
        cout<<p->data; 
        p=p->next; 
    } 
    cout<<endl; 
}


3、写出正则表达式,从一个字符串中提取链接地址。比如下面字符串中
"IT面试题博客中包含很多  <a href=
http://hi.baidu.com/mianshiti/blog/category/微软面试题> 微软面试题 </a> "
则需要提取的地址为 "
http://hi.baidu.com/mianshiti/blog/category/微软面试题 "
使用linux的awk方法:

awk -F'[<>]' '{print $2}' 文件名|grep 'http.*'


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值