整理:数据结构面试大全

1.判断链表是否存在环型链表
问题:判断一个链表是否存在环,例如下面这个链表就存在一个环:
例如 N1->N2->N3->N4->N5->N2 就是一个有环的链表,环的开始结点是 N5
这里有一个比较简单的解法。设置两个指针 p1 p2 。每次循环 p1 向前走一步, p2 向前走两步。直到 p2 碰到 NULL 指针或者两个指针相等结束循环。如果两个指针相等则说明存在环。
struct link 
{
   int data;
    link* next;
};
 
bool IsLoop(link* head)
{
    link* p1=head, *p2 = head;
     if (head ==NULL || head->next ==NULL) 
     {
          return false;
     }
    do{
        p1= p1->next;
        p2 = p2->next->next;
    } while(p2 && p2->next && p1!=p2);     
     if(p1 == p2)
          return true;
     else
          return false;
}
2,链表反转
单向链表的反转是一个经常被问到的一个面试题,也是一个非常基础的问题。比如一个链表是这样的: 1->2->3->4->5 通过反转后成为5->4->3->2->1。最容易想到的方法遍历一遍链表,利用一个辅助指针,存储遍历过程中当前指针指向的下一个元素,然后将当前节点元素的指针反转后,利用已经存储的指针往后面继续遍历。源代码如下:
struct linka {
     int data;
     linka* next;
};
 
void reverse(linka*& head)
{
     if(head ==NULL)
          return;
     linka*pre, *cur, *ne;
     pre=head;
     cur=head->next;
     while(cur)
     {
          ne = cur->next;
          cur->next = pre;
          pre = cur;
          cur = ne;
     }
     head->next = NULL;
     head = pre;
}
还有一种利用递归的方法。这种方法的基本思想是在反转当前节点之前先调用递归函数反转后续节点。源代码如下。不过这个方法有一个缺点,就是在反转后的最后一个结点会形成一个环,所以必须将函数的返回的节点的next域置为NULL。因为要改变head指针,所以我用了引用。算法的源代码如下:
linka* reverse(linka* p,linka*& head)
{
     if(p == NULL || p->next == NULL)
     {
          head=p;
          return p;
     }
     else
     {
          linka* tmp = reverse(p->next,head);
          tmp->next = p;
          return p;
     }
}
3,判断两个数组中是否存在相同的数字
给定两个排好序的数组,怎样高效得判断这两个数组中存在相同的数字?
这个问题首先想到的是一个O(nlogn)的算法。就是任意挑选一个数组,遍历这个数组的所有元素,遍历过程中,在另一个数组中对第一个数组中的每个元素进行binary search。用C++实现代码如下:
bool findcommon(int a[],int size1,int b[],int size2)
{
     int i;
     for(i=0;i<size1;i++)
     {
          int start=0,end=size2-1,mid;
          while(start<=end)
          {
               mid=(start+end)/2;
               if(a[i]==b[mid])
                    return true;
               else if (a[i]<b[mid])
                    end=mid-1;
               else
                    start=mid+1;
          }
     }
     return false;
}
后来发现有一个 O(n)算法。因为两个数组都是排好序的。所以只要一次遍历就行了。首先设两个下标,分别初始化为两个数组的起始地址,依次向前推进。推进的规则是比较两个数组中的数字,小的那个数组的下标向前推进一步,直到任何一个数组的下标到达数组末尾时,如果这时还没碰到相同的数字,说明数组中没有相同的数字。
bool findcommon2(int a[], int size1, int b[], int size2)
{
     int i=0,j=0;
     while(i<size1 && j<size2)
     {
          if(a[i]==b[j])
               return true;
          if(a[i]>b[j])
               j++;
          if(a[i]<b[j])
               i++;
     }
     return false;
}
4,最大子序列
问题:
给定一整数序列A1, A2,... An (可能有负数),求A1~An的一个子序列Ai~Aj,使得Ai到Aj的和最大
例如:
整数序列-2, 11, -4, 13, -5, 2, -5, -3, 12, -9的最大子序列的和为21。
对于这个问题,最简单也是最容易想到的那就是穷举所有子序列的方法。利用三重循环,依次求出所有子序列的和然后取最大的那个。当然算法复杂度会达到O(n^3)。显然这种方法不是最优的,下面给出一个算法复杂度为O(n)的线性算法实现,算法的来源于 Programming Pearls一书。
在给出线性算法之前,先来看一个对穷举算法进行优化的算法,它的算法复杂度为O(n^2)。其实这个算法只是对对穷举算法稍微做了一些修改:其实子序列的和我们并不需要每次都重新计算一遍。假设Sum(i, j)是A[i] ... A[j]的和,那么Sum(i, j+1) = Sum(i, j) + A[j+1]。利用这一个递推,我们就可以得到下面这个算法:
int max_sub(int a[],int size)
{
     int i,j,v,max=a[0];
     for(i=0;i<size;i++)
     {
          v=0;
          for(j=i;j<size;j++)
          {
               v=v+a[j];//Sum(i, j+1) = Sum(i, j) + A[j+1]
               if(v>max)
                    max=v;
          }
     }
     return max;
}
那怎样才能达到线性复杂度呢?这里运用动态规划的思想。先看一下源代码实现:
int max_sub2(int a[], int size)
{
     int i,max=0,temp_sum=0;
     for(i=0;i<size;i++)
     {
          temp_sum+=a[i];
          if(temp_sum>max)
               max=temp_sum;
          else if(temp_sum<0)
               temp_sum=0;
     }
     return max;
}
在这一遍扫描数组当中,从左到右记录当前子序列的和temp_sum,若这个和不断增加,那么最大子序列的和max也不断增加(不断更新max)。如果往前扫描中遇到负数,那么当前子序列的和将会减小。此时temp_sum 将会小于max,当然max也就不更新。如果temp_sum降到0时,说明前面已经扫描的那一段就可以抛弃了,这时将temp_sum置为0。然后,temp_sum将从后面开始将这个子段进行分析,若有比当前max大的子段,继续更新max。这样一趟扫描结果也就出来了。
5, 找出单向链表的中间结点
这道题和解判断链表是否存在环,我用的是非常类似的方法,只不过结束循环的条件和函数返回值不一样罢了。设置两个指针p1,p2。每次循环p1向前走一步,p2向前走两步。当p2到达链表的末尾时,p1指向的时链表的中间。
link* mid(link* head)
{
       link* p1,*p2;
       p1=p2=head;
       if(head==NULL || head->next==NULL)
              return head;
       do {
              p1=p1->next;
              p2=p2->next->next;
       } while(p2 && p2->next);
       return p1;
}
6,按单词反转字符串
并不是简单的字符串反转,而是按给定字符串里的单词将字符串倒转过来,就是说字符串里面的单词还是保持原来的顺序,这里的每个单词用空格分开。例如:
Here is www.fishksy.com.cn
经过反转后变为:
www.fishksy.com.cn is Here
如果只是简单的将所有字符串翻转的话,可以遍历字符串,将第一个字符和最后一个交换,第二个和倒数第二个交换,依次循环。其实按照单词反转的话可以在第一遍遍历的基础上,再遍历一遍字符串,对每一个单词再反转一次。这样每个单词又恢复了原来的顺序。
char* reverse_word(const char* str)
{
     int len = strlen(str);
     char* restr = new char[len+1];
     strcpy(restr,str);
     int i,j;
     for(i=0,j=len-1;i<j;i++,j--)
     {
          char temp=restr[i];
          restr[i]=restr[j];
          restr[j]=temp;
     }
     int k=0;
     while(k<len)
     {
          i=j=k;
          while(restr[j]!=' ' && restr[j]!='/0' )
               j++;
          k=j+1;
          j--;
          for(;i<j;i++,j--)
          {
               char temp=restr[i];
               restr[i]=restr[j];
               restr[j]=temp;
          }
     }
     return restr;
}
如果考虑空间和时间的优化的话,当然可以将上面代码里两个字符串交换部分改为异或实现。
例如将
          char temp=restr[i];
          restr[i]=restr[j];
          restr[j]=temp;
改为
          restr[i]^=restr[j];
          restr[j]^=restr[i];
          restr[i]^=restr[j];
 
7,字符串反转
我没有记错的话是一道MSN的笔试题,网上无意中看到的,拿来做了一下。题目是这样的,给定一个字符串,一个这个字符串的子串,将第一个字符串反转,但保留子串的顺序不变。例如:
输入:第一个字符串: "This is fishsky 's Chinese site: http://www.fishsky.com.cn/cn"
子串: "fishsky"
输出: "nc/nc.moc.fishsky.www//:ptth :etis esenihC s'fishsky si sihT"
一般的方法是先扫描一边第一个字符串,然后用stack把它反转,同时记录下子串出现的位置。然后再扫描一遍把记录下来的子串再用stack反转。我用的方法是用一遍扫描数组的方法。扫描中如果发现子串,就将子串倒过来压入堆栈。
最后再将堆栈里的字符弹出,这样子串又恢复了原来的顺序。源代码如下:
#include <iostream>
#include <cassert>
#include <stack>
using namespace std;
//reverse the string 's1' except the substring 'token'.
const char* reverse(const char* s1, const char* token)
{
       assert(s1 && token);
       stack<char> stack1;
       const char* ptoken = token, *head = s1, *rear = s1;
       while (*head != '/0')
       {
              while(*head!= '/0' && *ptoken == *head)
              {
                 ptoken++;
                 head++;
              }
              if(*ptoken == '/0')//contain the token
              {
                 const char* p;
                 for(p=head-1;p>=rear;p--)
                        stack1.push(*p);
 
                 ptoken = token;
                 rear = head;
              }
              else
              {
                 stack1.push(*rear);
                 head=++rear;
                 ptoken = token;
              }
       }
       char * return_v = new char[strlen(s1)+1];
       int i=0;
       while(!stack1.empty())
       {
              return_v[i++] = stack1.top();
              stack1.pop();
       }
       return_v[i]='/0';
       return return_v;
}
int main(int argc, char* argv[])
{
       
       cout<<"This is fishsky 's Chinese site: http://www.fishsky.com.cn/cn/n";
       cout<<reverse("This is fishsky's Chinese site: http://www. fishsky.com.cn/cn"," fishsky ");
       return 0;
}
 
8, 删除数组中重复的数字
问题:一个动态长度可变的数字序列,以数字0为结束标志,要求将重复的数字用一个数字代替,例如:
将数组 1,1,1,2,2,2,2,2,7,7,1,5,5,5,0 转变成1,2,7,1,5,0
问题比较简单,要注意的是这个数组是动态的。所以避免麻烦我还是用了STL的vector。
#include <iostream>
#include <vector>
using namespace std;
//remove the duplicated numbers in an intger array, the array was end with 0;
//e.g. 1,1,1,2,2,5,4,4,4,4,1,0 --->1,2,5,4,1,0
void static remove_duplicated(int a[], vector<int>& _st)
{
       _st.push_back(a[0]);
       for(int i=1;_st[_st.size()-1]!=0;i++)
       {
              if(a[i-1]!=a[i])
                 _st.push_back(a[i]);
       }
}
当然如果可以改变原来的数组的话,可以不用STL,仅需要指针操作就可以了。下面这个程序将修改原来数组的内容。
void static remove_duplicated2(int a[])
{
       if(a[0]==0 || a==NULL)
              return;
       int insert=1,current=1;
       while(a[current]!=0)
       {
              if(a[current]!=a[current-1])
              {
                 a[insert]=a[current];
                 insert++;
                 current++;
              }
              else
                 current++;
       }
       a[insert]=0;
}
 
9,如何判断一棵二叉树是否是平衡二叉树
问题:判断一个二叉排序树是否是平衡二叉树 
解决方案:
根据平衡二叉树的定义,如果任意节点的左右子树的深度相差不超过1,那这棵树就是平衡二叉树。
首先编写一个计算二叉树深度的函数,利用递归实现。
template<typename T>
static int Depth(BSTreeNode<T>* pbs)
{
       if (pbs==NULL)
              return 0;
       else
       {
              int ld = Depth(pbs->left);
              int rd = Depth(pbs->right);
              return 1 + (ld >rd ? ld : rd);
       }
}
下面是利用递归判断左右子树的深度是否相差1来判断是否是平衡二叉树的函数:
template<typename T>
static bool isBalance(BSTreeNode<T>* pbs)
{
       if (pbs==NULL) 
              return true;
       int dis = Depth(pbs->left) - Depth(pbs->right);
       if (dis>1 || dis<-1 )
              return false;
       else
              return isBalance(pbs->left) && isBalance(pbs->right);
}
10, strstr()的简单实现
strstr(s1,s2)是一个经常用的函数,他的作用就是在字符串s1中寻找字符串s2如果找到了就返回指针,否则返回NULL。
下面是这个函数的一个简单实现:
static const char* _strstr(const char* s1, const char* s2)
{
     assert(s2 && s1);
     const char* p=s1, *r=s2;
     while(*p!='/0')
     {
          while(*p++==*r++);
          if(*r=='/0')
               return p;
          else
          {
               r=s2;
               p=++s1;
          }
     }
     return NULL;
}
11,半素数
题目定义了一种叫半素数的数:只要一个数能被分解成两个素数,那么这个数就是半素数。
Prime Number Definition
An integer greater than one is called a prime number if its only positive divisors (factors) are one and itself. For instance, 2, 11, 67, 89 are prime numbers but 8, 20, 27 are not.
Semi-Prime Number Definition
An integer greater than one is called a semi-prime number if it can be decompounded to TWO prime numbers. For example, 6 is a semi-prime number but 12 is not.
Your task is just to determinate whether a given number is a semi-prime number.
Input
There are several test cases in the input. Each case contains a single integer N (2 <= N <= 1,000,000)
Output
One line with a single integer for each case. If the number is a semi-prime number, then output "Yes", otherwise "No".
Sample Input
3
4
6
12
Sample Output
No
Yes
Yes
No
没什么好说的,解法很简单,解法如下:
#include <iostream>
#include <cmath>
using namespace std;
bool isprime(long test)
{
     int i;
     for(i=2;i<=sqrt((long double)test);i++)
     {
          if(test%i ==0)
               return false;
     }
     return true;
}
bool isSemiPrime(long test)
{
     int i;
     for(i=2;i<=sqrt((long double)test);i++)
     {
          if(test%i ==0)
          {
               int temp = test/i;
               return isprime(i) && isprime(temp);
          }
     }
     return false;
}
int main()
{
     long n;
     while(cin>>n && n !=0)
     {
          if(isSemiPrime(n))
               cout<<"Yes"<<endl;
          else
               cout<<"No"<<endl;
     }
}
12,淘汰赛问题
题目:
Our school is planning to hold a new exciting computer programming contest. During each round of the contest, the competitors will be paired, and compete head-to-head. The loser will be eliminated, and the winner will advance to next round. It proceeds until there is only one competitor left, who is the champion. In a certain round, if the number of the remaining competitors is not even, one of them will be chosed randomly to advance to next round automatically, and then the others will be paired and fight as usual. The contest committee want to know how many rounds is needed to produce to champion, then they could prepare enough problems for the contest.
Input 
The input consists of several test cases. Each case consists of a single line containing a integer N - the number of the competitors in total. 1 <= N <= 2,147,483,647. An input with 0(zero) signals the end of the input, which should not be processed.
Output 
For each test case, output the number of rounds needed in the contest, on a single line.
Sample Input 
8
16
15
0
Sample Output 
3
4
4
题目比较简单,下面是我给的解法。其实就是计算一个数是2的几次方。
#include <iostream>
using namespace std;
long calculate(long test)
{
     long ret = 0;
     bool is2square = true;
     while(test!=1)
     {
          if(test % 2)
               is2square = false;
          test /= 2;
         ret++;
     }
     if(!is2square)
          ret++;
     return ret;
}
 
int main()
{
     long n;
     while(cin>>n && n !=0)
     {
          cout<<calculate(n)<<endl;
     }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值