人人网2014笔试算法题汇总

来源:互联网 发布:淘宝商城怎么注册 编辑:程序博客网 时间:2024/05/21 11:06

1.给出一个有序数组啊,长度为len,另外给出第三个数X,问是否能在数组中找到两个数,这两个数之和等于第三个数X。

我们首先看到第一句话,这个数组是有序的,所以,我们可以定义两个指针,一个指向数组的第一个元素,另一个指向应该指向的位置(这个需要看具体的实现和数组给定的值),首先计算两个位置的和是否等于给定的第三个数,如果等于则算法结束,如果大于,则尾指针向头指针方向移动,如果小于,则头指针向尾指针方向移动,当头指针大于等于尾指针时算法结束,没有找到这样的两个数。

解法一:

[cpp] view plaincopy
  1. #include <stdio.h>    
  2.     
  3. int judge(int *a, int len, int k, int *num1, int *num2);    
  4.     
  5. int main(int argc, char **argv)    
  6. {    
  7.     int test_array[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};    
  8.     int result = -1;    
  9.     int num1, num2;    
  10.     result = judge(test_array, sizeof(test_array) / sizeof(int), 12, &num1, &num2);    
  11.     if(result == 0)    
  12.     {    
  13.         printf("%d\t%d\n", num1, num2);    
  14.     }    
  15.     else if(result == -1)    
  16.     {    
  17.         printf("can't find");    
  18.     }    
  19.     else    
  20.     {    
  21.         printf("error");    
  22.     }    
  23. }    
  24.     
  25. int judge(int *a, int len, int k, int *num1, int *num2)    
  26. {    
  27.     int *low = NULL;    
  28.     int *high = NULL;    
  29.     int i = 0;    
  30.     int result = -1;    
  31.     if(a == NULL || len < 2)    
  32.     {    
  33.         return result;    
  34.     }    
  35.     if(a[0] >= k)    
  36.     {    
  37.         return result;    
  38.     }    
  39.     while(a[i] <= k && i < len)    
  40.     {    
  41.         i++;    
  42.     }    
  43.     low = a;    
  44.     high = a + i - 1;    
  45.     while(low  < high)    
  46.     {    
  47.         *num1 = *low;    
  48.         *num2 = *high;    
  49.         if((*low + *high) == k)    
  50.         {    
  51.             result = 0;    
  52.             break;    
  53.         }    
  54.         else if((*low + *high) > k)    
  55.         {    
  56.             high--;    
  57.         }    
  58.         else if((*low + *high) < k)    
  59.         {    
  60.             low++;    
  61.         }    
  62.     }    
  63.     return result;    
  64. }    

解法二:

[cpp] view plaincopy
  1. #include <iostream>    
  2.     
  3. using namespace std;    
  4.     
  5. int hash_table[100];    
  6.     
  7. bool judge(int *a, int len, int x)    
  8. {    
  9.     memset(hash_table, 0, sizeof(hash_table));    
  10.     for (int i=0; i<len; ++i)    
  11.     {    
  12.         hash_table[x - a[i]] = 1;    
  13.     }    
  14.     
  15.     for (int i=0; i<len; ++i)    
  16.     {    
  17.         if (hash_table[i] == 1)    
  18.         {    
  19.             return true;    
  20.         }    
  21.     }    
  22.     
  23.     return false;    
  24. }    
  25.     
  26. int main()    
  27. {    
  28.     int len = 10;    
  29.     int a[10] = {1, 3, 5, 7, 9, 4, 2, 8, 10, 6};    
  30.     int x = 19;    
  31.     
  32.     if (judge(a, len, x))    
  33.     {    
  34.         cout<<"Yes"<<endl;    
  35.     }    
  36.     else    
  37.     {    
  38.         cout<<"No"<<endl;    
  39.     }    
  40.     system("pause");    
  41.     return 0;    
  42. }    

本题解决方法:hash table。

时间复杂度:O(N)

空间复杂度:O(N)


2.给定有n个数的数组a,其中有超过一半的数为一个定值,在不进行排序,不开设额外数组的情况下,以最高效的算法找出这个数。

int find(int* a, int n);

[cpp] view plaincopy
  1. #include <iostream>    
  2.     
  3. using namespace std;    
  4.     
  5. int find(int *a, int n)    
  6. {    
  7.     int t = a[0];    
  8.     int count = 0;    
  9.     for (int i=0; i<n; ++i)    
  10.     {    
  11.         if (count == 0)    
  12.         {    
  13.             t = a[i];    
  14.             count = 1;    
  15.             continue;    
  16.         }    
  17.         else    
  18.         {    
  19.             if (a[i] == t)    
  20.             {    
  21.                 count++;    
  22.             }    
  23.             else    
  24.             {    
  25.                 count--;    
  26.             }    
  27.         }    
  28.     }    
  29.     
  30.     return t;    
  31. }    
  32.     
  33. int main()    
  34. {    
  35.     int n = 10;    
  36.     int a[10] = {1, 3, 2, 3, 3, 4, 3, 3, 3, 6};    
  37.     
  38.     cout<<find(a, n)<<endl;    
  39.     
  40.     system("pause");    
  41.     return 0;    
  42. }    

Time Complexity: O(n)

Space Complexity:O(1)


转载请注明原创链接:http://blog.csdn.net/wujunokay/article/details/12209217

原创粉丝点击