2012腾讯实习生内推面试题

1.链表逆置

  1. #include <iostream>  
  2. #include <ctime>  
  3. using namespace std;  
  4.   
  5. struct node  
  6. {  
  7.     int value;  
  8.     node * next;  
  9. };  
  10.   
  11. node* reverse(node * );  
  12. node * make_link();  
  13. void display(node *);  
  14.   
  15. int main()  
  16. {  
  17.   
  18.     node* head = make_link();  
  19.     display(head);  
  20.     head = reverse(head);  
  21.     display(head);  
  22.     return 0;  
  23. }  
  24.   
  25. node* reverse(node *head)  
  26. {  
  27.     node *pre, *cur, *post;  
  28.     if(!head || !head->next)  
  29.         return head;  
  30.   
  31.     pre = head;  
  32.     cur = pre->next;  
  33.       
  34.     while(cur)  
  35.     {     
  36.         post = cur->next;  
  37.         cur->next = pre;  
  38.         pre = cur;  
  39.         cur = post;  
  40.     }  
  41.     head->next = NULL;  
  42.     return pre;  
  43. }  
  44. node * make_link()  
  45. {  
  46.     srand(time(NULL));  
  47.     node *head = new node();  
  48.     node *cur = head;  
  49.     for(int i= 0; i < 10; i++)  
  50.     {  
  51.         cur->value = rand() % 10;  
  52.         cur->next = new node();  
  53.         cur = cur->next;  
  54.     }  
  55.     return head;  
  56. }  
  57. void display(node *head)  
  58. {  
  59.     node *cur = head;  
  60.     while(cur)  
  61.     {  
  62.         cout << cur->value << " " ;  
  63.           
  64.         cur = cur->next;  
  65.     }  
  66.     cout << endl;  
  67. }  


2.链表合并,将两个有序的链表合并为一个


  1. #include <iostream>  
  2. #include <ctime>  
  3.   
  4. using namespace std;  
  5.   
  6.   
  7. struct node  
  8. {  
  9.     int value;  
  10.     node * next;  
  11. };  
  12.   
  13. node * make_link();  
  14. void display(node *);  
  15. void sort(node *);  
  16. node * merge(node *,node *);  
  17.   
  18. int main()  
  19. {  
  20.   
  21.     node* head1 = make_link();  
  22.     display(head1);  
  23.     sort(head1);  
  24.   
  25.     node* head2 = make_link();  
  26.     display(head2);  
  27.     sort(head2);  
  28.   
  29.     node *head = merge(head1,head2);  
  30.   
  31.     display(head);  
  32.     return 0;  
  33. }  
  34.   
  35. node * make_link()  
  36. {  
  37.     srand(time(NULL));  
  38.     node *head = new node();  
  39.     node *cur = head;  
  40.     for(int i= 0; i < 10; i++)  
  41.     {  
  42.         cur->value = rand() % 10;  
  43.         cur->next = new node();  
  44.         cur = cur->next;  
  45.     }  
  46.     return head;  
  47. }  
  48. void display(node *head)  
  49. {  
  50.     node *cur = head;  
  51.     while(cur)  
  52.     {  
  53.         cout << cur->value << " " ;  
  54.           
  55.         cur = cur->next;  
  56.     }  
  57.     cout << endl;  
  58. }  
  59.   
  60. void sort(node *head)  
  61. {  
  62.     //cout << "sorting" << endl;  
  63.     node *cur = head;  
  64.       
  65.     while(cur)  
  66.     {  
  67.         node *min = cur;  
  68.         node *cur2 = cur->next;  
  69.         while(cur2)  
  70.         {  
  71.             if(cur2->value < min->value)  
  72.             {  
  73.                 min = cur2;   
  74.             }  
  75.             cur2 = cur2->next;  
  76.         }  
  77.         int tem = cur->value;  
  78.         cur->value = min->value;  
  79.         min->value = tem;  
  80.         cur = cur->next;  
  81.     }  
  82.   
  83.   
  84. }  
  85. node * merge(node *h1,node *h2)  
  86. {  
  87.     node *mcur = new node();  
  88.     node *cur1 = h1 , *cur2 = h2;  
  89.     while(cur1 && cur2){  
  90.         if(cur1->value < cur2->value){  
  91.             mcur->next = cur1;  
  92.             mcur = mcur->next;  
  93.             cur1 = cur1->next;  
  94.         }else{  
  95.             mcur->next = cur2;  
  96.             mcur = mcur->next;  
  97.             cur2 = cur2->next;  
  98.         }  
  99.     }  
  100.     if(cur1)  
  101.     {  
  102.         mcur->next = cur1;  
  103.     }else  
  104.     {  
  105.         mcur->next = cur2;  
  106.     }  
  107.       
  108.     return h1->value < h2->value ? h1 : h2;  
  109. }  


3.一棵树是否某条路径结点之和等于给定值。并描述算法复杂度

  1. //基本思想:递归  
  2. #include <iostream>  
  3. #include <ctime>  
  4. using namespace std;  
  5.   
  6. struct node  
  7. {  
  8.     int value;  
  9.     node *left;  
  10.     node *right;  
  11.       
  12. };  
  13.   
  14. node * build_tree();  
  15. bool find(node *, int);  
  16.   
  17. int main(void)  
  18. {  
  19.     node *root = build_tree();  
  20.     int t;  
  21.     cin >> t;  
  22.     cout << find(root,t);  
  23.       
  24.     return 0;  
  25. }  
  26.   
  27.   
  28.   
  29. node * build_tree()  
  30. {  
  31.     int a;  
  32.     cin >> a;  
  33.     if(a == 0)  
  34.     {  
  35.         return NULL;  
  36.     }  
  37.     node *root = new node();  
  38.     root->value = a;  
  39.     root->left = build_tree();  
  40.     root->right = build_tree();  
  41.     return root;  
  42. }  
  43. bool find(node *root,int v) //时间复杂度:  
  44. //平均要查找n条路径,路径上的平均节点数为树的高度——lg(n),对每个节点的操作为常量时间,所以时间复杂度为 n * lg(n)  
  45. {  
  46.     if(root == NULL)  
  47.         return false;  
  48.     if(root->value == v)  
  49.         return true;  
  50.     else   
  51.         return find(root->left, v-root->value) || find(root->right, v-root->value);  
  52. }  









4.你熟悉的排序算法并描述算法复杂度。

  1. //快排:分难合易  
  2. //基本思想:先用partition()函数找到中位数,再分别对小于和大于中位数的那部分进行快速排序  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. int partition(int a[], int b, int e)  
  7. {  
  8.     int i = -1;  
  9.     for(int j = 0; j < e; j++)  
  10.     {  
  11.         if(a[j] < a[e])  
  12.         {  
  13.             i++;  
  14.             int tem = a[i];  
  15.             a[i] = a[j];  
  16.             a[j] = tem;  
  17.         }  
  18.     }  
  19.     int tem = a[i + 1];  
  20.     a[i + 1] = a[e];  
  21.     a[e] = tem;  
  22.     return i + 1;  
  23.   
  24. }  
  25.   
  26. void qsort(int a[], int b, int e)//时间复杂度:  
  27. {  
  28.     if(b < e)  
  29.     {  
  30.         int m = partition(a,b,e);  
  31.         qsort(a,b,m-1);  
  32.         qsort(a,m+1,e);  
  33.     }  
  34. }  
  35.   
  36.   
  37. int main()  
  38. {  
  39.     int a[] = {2,9,4,1,6,8};  
  40.     qsort(a, 0, 5);  
  41.     for(int i = 0; i < 6; i++)  
  42.     {  
  43.         cout << a[i] << " ";  
  44.     }  
  45.     cout << endl;  
  46.     return 0;  
  47. }  

  1. //归并排序:分易合难  
  2.   
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. void display(int a[], int size)  
  7. {  
  8.     for(int i= 0; i < size; i++)  
  9.     {  
  10.         cout << a[i] << " ";   
  11.     }  
  12.     cout << endl;  
  13. }  
  14.   
  15. void merge(int *a, int p, int q, int r)  
  16. {  
  17.     int n1= q- p + 1;  
  18.     int n2 = r - q;  
  19.     int f[100], s[100];  
  20.     for(int i = 0; i < n1; i++)  
  21.     {  
  22.         f[i] = a[p+i];  
  23.     }  
  24.     for(int i = 0; i < n2; i++)  
  25.     {  
  26.         s[i] = a[q + 1 + i];  
  27.     }  
  28.     f[n1] = 9999;//哨兵  
  29.     s[n2] = 9999;  
  30.     int i = 0, j = 0;  
  31.     for(int k = p; k <= r; k++)  
  32.     {  
  33.         if(f[i] < s[j])  
  34.         {  
  35.             a[k] = f[i++];  
  36.         }  
  37.         else{  
  38.             a[k] = s[j++];  
  39.         }  
  40.     }  
  41. }  
  42.   
  43. void merge_sort(int *a, int b, int e)  
  44. {  
  45.     if(b < e)  
  46.     {  
  47.         int p = (b + e) / 2;  
  48.         merge_sort(a,b,p);  
  49.         merge_sort(a,p+1,e);  
  50.         merge(a,b,p,e);  
  51.     }  
  52. }  
  53.   
  54. int main()  
  55. {  
  56.     int a[] = {9,3,5,7,6,8,10,22,21,34};  
  57.     display(a, 10);  
  58.     merge_sort(a, 0, 9);  
  59.     display(a, 10);  
  60.       
  61.     return 0;  
  62. }  


  1. //推排序  
  2. //a[0]存放数组大小,既方便得到数组大小,又方便堆中节点的父/子下标的计算  
  3. //基本思想:先从底往上建大根堆,然后每次将数组最后一个元素与堆的根元素交换,并将堆的大小减一,再保持堆的性质。 heapsort<-build_heap<-max_heapify  
  4. #include <iostream>  
  5. using namespace std;  
  6.   
  7.   
  8. void exchange(int &a,int &b)  
  9. {  
  10.     int tem = a;  
  11.     a = b;  
  12.     b = tem;  
  13. }  
  14.   
  15. void display(int a[])  
  16. {  
  17.     for(int i= 1; i <= a[0]; i++)  
  18.     {  
  19.         cout << a[i] << " ";   
  20.     }  
  21.     cout << endl;  
  22. }  
  23.   
  24. void max_heapify(int *a, int i)  
  25. {  
  26.     int max = i;  
  27.     int l = a[0];  
  28.     if(2 * i <= l && a[2 * i] > a[max]){  
  29.         max = 2 * i;  
  30.     }  
  31.     if(2 * i + 1 <= l && a[2 * i + 1] > a[max])  
  32.     {  
  33.         max = 2 * i + 1;  
  34.     }  
  35.     if(max != i)  
  36.     {  
  37.         exchange(a[i],a[max]);  
  38.         max_heapify(a, max);  
  39.     }  
  40. }  
  41.   
  42. void build_max_heap(int *a)  
  43. {  
  44.     int len = a[0];  
  45.     for(int i = len/2; i > 0; i--)  
  46.     {  
  47.         max_heapify(a,i);  
  48.     }  
  49. }  
  50. void heapsort(int a[])  
  51. {  
  52.     int len = a[0];  
  53.     for(int i = len;i > 1; i--)  
  54.     {  
  55.         exchange(a[i],a[1]);  
  56.         a[0]--;  
  57.         max_heapify(a,1);  
  58.     }  
  59.     a[0] = len;  
  60. }  
  61.   
  62.   
  63.   
  64. int main()  
  65. {  
  66.     int a[] = {9,3,5,7,6,8,10,22,21,34};  
  67.     display(a);  
  68.     build_max_heap(a);  
  69.     display(a);  
  70.     heapsort(a);  
  71.     display(a);  
  72.     return 0;  
  73. }  

  1. //选择排序  
  2. //基本思想:  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. void selectionSort(int a[], int n)  
  7. {  
  8.     for(int i = 0; i < n - 1; i++)  
  9.     {  
  10.         int index = i;  
  11.         for(int j = i + 1; j < n; j ++)  
  12.         {  
  13.             if(a[j] < a[i])  
  14.                 index = j;  
  15.         }  
  16.         int tem = a[i];  
  17.         a[i] = a[index];  
  18.         a[index] = tem;  
  19.     }  
  20. }  
  21.   
  22. int main()  
  23. {  
  24.     int a[] = {2,1,7,5,4,3};  
  25.     selectionSort(a, 6);  
  26.     for(int i = 0; i < 6; i++)  
  27.     {  
  28.         cout << a[i] << endl;  
  29.     }  
  30. }  

  1. //插入排序  
  2. //基本思想:  
  3. #include <iostream>  
  4. using namespace std;  
  5. void insertionSort(int a[], int n)  
  6. {  
  7.     for(int i = 1; i < n; i++)  
  8.     {  
  9.         int tem = a[i];  
  10.         int j;  
  11.         for(j = i - 1; j >= 0; j--)  
  12.         {  
  13.               
  14.             if(a[j] > tem)  
  15.             {  
  16.                 a[j + 1] = a[j];  
  17.             }else  
  18.             {  
  19.                 break;  
  20.             }  
  21.               
  22.         }  
  23.         a[j + 1] = tem;  
  24.     }  
  25. }  
  26.   
  27. int main()  
  28. {  
  29.     int a[] = {2,1,7,5,4,3};  
  30.     insertionSort(a, 6);  
  31.     for(int i = 0; i < 6; i++)  
  32.     {  
  33.         cout << a[i] << endl;  
  34.     }  
  35. }  




5.二叉树与B+树的区别

   ISAM树与B+树的区别

   溢出时,ISAM树采用溢出页的方式解决,B+树则动态调整自身的结构(此外B+树的叶子页之间用指针链起来)



6.一个html文件有一千个<a>,写一个js程序,当点击某一个<a>时,能输出其对应的序号

7.用js实现继承,实现多继承

8.一棵要能快速查找,需要什么性质,怎么保持这个性质

9.hash表


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值