面试题

有幸读到july大神的blog,也来记录一下我的学习的点点滴滴。
july大神blog:<a target=_blank href="http://blog.youkuaiyun.com/v_july_v/article/details/6126406">点击打开链接</a>


1.输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
#include<iostream>
using namespace std;
struct BSTreeNode
{
	int m_Value;
	BSTreeNode *m_pLeft;
	BSTreeNode *m_pRight;
};
typedef BSTreeNode *DoubleList,*BSTree;
DoubleList phead,ptemp,plist;
void CreateBSTree(BSTree& root,int value)
{
    if(root)
	{
	   if(root->m_Value>value)
		   CreateBSTree(root->m_pLeft,value);
	   if(root->m_Value<value)
		   CreateBSTree(root->m_pRight,value);
	}
	else
	{
		root = new BSTreeNode;
		root ->m_Value =value;
		root ->m_pLeft = root->m_pRight =NULL;
	}
	
}
/*void CvtBSTtoDoubleList(BSTree root) 
{
      if(root)
      {
        CvtBSTtoDoubleList(root->m_pLeft);
        if(phead==NULL)
        {
            phead =plist= root;
            phead->m_pLeft=NULL;
        }
        else
        {
            plist ->m_pRight = root;
            root->m_pLeft = plist;
            plist = root;
        }
        CvtBSTtoDoubleList(root->m_pRight);
      }
}*/
void CvtBSTtoDoubleList(BSTree root)
{
      if(root)
	  {
		CvtBSTtoDoubleList(root->m_pLeft);
		ptemp = new BSTreeNode;
		ptemp ->m_Value = root->m_Value;
		if(phead==NULL)
		{
			phead =plist= ptemp;
            phead->m_pLeft=NULL;
		}
		else
		{
			plist ->m_pRight = ptemp;
			ptemp->m_pLeft = plist;
			plist = ptemp;
		}
		plist->m_pRight=NULL;
		CvtBSTtoDoubleList(root->m_pRight);
	  }
}
void ShowBSTree(BSTree root)
{
	if(root)
	{
		ShowBSTree(root->m_pLeft);
		cout<<root->m_Value<<" ";
		ShowBSTree(root->m_pRight);
	}
}
void ShowDoubleList(DoubleList head)
{
	while(head)
	{
		cout<<head->m_Value<<" ";
		head = head->m_pRight;
	}
	cout<<endl;
}
void main()
{
	int data[]={8,2,3,4,5,6};
    BSTreeNode *root=NULL;
	for(int i=0;i<sizeof(data)/sizeof(data[0]);i++)
		CreateBSTree(root,data[i]);
	ShowBSTree(root);
	cout<<endl;
	phead=NULL;
	CvtBSTtoDoubleList(root);
	ShowDoubleList(phead);
}
2.定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。
要求函数min、push以及pop的时间复杂度都是O(1)。
#include<iostream>
#include<iomanip>
using namespace std;
#define N  100
struct StackNode
{
	int num[N];
	int top;
};
typedef StackNode Stack,*pStack;
void InitStack(pStack& s)
{
	s=new Stack;
	s->top=-1;
}
void push(pStack& s,int value)
{
	s->num[++s->top]=value;
}
void pop(pStack& s,int& value)
{
    value = s->num[s->top];
	s->top--;
}
int min(pStack& temps)
{
    int value;
	pop(temps,value);
	return value;
}
void main()
{
   int num[]={2,10,3,1,22,3},value,minvalue;
   pStack s,temps;
   InitStack(s);
   InitStack(temps);
   minvalue = num[0];
   for(int i=0;i<sizeof(num)/sizeof(*num);i++)
   {
     push(s,num[i]);
	 if(minvalue>num[i]) 
	 {
		 minvalue=num[i];
	 }
	 push(temps,minvalue);
   }
   for(i=0;i<sizeof(num)/sizeof(*num);i++)
   {
	  pop(s,value);
	  cout<<left<<setw(5)<<value<<" ";
	  pop(temps,value);//或者是用cout<<min(temps)<<endl;
	  cout<<left<<setw(5)<<value<<endl;
   }      
}
3.当访问到某一结点时,把该结点添加到路径上,并累加当前结点的值。
如果当前结点为叶结点并且当前路径的和刚好等于输入的整数,则当前的路径符合要求,我们把它打印出来
#include<iostream>  
#include<vector>
using namespace std;  
struct BTreeNode  
{  
    int m_Value;  
    BTreeNode *m_pLeft;  
    BTreeNode *m_pRight;  
};  
typedef BTreeNode *BTree;  
int sum,value;
vector<int> path;
void CreateBTree(BTree& root)  
{  
	int value;
	cin>>value;
    if(!value) root=NULL;
	else
    {  
           root = new BTreeNode;  
           root ->m_Value =value; 
           CreateBTree(root->m_pLeft);  
           CreateBTree(root->m_pRight);  
    }  
}  
void FindPath(BTree root,int sum,vector<int>& path,int& currentvalue)
{
	if(!root) return ; 
	currentvalue+=root->m_Value;
	path.push_back(root->m_Value);
	bool isLeaf = !root->m_pLeft && !root->m_pRight;
	if(currentvalue == sum && isLeaf)
	{
		vector<int>::iterator it=path.begin();
		for(;it!=path.end();++it)
			cout<<*it<<" ";
		cout<<endl;
	}
	if(root->m_pLeft) 
		FindPath(root->m_pLeft, sum, path, currentvalue);
	if(root->m_pRight)
		FindPath(root->m_pRight,sum, path,currentvalue);
	currentvalue-=root->m_Value;
	path.pop_back();
}
void ShowBTree(BTree root)
{
	if(root)
	{
                 ShowBTree(root->m_pLeft);
		 cout<<root->m_Value<<" ";
		 ShowBTree(root->m_pRight);
	}
}
void main()  
{  
   
    BTreeNode *root=NULL;  
    CreateBTree(root); 
	ShowBTree(root);
	cout<<endl;
	cin>>sum;
	FindPath(root,sum,path,value);
  
}
4.检测链表中是否有环。
#include<iostream>
using namespace std;
struct Node
{
    int data;
    Node *next;
};
typedef Node *List;
void CreateList(List& head)
{
    int num[]={10,3,4,5,1};
    List p,q;
    for(int i=0;i<sizeof(num)/sizeof(*num);i++)
    {
        q=new Node;
        q->data=num[i];
        if(i==0)
        {
            head=p=q;
        }
        else
        {
            p->next=q;
            p=q;
        }
    }
    p->next=head;//指向头,构成循环链表
}
void ShowList(List head)
{
    List p=head;
    while(1)
    {
        cout<<p->data<<" ";
        p=p->next;
        if(p==head) break; 
    }
    cout<<endl;
}
bool Check(List head) //检测链表是否构成环
{
    List p=head,q=p->next;
    while(p->next && q->next)
    {
        p=p->next;
        q=q->next->next;
        if(p==q)
            return true;
    }
    return false;
}
void main()
{
    List head=NULL;
    CreateList(head);
    ShowList(head);
    if(Check(head))
        cout<<"1"<<endl;
    else
        cout<<"0"<<endl;
}
5.单词翻转
#include<iostream>
#include<string>
using namespace std;
void Reverse(char str[],int begin,int end)
{
	char temp;
	for(int i=begin;i<begin+(end-begin)/2;i++)
	{
		temp=str[i];
		str[i]=str[begin+end-i-1];
		str[begin+end-i-1]=temp;
	}
}
void ReverseWords(char str[])
{
    int begin,end;
	bool isbegin=false,isend=false;
	for(int i=0;str[i]!='\0';i++)
	{
		if(i==0 && str[i]!=' ') {begin=0;isbegin=true;}
		else
		{
			if(str[i-1]==' ' && str[i]!=' ') {begin=i;isbegin=true;}
			if(str[i-1]!=' ' && str[i]==' ')
			{end =i;isend=true;}
			else if(str[i-1]!=' '&& str[i+1]=='\0')
			{end=i+1;isend=true;}
		}
        if(isbegin && isend)
		{
			Reverse(str,begin,end);
			isbegin=false;
			isend = false;
		}
	}
	Reverse(str,0,strlen(str));
}
void main()
{
	char str[100];
	gets(str);
	ReverseWords(str);
	cout<<str<<endl;
}
6.求二叉树中节点的最大距离
#include<iostream>
using namespace std;
struct BTNode
{
	int key;
	BTNode* pLeft;
	BTNode* pRight;
};
typedef BTNode* BTree;
int max=0;
void CreateBTree(BTree & root)
{
	int value;
	scanf("%d",&value);
	if(!value) root=NULL;
	else
	{
		root = new BTNode;
		root->key = value;
		CreateBTree(root->pLeft);
		CreateBTree(root->pRight);
	}
}
int FindLenMax(BTree root)
{
	int lh,rh,len;
	if(!root) return 0;
	if(!root->pLeft && !root->pRight)
		return 1;
	lh=FindLenMax(root->pLeft);
	rh=FindLenMax(root->pRight);
	len = lh+rh;
	if(len>max) max=len;
	return lh>rh?lh+1:rh+1;
}
void main()
{
	BTree root;
	CreateBTree(root);
	FindLenMax(root);
	cout<<max<<endl;
}
7.题目:输入一颗二元查找树,将该树转换为它的镜像,
#include<iostream>
using namespace std;
struct BSTNode
{
	int value;
	BSTNode *m_pLeft;
    BSTNode *m_pRight;
};
typedef BSTNode *BSTree;
void CreateBSTree(BSTree& root,int value)
{
    if(root)
	{
       if(root->value>value) 
	   CreateBSTree(root->m_pLeft,value);
	   if(root->value<value)
	   CreateBSTree(root->m_pRight,value);
	}
	else
	{
		root=new BSTNode;
		root->value=value;
		root->m_pLeft=root->m_pRight=NULL;
	}
}
void ShowBSTree(BSTree root)
{
	if(root)
	{
		ShowBSTree(root->m_pLeft);
		cout<<root->value<<" ";
		ShowBSTree(root->m_pRight);
	}
}
void RevertBTree(BSTree& root)
{
	if(!root) return ;
	BSTree p;
	p = root->m_pLeft;
	root->m_pLeft = root->m_pRight;
	root->m_pRight = p;
    if(root->m_pLeft)
		RevertBTree(root->m_pLeft);
	if(root->m_pRight)
		RevertBTree(root->m_pRight);
}
void main()
{
	int data[]={8,6,5,10,9,11};
	BSTree root=NULL;
	for(int i=0;i<sizeof(data)/sizeof(data[0]);i++)
		CreateBSTree(root,data[i]);
	ShowBSTree(root);
	cout<<endl;
	RevertBTree(root);
	ShowBSTree(root);
	cout<<endl;
}
非递归
#include<iostream>
#include<stack>
using namespace std;
struct BSTNode
{
	int value;
	BSTNode *m_pLeft;
    BSTNode *m_pRight;
};
typedef BSTNode *BSTree;
void CreateBSTree(BSTree& root,int value)
{
    if(root)
	{
       if(root->value>value) 
	   CreateBSTree(root->m_pLeft,value);
	   if(root->value<value)
	   CreateBSTree(root->m_pRight,value);
	}
	else
	{
		root=new BSTNode;
		root->value=value;
		root->m_pLeft=root->m_pRight=NULL;
	}
}
void ShowBSTree(BSTree root)
{
	if(root)
	{
		ShowBSTree(root->m_pLeft);
		cout<<root->value<<" ";
		ShowBSTree(root->m_pRight);
	}
}
void RevertBTree(BSTree& root)
{
   if(!root) return;
   stack<BSTree> Stack;
   Stack.push(root);
   while(!Stack.empty())
   {
	   BSTree p=Stack.top();
	   Stack.pop();
	   BSTree pTemp;
	   pTemp = p->m_pLeft;
	   p->m_pLeft = p->m_pRight;
	   p->m_pRight = pTemp;
	   if(p->m_pLeft)
		  Stack.push(p->m_pLeft);
	   if(p->m_pRight)
		  Stack.push(p->m_pRight);  
   }

}
void main()
{
	int data[]={8,6,5,10,9,11};
	BSTree root=NULL;
	for(int i=0;i<sizeof(data)/sizeof(data[0]);i++)
		CreateBSTree(root,data[i]);
	ShowBSTree(root);
	cout<<endl;
	RevertBTree(root);
	ShowBSTree(root);
	cout<<endl;
}
8.约瑟夫问题
#include<iostream>
using namespace std;
typedef struct Node
{
    int num;
    Node* next;
}Node,*List;
void CreateCircleList(List& head,int n)
{
    List p,q;
    for(int i=0;i<n;i++)
    {
        q= new Node;
        q->num=i+1;
        if(i==0)
        {
            head=p=q;
        }
        else
        {
            p->next=q;
            p=q;
        }
    }
    p->next=head;
}
void DeleteNum(List head,int m)
{
    List q=head,p;
    int i=0;
    while(q!=q->next)
    {
      i++;
      if(i==m)
      {
       cout<<q->num<<" ";
       p->next=q->next;
       i=0;
      }
      p=q;
      q=q->next;
    }
    cout<<q->num<<endl;
}
void main()
{
   List head;
   int n,m;
   cout<<"输入n:";
   cin>>n;
   CreateCircleList(head,n);
   cout<<"输入m:";
   cin>>m;
   DeleteNum(head,m);
 
 
}
9.在一个字符串中找到第一个只出现一次的字符
#include<iostream>
#include<string>
using namespace std;
struct Info
{
	int num;
	int index;
};
void FindOnlyOneChar(char *str)
{
	Info info[256];
	memset(info,0,sizeof(Info)*256);
	int i=1;
	while(*str!='\0')
	{
		info[*str].num++;
		info[*str].index=i;
		str++;
		i++;
	}
	int minindex=256,temp;
	for(i=1;i<256;i++)
	{
		if(info[i].num==1)
		{
			if(minindex>info[i].index) 
			{
				minindex=info[i].index;
				temp=i;
			}
		}
	}
	
	cout<<(char)temp<<endl;
}
void main()
{
	char str[100];
	gets(str);
	FindOnlyOneChar(str);
}
10.输入一个表示整数的字符串,把该字符串转换成整数并输出
#include<iostream>
using namespace std;
int StringToInt(const char *str)
{
	int  sum=0,flag=1;
	if(str==NULL) return 0;
	else if(*str=='+') str++;
	else if(*str=='-') str++,flag=-1;
	while(*str!='\0')
	{
		if(*str<'0' || *str>'9')
			return 0;
		sum=10*sum+*str-'0';
		str++;
	}
	return flag*sum;
}
void main()
{
	char str[20];
	gets(str);
	cout<<StringToInt(str)<<endl;
}
11.输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数,
使其和等于 m ,要求将其中所有的可能组合列出来.
不重复情况:
#include<iostream>
#include<vector>
using namespace std;
vector<int> vect;
void  Find_Factor(int m,int n)
{
     if(n<=0 || m<=0) return ;
	 if(m==n) 
	 {
         vector<int>::iterator it = vect.begin();
		 for(;it!=vect.end();++it)
			 cout<<*it<<" ";
		 cout<<m<<endl;
	 }
	   vect.push_back(n);
	   Find_Factor(m-n,n-1);
	   vect.pop_back();
	   Find_Factor(m,n-1);
}
void main()
{
	int m,n;
	cin>>m>>n;
    Find_Factor(m,n);
}
重复情况:
#include<iostream>
#include<vector>
using namespace std;
vector<int> vect;
void  Find_Factor(int m,int n)
{
     if(n<=0 || m<=0) return ;
	 if(m==n) 
	 {
         vector<int>::iterator it = vect.begin();
		 for(;it!=vect.end();++it)
			 cout<<*it<<" ";
		 cout<<m<<endl;
	 }
	   for(int i=n;i>=1;i--)
	   {
	    vect.push_back(i);
		int k=m-i,temp=i;
		if(k<i) i=k;
	    Find_Factor(k,i);
		i = temp;
	    vect.pop_back();
	   }
}
void main()
{
	int m,n;
	cin>>m>>n;
    Find_Factor(m,n);
}
12.输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数。
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int FindOneNumber(int n)
{
	char str[10];
	int len,m,sum=0,temp,i;
	m=n;
	sprintf(str,"%d",n);
	len = strlen(str);
	for(i=len;i>=1;i--)
	{
		temp = pow(10,i-1);
		if(i==len)
		{
		if(n/temp>=2) sum+=temp;
		else
			sum+=n%temp+1;
		}
		else
		{
			sum+=(n/(10*temp)+1)*temp;
		}
	}
	return sum;
}
void main()
{
	int n;
	while(cin>>n)
	{
		cout<<FindOneNumber(n)<<endl;
	}


}
13.最大列表的和
#include<iostream>
using namespace std;
#define N  100
int maxSum(int *a,int n)
{
    int max=0,i,b=0,begin,end;
	begin=end=0;
	for(i=0;i<n;i++)
	{
       if(b<=0)
		   b=a[i],begin=i;
	   else
		   b+=a[i];
	   if(b>max) 
	   {
		   max=b;
		   end=i;
	   }
	}
	cout<<begin<<" "<<end<<endl;
	return max;
}
void main()
{
	int a[]={1,-2,3,10,-4,7,-2,-5};
	cout<<maxSum(a,sizeof(a)/sizeof(*a))<<endl;
}

14.求最长公共子序列

//LCS
#include<iostream>
#include<string>
using namespace std;
void LCS_Length(string x,string y,int (*c)[100],int (*b)[100])
{
	int m,n;
	m=x.length();
	n=x.length();
	int i,j;
	//如果i或j等于0则c[i][j]=0;
	for(i=1;i<=m;i++)
	{
		c[i][0]=0;
	}
	for(i=1;i<=n;i++)
	{
		c[0][i]=0;
	}
	//遍历两个字符串,依次标记c[i][j],c[i][j]标记了从x的开始到第i个元素与从
	//y开始到第j个元素中LCS的长度
	//数组b用来标记最长公共子串所要走的路线,该路线为两个字符串组成的矩阵中的对应的字母
	for(i=1;i<=m;i++)
	{
		for(j=1;j<=n;j++)
		{
			if(x[i-1]==y[j-1])
			{
				c[i][j]=c[i-1][j-1]+1;
				//代替箭头指向左上
				b[i][j]=1;
			}
			else
			{
				if(c[i][j-1]>=c[i-1][j])
				{
					c[i][j]=c[i][j-1];
					//代替箭头指左
					b[i][j]=2;
				}
				else
				{
					c[i][j]=c[i-1][j];
					//代替箭头指向上
					b[i][j]=3;
				}
			}
		}
	}
}
void PrintAnswer(string x,int(*b)[100],int i,int j)
{
	if(i==0||j==0)
	{
		return ;
	}
	else
	{
		if(b[i][j]==1)
		{
			PrintAnswer(x,b,i-1,j-1);
			cout<<x[i-1]<<" ";
		}
		else if(b[i][j]==2)
		{
			PrintAnswer(x,b,i,j-1);
		}
		else
		{
			PrintAnswer(x,b,i-1,j);
		}
	}
}
int main()
{
	string x="aadaae";
	string y="adaaf";
	int c[100][100]={0};
	int b[100][100]={0};
	LCS_Length(x,y,c,b);
	cout<<"the LCS is: "<<c[x.length()][y.length()]<<endl;
/*	for(int i=0;i<=x.length();i++)
	{
		for(int j=0;j<=y.length();j++)
		cout<<c[i][j]<<" ";
		cout<<endl;
	}
	*/
	PrintAnswer(x,b,x.length(),y.length());
	return 0;

}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值