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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数组面试

發布時間:2023/12/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数组面试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.數組求和

如果只是普通求和會簡單,但是只能使用一行代碼該怎么辦呢?

int sum(int *a,int n) {return n ==0 ? 0 : sum(a,n-1) + a[n-1]; }

2.尋找發帖水王

int Find(int *a,int n) {int times = 0;int value;for (int i = 0;i < n;++i){if (times == 0){value = a[i];times = 1;}else{if (a[i] == value){times++;}elsetimes--;}}return value; }int main() {int a[10] = {1,1,1,7,8,1,1,1,2,4};cout <<Find(a,10) <<endl;return 0; }

?3.求最大值和最小值

void arrmax(int *a,int n) {max = 0;min = 0;for (int i = 0;i < n;++i){if (a[i] >max){max = a[i];}if (a[i] < min){min = a[i];}} }

?4.最大值和次大值

void arrmax(int *a,int n) {max = 0;min = 0;for (int i = 0;i < n;++i){if (a[i] >max){max = a[i];}if (a[i] < min){min = a[i];}} }

?5.求數組中最短兩個元素之間的距離

因為最短距離只可能是相鄰元素之間的距離,所以轉化為排序問題,時間復雜度O(N*logN);

6.找出兩個數組的共同元素

void arrmax(int *a,int *b,int n) {int i = 0;int j = 0;while(i <n && j <n){if (a[i] < b[j]){i++;}else if (a[i] == b[j]){cout << a[i] <<endl;i++;j++;}else{j++;}} }int main() {int a[5] = {0,1,2,3,4};int b[5] = {1,3,5,7,9};arrmax(a,b,5);return 0; }

7.找出數組中唯一重復的元素

給定含有1001個元素的數組,其中存放了1-1000之內的整數,只有一個整數是重復的,請找出這個數

先對這個數組進行求和,然后減去1到1000的和。

8.找出出現奇數次的數

給定一個含有n個元素的整型數組a,其中只有一個元素出現奇數次,找出這個元素?

因為對于任意一個數k,有k ^ k = 0,k ^ 0 = k,所以將a中所有元素進行異或,那么個數為偶數的元素異或后都變成了0,只留下了個數為奇數的那個元素。

int findOdd(int *a,int n) {int r = a[0];for (int i = 1;i < n;++i){r^= a[i];}return r; }int main() {int a[11] = {1,1,4,3,4,6,8,8,9,3,6};cout << findOdd(a,11)<<endl;return 0; }

?9.找出數組中給定值的數對

有兩種情況,一種是經過排序的,一種是沒有經過排序的

沒有經過排序的需要用一個額外的數組做標記。然后對判斷下標進行操作。這里的下標就是數組里的數。

void findSum(int *a,int n,int sum) {int ahead = n -1;int behind = 0;int curSum = 0;while(behind < ahead){curSum = a[ahead] + a[behind];if (curSum == sum){cout << a[ahead] << "," <<a[behind] <<endl;break;}else if (curSum > sum){ahead--;}else{behind++;}} } int main() {int a[10] = {1,2,4,6,8,9,12,13,15,19};findSum(a,10,18);return 0; }

?10.找出數組最大字段和

int findSum(int *a,int n) {int cur = 0;int sum = 0;for (int i = 0;i < n;++i){cur = a[i] +cur;if (cur < 0){cur = 0;}if (cur > sum){sum = cur;}}return sum; } int main() {int a[8] = {1,-2,3,10,-4,7,2,-5};cout << findSum(a,8);return 0; } 11.數組最大字段乘積 // 子數組的最大乘積 int MaxProduct(int *a, int n) {int maxProduct = 1; // max positive product at current positionint minProduct = 1; // min negative product at current positionint r = 1; // result, max multiplication totallyfor (int i = 0; i < n; i++){if (a[i] > 0){maxProduct *= a[i];minProduct = min(minProduct * a[i], 1);}else if (a[i] == 0){maxProduct = 1;minProduct = 1;}else // a[i] < 0{int temp = maxProduct;maxProduct = max(minProduct * a[i], 1);minProduct = temp * a[i];}r = max(r, maxProduct);}return r; }

?

轉載于:https://www.cnblogs.com/liuweilinlin/p/3282346.html

總結

以上是生活随笔為你收集整理的数组面试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。