zoukankan      html  css  js  c++  java
  • 面试题之数组统计

    题目:给定数组A,大小为n,数组元素为0到n-1的数字,不过有的数字出现了多次,有的数字没有出现。请给出算法和程序,统计哪些数字没有出现,哪些数字出现了多少次。要求在O(n)的时间复杂度,O(1)的空间复杂度下完成

     问题的难点在于时间和空间复杂度。O(1)的空间含义,可以使用变量,但不能开辟数组或者map等来计数

    解法一:直接用两层遍历,O(n^2)的时间复杂度,O(1)的空间复杂度。效率太低.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n, i, j, count = 0;     //n is The length of the Array
        while (scanf("%d", &n) != EOF)
        {   
            int *a = malloc(sizeof(int) * n); 
            for (i = 0; i < n; i++)
                scanf("%d", &a[i]);
    
            for (i = 0; i < n; i++)
            {   
                count = 0;
                for (j = 0; j < n; j++)
                {   
                    if (i == a[j])
                    {   
                        count++;
                    }   
                }   
                if (count == 0)
                    printf("%d does not appear in the array!
    ", i); 
                else
                    printf("%d appear in the array for %d times
    ", i, count);
            }   
        }   
    }

    解法而:用map来存储元素和其对于的个数。明显时间复杂度为O(N),空间为O(n),不满足要求。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        int n, i, j, count = 0;     //n is The length of the Array
        while (scanf("%d", &n) != EOF)
        {   
            int *a = malloc(sizeof(int) * n); 
            int *map = malloc(sizeof(int) *n);
            memset(map, 0, sizeof(map));
    
            for (i = 0; i < n; i++)
                scanf("%d", &a[i]);
    
            for (i = 0; i < n; i++)
            {   
                map[a[i]]++;
            }   
    
            for (i = 0; i < n; i++)
            {   
                if (map[i] == 0)
                    printf("%d does not appear in the array!
    ", i); 
                else
                    printf("%d appear in the array for %d times
    ", i, map[i]);
            }   
        }   
    }

    但上述解法都不满足题目对时间复杂度和空间复杂度的要求,因此我们想到重复利用数组A

    解法三:

    我们要注意一个条件:

     数组A,大小为n,数组元素为0到n-1的数字, 元素都不超过n,google有一道面试题就是利用这个特性,大小为n的数组,元素都是

    0-n直接的整数,看是否重复。以前写的:http://www.cnblogs.com/youxin/p/3297788.html).

    这个很重要。

          三次遍历数组的方法:

              第一次遍历:对于每一个A[i] = A[i] * n

              第二次遍历:对于每一个i, A[A[i]/n]++

              第三次遍历:对于每一个i,A[i]%n就是i出现的次数

    解释:A[i]应该出现在A中的A[i]位置,乘以n、再除以n,很容易来回变换;第二次遍历,对于A[i]本来所在的位置不断增1,但绝不超出n,那么每一个i出现的次数,就是A[I]对n取余。 

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n, i;       //n is The length of the Array
        while (scanf("%d", &n) != EOF)
        {   
            int *a = malloc(sizeof(int) * n); 
            for (i = 0; i < n; i++)
                scanf("%d", &a[i]);
    
            for (i = 0; i < n; i++)
                a[i] = a[i] * n;
    
            for (i = 0; i < n; i++)
                a[a[i]/n]++;
    
            for (i = 0; i < n; i++)
            {   
                if (a[i] % n == 0)
                    printf("%d does not appear in the array!
    ", i); 
                else
                    printf("%d appear in the array for %d times
    ", i, a[i]%n);
            }   
        }   
    }

    结果:

    4
    2 3 1 2
    0没有出现
    1 出现次数:1
    2 出现次数:2
    3 出现次数:1


    4
    1 2 2 2
    0没有出现
    1 出现次数:1
    2 出现次数:3
    3没有出现

    分析:假设输入为2 3 1 2

     0 1 2 3

     2 3 1 2 a[i]=a[i]*n后为

     8 12 4 8

     a[a[i]/n]++;操作
    i=0; a[2]=9
    i=1 a[3]=5;
    i=2 a[1]=13
    i=3 a[2]=9+1=10

    a[0]还是原来的8.
    判断次数a[i]%n
    a[0]:8 % 4=0 所以没出现
    a[1]:13%4=1 出现1次
    a[2]:10%8=2 出现2次
    a[3]:5%4=1出现1次

    一开始我很难理解为什么
    要:

     a[i] = a[i] * n;
    接着a[a[i]%n]++;
    而不是a[a[i]]++;
    这是为了后面判断a[i]%n是否为0.


    解法四:

        两次遍历数组的方法:考虑A[i],现在的位置为i,如果采用A来计数,它的位置应该是A[i]%n,找到计数位置,处理这个计数位置的办法就是加n.

           第一次遍历:对A[i]的计算位置加n,加n可以保证A[i]%n的是不变的

           第二次遍历:A数组,最后每一个元素表示为A[i] = x + k * n; 其中x<n,并且k就是我们要统计的频率

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n, i;       //n is The length of the Array
        while (scanf("%d", &n) != EOF)
        {   
            int *a = malloc(sizeof(int) * n); 
            for (i = 0; i < n; i++)
                scanf("%d", &a[i]);
    
            for (i = 0; i < n; i++)
                a[a[i] % n] += n;
    
            for (i = 0; i < n; i++)
            {   
                if (a[i] / n == 0)
                    printf("%d does not appear in the array!
    ", i); 
                else
                    printf("%d appear in the array for %d times
    ", i, a[i]/n);
            }   
        }   
    }

    解法四:

    还有一种两次遍历的方法,也是上面的思路:题目中数组是1到n,为了方便算法考虑,以及数组存储方便,我们考虑0-n-1,结果是相同的。 考虑A[i],现在位置是i,如果采用A来计数,它的位置应该是A[i] % n,找到计数位置,该如何处理这个位置呢?加1么?显然不可以,这里有一个技巧,方法如下:

       第一次遍历:对A[i]的计算位置加n,加n可以保证A[i]%n的是不变的

         第二次遍历:A数组,最后每一个元素表示为A[i] = x + k * n; 其中x<n,并且k就是我们要统计的频率

    上面的思路,转换为代码如下:

    void repeat(int a[],int n)
    {
        int i=0;
        for(i=0;i<n;i++)
            a[a[i]%n] += n;
    
        for(i=0;i<n;i++)
        {
            int frequency=a[i]/n;
            cout<<i<<"-->"<<frequency<<endl;
        }
    }

    分析:输入:

    0 1 2 3 (i序号)

    2 3 1 2

    i=0; a[2]=2+n;

    i=1; a[3]=1+n;

    i=2; a[1]=3+n;

    i=3 a[2]=2+n+n;

    参考:http://blog.csdn.net/buaa_shang/article/details/10516135

  • 相关阅读:
    nginx 日志之 error_log
    ssl 原理简介
    nginx 配置ssl
    自建 ca 及使用 ca 颁发证书
    nginx 访问控制之 认证
    nginx 访问控制之 限速
    nginx 访问控制之 http_referer
    nginx 访问控制之 user_agent
    nginx 访问控制之 request_uri
    nginx 访问控制之 document_uri
  • 原文地址:https://www.cnblogs.com/youxin/p/3349867.html
Copyright © 2011-2022 走看看