2014-03-06 33 views
0

我如何可能的辦法解決出現以下問題:查找當前號碼至少有兩個三個數組

創建包含在至少兩個指定數組的整數數組。

例如:

int[] a1 = new int[] { 1, 2, 3, 4, 5 };   
int[] a2 = new int[] { 5, 10, 11, 8 };   
int[] a3 = new int[] { 1, 7, 6, 4, 5, 3, 11 }; 

必須給結果數組

int[] result = new int[] {1, 3, 4, 5, 11} 

P.S.我感興趣的建議,就如何我可能會接近這個(「算法」)的,而不是Java的utils的可能給我答案

回答

3
  1. a1號在Map<Integer,Integer> count,使用值作爲重點,並設置計數到1
  2. a2數字放入同一張地圖。如果項目不存在,則分配計數1,否則將其分配給現有計數+ 1
  3. a3數字放入相同的地圖。如果項目不存在,則分配計數1,否則將其分配給現有計數+ 1
  4. 瀏覽地圖中的條目,並輸出其中值大於1的所有鍵。

該算法在三個數組中的元素組合數中分攤線性時間。

如果三個數組中的數字限制爲1000或另一個相對較小的數字,則可以避免使用集合,但根據數字的上限使用可能更昂貴的算法:替換映射與陣列counts[MAX_NUM+1],然後運行相同的算法,如下所示:

int[] counts = new int[MAX_NUM+1]; 
for (int a : a1) counts[a]++; 
for (int a : a2) counts[a]++; 
for (int a : a3) counts[a]++; 
for (int i = 0 ; i != MAX_NUM+1 ; i++) { 
    if (counts[i] > 1) { 
     System.out.println(i); 
    } 
} 
+0

我認爲他想避免使用集合庫 – ucsunil

+0

我猜這個答案會超出原始問題的學習曲線。換句話說,「地圖」一章尚未達成。 – WonderWorld

+0

@Sunil我不確定這一點 - 他說他正在尋找一種不使用Java的utils的算法,但他對收集沒有提及。不過,我會用基於數組的解決方案進行擴展。 – dasblinkenlight

0

爲此而不集合將是從數組中取的元素的唯一方式,迭代的其餘兩個陣列,以查看是否超過發現重複(然後中斷並移動到下一個元素)。您需要爲三個陣列中的兩個陣列執行此操作,因爲在您移至第三個陣列時,您已經得到了答案。

0

我喜歡繪製維恩圖。你知道有三個相交圓的圖,例如見here

然後您會看到補碼更容易描述: 那些只存在於一個數組中的元素並不令人感興趣。

因此,您可以在散列圖中建立一個頻率列表(即key = element,value =您在第一次找到多少個數組的數量),然後在最後一遍中選擇所有元素髮生不止一次。

爲了簡單起見,我使用了集合。如果您的數組包含相同值的多個條目,則在構建頻率列表時必須忽略這些額外的出現。

3

你可以看看3個數組作爲sets,並找到某些對集合的intersection中的每個元素。

基本上,你正在尋找(set1 [intersection] set2) [union] (set2 [intersection] set3) [union] (set1 [intersection] set2)

我同意它可能不是達到你所追求的最簡單的方式,但能夠減少一個問題到另一個是一種技術,每一個程序員應該掌握,而這個解決方案應該是非常有教育意識的

+0

輝煌的解決方案 –

0

數學上,這可以解決如下:

可以使用每三個陣列的構造三套,所以每個陣列中重複的條目將在每一組只出現一次。然後至少在三組中出現的條目是解決方案。因此,他們被

(S_1 intersect S_2) union (S_2 intersect S_3) union (S_3 intersect S_1) 
0

給出想想這個問題,不同的策略,你可以使用:

  1. 遍歷每個陣列中的每個條目,如果該條目是不是已經在「重複」的結果,然後查看該條目是否在每個剩餘陣列中。如果是,返回下一個整數
  2. 通過從每個數組中添加一個條目(如果它已經存在,將其放入重複數組中)創建一個非重複數組。
  3. 使用自己的
0

做法的另一個創意策略可能是這樣的: 1.Sort所有陣列。 2.對於陣列的每個組合,請執行以下操作:

讓我們考慮前兩個陣列A,B。讓a成爲A的大小。 另外採取的第三陣列或向量來存儲我們的結果

for i=0-->a-1 { 

    Search for A[i] in B using binarySearch. 

    if A[i] exists in B then insert A[i] into our result vector 

    } 

重複用於相同的過程(B,C)和(C,A)。

現在排序&遍歷從最終的結果矢量,除去具有屬性

 result[i] = result[i-1] 

最終的載體是所需的結果的元素。

時間複雜度分析:

T(n) = O(nlog(n)) for Sorting where n is the highest array size among the given three 

    For searching each element of an array in other sorted array T(n) = n * O(log n) 

    T(n) = O(n (log n)) for sorting the result and O(n) for traversing 

    So overall time complexity is O(n log(n)); and space complexity is O(n) 

請糾正我的,我錯了

0

在Java:

會寫一個不使用java.utils不久。

與此同時使用java.utils的溶液:在最後for循環

public static void twice(int[] a, int[] b, int[] c) { 

    //Used Set to remove duplicates 
    Set<Integer> setA = new HashSet<Integer>(); 
    for (int i = 0; i < a.length; i++) { 
     setA.add(a[i]); 
    } 

    Set<Integer> setB = new HashSet<Integer>(); 
    for (int i = 0; i < b.length; i++) { 
     setB.add(b[i]); 
    } 

    Set<Integer> setC = new HashSet<Integer>(); 
    for (int i = 0; i < c.length; i++) { 
     setC.add(c[i]); 
    } 

    //Logic to fill data into a Map 
    Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 

    for (Integer val : setA) { 
     map.put(val, 1); 
    } 

    for (Integer val : setB) { 
     if (map.get(val) != null) { 
      int count = map.get(val); 
      count++; 
      map.put(val, count); 
     } else { 
      map.put(val, 1); 
     } 
    } 

    for (Integer val : setC) { 
     if (map.get(val) != null) { 
      int count = map.get(val); 
      count++; 
      map.put(val, count); 
     } else { 
      map.put(val, 1); 
     } 
    } 

    for (Map.Entry<Integer, Integer> entry2 : map.entrySet()) { 

     //if (entry2.getValue() == 2) { //Return the elements that are present in two out of three arrays. 
     if(entry2.getValue() >= 2) {  //Return elements that are present **at least** twice in the three arrays. 
      System.out.print(" " + entry2.getKey()); 
     } 
    } 
} 

改變條件的情況下,一個需要返回存在於三分之二的陣列的元素。你說:

int[] a = { 2, 3, 8, 4, 1, 9, 8 }; 
int[] b = { 6, 5, 3, 7, 9, 2, 1 }; 
int[] c = { 5, 1, 8, 2, 4, 0, 5 }; 

Output: { 3, 8, 4, 5, 9 } 
0

這裏不用任何的java.util庫:

public static void twice(int[] a, int[] b, int[] c) { 
    int[] a1 = removeDuplicates(a); 
    int[] b1 = removeDuplicates(b); 
    int[] c1 = removeDuplicates(c); 

    int totalLen = a1.length + b1.length +c1.length; 
    int[][] keyValue = new int[totalLen][2]; 

    int index = 0; 
    for(int i=0; i<a1.length; i++, index++) 
    { 
     keyValue[index][0] = a1[i]; //Key 
     keyValue[index][1] = 1;  //Value 
    } 

    for(int i=0; i<b1.length; i++) 
    { 
     boolean found = false; 
     int tempIndex = -1; 
     for(int j=0; j<index; j++) 
     { 
      if (keyValue[j][0] == b1[i]) { 
       found = true; 
       tempIndex = j; 
       break; 
      } 
     } 

     if(found){ 
      keyValue[tempIndex][1]++; 
     } else { 
      keyValue[index][0] = b1[i]; //Key 
      keyValue[index][1] = 1;  //Value 
      index++; 
     } 
    } 

    for(int i=0; i<c1.length; i++) 
    { 
     boolean found = false; 
     int tempIndex = -1; 
     for(int j=0; j<index; j++) 
     { 
      if (keyValue[j][0] == c1[i]) { 
       found = true; 
       tempIndex = j; 
       break; 
      } 
     } 

     if(found){ 
      keyValue[tempIndex][1]++; 
     } else { 
      keyValue[index][0] = c1[i]; //Key 
      keyValue[index][1] = 1;  //Value 
      index++; 
     } 
    } 

    for(int i=0; i<index; i++) 
    { 
     //if(keyValue[i][1] == 2) 
     if(keyValue[i][1] >= 2) 
     { 
      System.out.print(keyValue[i][0]+" "); 
     } 
    } 
} 

public static int[] removeDuplicates(int[] input) { 
    boolean[] dupInfo = new boolean[500];//Array should not have any value greater than 499. 
    int totalItems = 0; 

    for(int i = 0; i < input.length; ++i) { 
     if(dupInfo[input[i]] == false) { 
      dupInfo[input[i]] = true; 
      totalItems++; 
     } 
    } 

    int[] output = new int[totalItems]; 
    int j = 0; 
    for(int i = 0; i < dupInfo.length; ++i) { 
     if(dupInfo[i] == true) { 
      output[j++] = i; 
     } 
    } 
    return output; 
} 
0

這是非常簡單的,並且可以做了N個不同的陣列以同樣的方式:

public static void compute(int[] a1, int[] a2, int[] a3) { 
     HashMap<Integer, Integer> map = new HashMap<>(); 
     fillMap(map, a1); 
     fillMap(map, a2); 
     fillMap(map, a3); 

     for (Integer key : map.keySet()) { 
      System.out.print(map.get(key) > 1 ? key + ", " : ""); 
     } 
    } 

    public static void fillMap(HashMap<Integer, Integer> map, int[] a) { 
     for (int i : a) { 
      if (map.get(i) == null) { 
       map.put(i, 1); 
       continue; 
      } 
      int count = map.get(i); 
      map.put(i, ++count); 
     } 
    } 
+0

對基於重複的數組無效! – bhuvin

相關問題