找出數組中出現次數超過一半的元素

參考地址:http://www.cnblogs.com/DayByDay/p/3871834.html?utm_source=tuicool&utm_medium=referralhtml

題目:找出數組中出現次數超過一半的元素(前提是該元素必定存在)算法

解法1:每次刪除數組中兩個不一樣的元素,刪除後,要查找的那個元素的個數仍然超過刪除後的元素總數的一半數組

解法2:若是數據量小,能夠對數組進行排序,那麼數組中間的數就是出現次數超過一半的數微信

 1 #include <stdio.h>
 2 
 3 int half_number(int a[], int n)
 4 {
 5     if( a == NULL || n <= 0 )
 6         return -1;
 7 
 8     int i, candidate;
 9     int times = 0;
10     for( i=0; i<n; i++ )
11     {
12         if( times == 0 )
13         {
14             candidate = a[i];
15             times = 1;
16         }
17         else if( a[i] == candidate )
18             ++times;
19         else
20             --times;
21     }
22     return candidate;
23 }
24 
25 int main(void)
26 {
27     int a[] = {1,2,3,2,2,2,5,4,2};
28 
29     int result = half_number(a, 9);
30     if( result != -1 )
31         printf("%d\n", result);
32     else
33         printf("Error.\n");
34 
35     return 0;
36 }

 

坑!坑!坑!(騰訊挖的):測試

春節期間小明使用微信收到不少個紅包,很是開心。在查看領取紅包記錄時發現,某個紅包金額出現的次數超過了紅包總數的一半。請幫小明找到該紅包金額。寫出具體算法思路和代碼實現,要求算法儘量高效。ui

給定一個紅包的金額數組gifts及它的大小n,請返回所求紅包的金額。spa

本題的測試案例上包括0的狀況,即紅包中沒有出現相同金額次數超過紅包一半的,那此種狀況應該返回0code

 1 class Gift {
 2 public:
 3     int getValue(vector<int> gifts, int n) {
 4         // write code here
 5         // write code here
 6         int nRet;
 7         int nCnt = 0;
 8         for(int i = 0; i < n; ++i){
 9             if(nCnt == 0){
10                 nRet = gifts[i];
11                 nCnt = 1;   
12             }
13             else if(nRet == gifts[i]){
14                 ++nCnt;
15             }
16             else{
17                 --nCnt;
18             }
19         }
      //最後要作個驗證
20 nCnt = 0; 21 int nLeft = n; 22 for(int i = 0; i < n; ++i){ 23 if(gifts[i] == nRet) 24 ++nCnt; 25 if(nCnt + nLeft < n/2) 26 return 0; 27 if(nCnt > n/2) 28 return nRet; 29 30 --nLeft; 31 } 32 return 0; 33 } 34 };

 

 

 

該題的擴展:數組中有3個元素出現的次數都超過數組元素總數N的1/4, 找出這三個元素htm

解法:同上,可是每次刪除4個互不相同的元素,處理上比上面的稍微麻煩blog

 1 #include <stdio.h>
 2 
 3 void find(int a[], int n)
 4 {
 5     if( a==NULL || n<=3 )
 6     {
 7         printf("Error.\n");
 8         return ;
 9     }
10 
11     int i,j;
12     int times[3] = {0,0,0}; // 3個candidate的計數
13     int candidate[3] = {-1,-1,-1}; // 假設元素不多是-1
14 
15     for( i=0; i<n; i++ )
16     {
17         if( times[0] == 0 && a[i] != candidate[1] && a[i] != candidate[2] ) // 第1個candidate目前空缺, 且當前元素a[i]不等於其餘兩個candidate時, 將該元                                                素做爲新的candidate
18         {
19             candidate[0] = a[i];
20             times[0] = 1;
21         }
22         if( times[1] == 0 && a[i] != candidate[0] && a[i] != candidate[2] )
23         {
24             candidate[1] = a[i];
25             times[1] = 1;
26         }
27         if( times[2] == 0 && a[i] != candidate[1] && a[i] != candidate[0] )
28         {
29             candidate[2] = a[i];
30             times[2] = 1;
31         }
32         else if( a[i] == candidate[0] )
33         {
34             ++times[0];
35         }
36         else if( a[i] == candidate[1] )
37         {
38             ++times[1];
39         }
40         else if( a[i] == candidate[2] )
41         {
42             ++times[2];
43         }
44         else // 刪除4個各不相同的數組元素, 刪除後
45         {
46             --times[0];
47             --times[1];
48             --times[2];
49         }
50     }
51     printf("%d %d %d\n",candidate[0],candidate[1],candidate[2]);
52 }
53 
54 int main(void)
55 {
56     int a[] = {5,1,1,3,8,1,3,1,4,1,7,1,2,9,2,3,2,3,2,3,2,3,2};
57     find(a, 23);
58     return 0;
59 }
相關文章
相關標籤/搜索