2011-09-10 53 views
0

我期待在Java中創建一個函數,它將採用兩個數組或列表,並能夠判斷第一個數組(第一個數組)是否適合第二個數組(目標)。目標數組的值不能超過源數組中的值。一個數組中的所有數字都是<=到另一個數組

例如:

[ 16, 16, 16 ] will not fit into [ 13, 13, 22 ] 

[ 12, 12 ]  will fit into [ 16, 16, 12 ] 

[ 12, 18, 14 ] will not fit into [ 10, 18, 14 ] 

[ 12, 24 ]  will fit into [ 10, 12, 24 ] 

[ 10, 10, 10 ] will not fit into [ 10, 10 ] 

我目前的嘗試(IANA CS專業!)是確定爲3元的陣列,而這一切我需要擔心在短期內,但我錯過了一些內部循環中的邏輯將防止漏報。

要點:https://gist.github.com/1208514

private Boolean designFits(int[] max, int[] design) { 

    Boolean designFits = true; 

    Arrays.sort(max); 
    Arrays.sort(design); 
    int passCount = 0; 

    if(design.length <= max.length) { 

     for(int i = 0; i < max.length; i++) { 

      for(int j = 0; j < design.length; j++) { 

       if(max[i] <= design[j]) { 

        passCount++; 

       } 

      } 

     } 

     if(passCount == 0 || passCount > max.length) { 
      designFits = false; 

     } 


    } else { 
     designFits = false; 

    } 

    return designFits; 

} 
+0

什麼是 「合適」 呢? LHS中的元素是否需要存在於RHS中?如果是的話,這是否意味着LHS必須是RHS的子集? –

回答

2

我想,這達到你在找什麼:

private static boolean designFits(int[] source, int[] target) { 

    //if source is bigger than target, it cannot fit 
    if (source.length > target.length) { 
     return false; 
    } 

    //sort the arrays 
    Arrays.sort(source); 
    Arrays.sort(target); 

    //get the size difference between target and source 
    int targetSizeDiff = target.length - source.length; 

    //walk source: 
    for (int i = 0; i < source.length; i++) { 
     //compare source's value at index i with target's value at i + difference 
     //if it's greater, source cannot fit 
     if (source[i] > target[i + targetSizeDiff]) { 
      return false; 
     } 
    } 

    //at this point we know source can fit 
    return true; 
} 

public static void main(String[] args) { 

    //false 
    System.out.println(designFits(new int[]{16, 16, 16}, new int[]{13, 13, 22})); 

    //true 
    System.out.println(designFits(new int[]{12, 12}, new int[]{16, 16, 12})); 

    //false 
    System.out.println(designFits(new int[]{12, 18, 14}, new int[]{10, 18, 14})); 

    //true 
    System.out.println(designFits(new int[]{12, 24}, new int[]{10, 12, 24})); 

    //false 
    System.out.println(designFits(new int[]{10, 10, 10}, new int[]{10, 10})); 
} 
+0

真棒,這工作完美!謝謝!!! – Brian

0

隨着收藏品,你可以使用containsAll方法是這樣的:

List<Integer> list1 = new ArrayList(); 
list1.add(1); 
list1.add(2); 
list1.add(3); 
list1.add(4); 
list1.add(5); 
list1.add(6); 

List<Integer> list2 = new ArrayList(); 
list2.add(1); 
list2.add(2); 
list2.add(3); 

System.out.println(list1.containsAll(list2)); 
+1

如果我不需要考慮設計[]中的值小於max []中的任何值,那麼這將是完美的。 – Brian

+0

所以,這是否意味着,在你的情況下,{1,2,3}會適合{4,5,6}? – jFrenetic

+0

是的,這是正確的。 – Brian

0

看來要解決的模樣

return first.length <= last.length && max(first) <= min(second) 

哪裏「的問題min「和」max「是返回數組的最小和最大元素的函數。如果我理解正確,應該很容易編碼最小和最大。

+0

不適用於第四個示例。 –

2

對兩個數組排序,然後遍歷源數組,並嘗試將每個元素放入目標數組中。如果元素不適合嘗試目標中的下一個地方。如果您找不到適合的地方,如果您成功地找到適合的所有元素的位置。

0

您可以按降序對降序排列這兩個數組,並比較每一對,如果任何一對比較失敗都可以返回false。

private Boolean designFits(int[] max, int[] design) { 
    if (max.length > design.length) return false; 

    Arrays.sort(max, Collections.reverseOrder()); 
    Arrays.sort(design, Collections.reverseOrder()); 

    for (int i = 0; i < max.length; i++) 
     if (max[i] > design[i]) return false; 

    return true; 
} 
+1

Collections.reverseOrder()不適用於數組原語,但我認爲我可以將其轉換爲ArrayList,然後處理這些數組。我仍然在忙於各種Java的鑄造,並且對於如何在短期內完成這項工作感到困惑。 – Brian

+0

你說得對,它只適用於「Comparable」類型,所以它只有在數組是'Integer'時纔會起作用。無論如何,忽必烈汗的解決方案完全一樣,只是沒有相反的排序。 – guardianpt

相關問題