日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

面试题之数组统计

發(fā)布時(shí)間:2025/4/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 面试题之数组统计 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目:給定數(shù)組A,大小為n,數(shù)組元素為0到n-1的數(shù)字,不過(guò)有的數(shù)字出現(xiàn)了多次,有的數(shù)字沒(méi)有出現(xiàn)。請(qǐng)給出算法和程序,統(tǒng)計(jì)哪些數(shù)字沒(méi)有出現(xiàn),哪些數(shù)字出現(xiàn)了多少次。要求在O(n)的時(shí)間復(fù)雜度,O(1)的空間復(fù)雜度下完成。

?

解法一:

直接用兩層遍歷,O(n^2)的時(shí)間復(fù)雜度,O(1)的空間復(fù)雜度

#include <stdio.h> #include <stdlib.h>int main() {int n, i, j, count = 0; //n is The length of the Arraywhile (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!\n", i); elseprintf("%d appear in the array for %d times\n", i, count);} } }

?

解法二:
以空間換時(shí)間的辦法,用一個(gè)map數(shù)組來(lái)存放元素的計(jì)數(shù)。時(shí)間復(fù)雜度為O(n),空間復(fù)雜度為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 Arraywhile (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!\n", i); elseprintf("%d appear in the array for %d times\n", i, map[i]);} } }

?

但上述解法都不滿足題目對(duì)時(shí)間復(fù)雜度和空間復(fù)雜度的要求,因此我們想到重復(fù)利用數(shù)組A。

解法三:

????? 三次遍歷數(shù)組的方法:

????????? 第一次遍歷:對(duì)于每一個(gè)A[i] = A[i] * n

????????? 第二次遍歷:對(duì)于每一個(gè)i, A[A[i]/n]++

??????????第三次遍歷:對(duì)于每一個(gè)i,A[i]%n就是i出現(xiàn)的次數(shù)

解釋:A[i]應(yīng)該出現(xiàn)在A中的A[i]位置,乘以n、再除以n,很容易來(lái)回變換;第二次遍歷,對(duì)于A[i]本來(lái)所在的位置不斷增1,但絕不超出n,那么每一個(gè)i出現(xiàn)的次數(shù),就是A[I]對(duì)n取余。

#include <stdio.h> #include <stdlib.h>int main() {int n, i; //n is The length of the Arraywhile (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!\n", i); elseprintf("%d appear in the array for %d times\n", i, a[i]%n);} } }

?

解法四:

??? 兩次遍歷數(shù)組的方法:考慮A[i],現(xiàn)在的位置為i,如果采用A來(lái)計(jì)數(shù),它的位置應(yīng)該是A[i]%n,找到計(jì)數(shù)位置,處理這個(gè)計(jì)數(shù)位置的辦法就是加n.

?????? 第一次遍歷:對(duì)A[i]的計(jì)算位置加n,加n可以保證A[i]%n的是不變的

?????? 第二次遍歷:A數(shù)組,最后每一個(gè)元素表示為A[i] = x + k * n; 其中x<n,并且k就是我們要統(tǒng)計(jì)的頻率

#include <stdio.h> #include <stdlib.h>int main() {int n, i; //n is The length of the Arraywhile (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!\n", i); elseprintf("%d appear in the array for %d times\n", i, a[i]/n);} } }


?

???????

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/james1207/p/3290213.html

總結(jié)

以上是生活随笔為你收集整理的面试题之数组统计的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。