2014 --人人网笔试算法汇总

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

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

解法一:

  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. }    
#include <stdio.h>  
  
int judge(int *a, int len, int k, int *num1, int *num2);  
  
int main(int argc, char **argv)  
{  
    int test_array[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};  
    int result = -1;  
    int num1, num2;  
    result = judge(test_array, sizeof(test_array) / sizeof(int), 12, &num1, &num2);  
    if(result == 0)  
    {  
        printf("%d\t%d\n", num1, num2);  
    }  
    else if(result == -1)  
    {  
        printf("can't find");  
    }  
    else  
    {  
        printf("error");  
    }  
}  
  
int judge(int *a, int len, int k, int *num1, int *num2)  
{  
    int *low = NULL;  
    int *high = NULL;  
    int i = 0;  
    int result = -1;  
    if(a == NULL || len < 2)  
    {  
        return result;  
    }  
    if(a[0] >= k)  
    {  
        return result;  
    }  
    while(a[i] <= k && i < len)  
    {  
        i++;  
    }  
    low = a;  
    high = a + i - 1;  
    while(low  < high)  
    {  
        *num1 = *low;  
        *num2 = *high;  
        if((*low + *high) == k)  
        {  
            result = 0;  
            break;  
        }  
        else if((*low + *high) > k)  
        {  
            high--;  
        }  
        else if((*low + *high) < k)  
        {  
            low++;  
        }  
    }  
    return result;  
}  

解法二:

  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. }    
#include <iostream>  
  
using namespace std;  
  
int hash_table[100];  
  
bool judge(int *a, int len, int x)  
{  
    memset(hash_table, 0, sizeof(hash_table));  
    for (int i=0; i<len; ++i)  
    {  
        hash_table[x - a[i]] = 1;  
    }  
  
    for (int i=0; i<len; ++i)  
    {  
        if (hash_table[i] == 1)  
        {  
            return true;  
        }  
    }  
  
    return false;  
}  
  
int main()  
{  
    int len = 10;  
    int a[10] = {1, 3, 5, 7, 9, 4, 2, 8, 10, 6};  
    int x = 19;  
  
    if (judge(a, len, x))  
    {  
        cout<<"Yes"<<endl;  
    }  
    else  
    {  
        cout<<"No"<<endl;  
    }  
    system("pause");  
    return 0;  
}  

本题解决方法:hash table。

时间复杂度:O(N)

空间复杂度:O(N)


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

int find(int* a, int n);

  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. }    
#include <iostream>  
  
using namespace std;  
  
int find(int *a, int n)  
{  
    int t = a[0];  
    int count = 0;  
    for (int i=0; i<n; ++i)  
    {  
        if (count == 0)  
        {  
            t = a[i];  
            count = 1;  
            continue;  
        }  
        else  
        {  
            if (a[i] == t)  
            {  
                count++;  
            }  
            else  
            {  
                count--;  
            }  
        }  
    }  
  
    return t;  
}  
  
int main()  
{  
    int n = 10;  
    int a[10] = {1, 3, 2, 3, 3, 4, 3, 3, 3, 6};  
  
    cout<<find(a, n)<<endl;  
  
    system("pause");  
    return 0;  
}  

Time Complexity: O(n)

Space Complexity:O(1)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值