2012-04-01 61 views
0

我正在處理返回兩個數組中最大數的程序。下面是一些期望:Java - 返回兩個數組中的最大元素

maxInCommon({1, 2, 3, 4, 5, 6, 7, 8}, {-1, -2, 9, 5}) → 5 
maxInCommon({1}, {}) → 0 
maxInCommon({-1, -2}, {-2, -3, -4}) → -2 

我的代碼工作在很多情況下,但不是這一個:

asn9_maxInCommon({-10, -11}, {-10, -11, -12}) → -10 

而不是產生「-10」的,我的程序返回「-11」。以下是我的更新代碼:

public int maxInCommon(int[] nums1, int[] nums2) { 
    int numInCommon = 0; 

    Arrays.sort(nums1); 
    Arrays.sort(nums2); 

    ArrayList<Integer> nums1List = new ArrayList(); 
    ArrayList<Integer> nums2List = new ArrayList(); 

    int number = 0; 
    int number1 = 0; 

    for (int a = 0; a < nums1.length; a++) 
    { 
    number = nums1[a]; 
    nums1List.add(number); 
    } 

    for (int b = 0; b < nums2.length; b++) 
    { 
    number1 = nums2[b]; 
    nums2List.add(number1); 
    } 

    for (int c = (nums1List.size() - 1); c > - 1; c--) 
    { 
    for (int d = (nums2List.size() - 1); d > -1; d--) 
     { 
      if (nums1List.get(c) == nums2List.get(d)) 
       numInCommon = nums1List.get(c); 
     } 
    } 
    return numInCommon; 
} 

我認爲代碼的邏輯應該沒問題,但它仍然不起作用。邏輯失敗的一些其他情況是:

maxInCommon({0,2}, {0,1,2}) → 2 

我的代碼產生「0」代替。

maxInCommon({1, 2, 3, 4, 5, 6, 7, 8}, {-1, -2, 9, 6, 7, 8, 9}) → 8 

我的代碼產生「6」代替。
ArrayLists的目的是以某種方式在每次比較後刪除一個元素。我沒有在這裏使用過這個功能。

+0

你應該改變'numInCommon'只有在實際的共同價值高於最後的共同價值大發現 – 2012-04-01 05:43:58

+0

沒錯。只需添加一個檢查來查看是否max1 == nums2 [b]並且該max1> numInCommon。 – cjm 2012-04-01 05:49:20

+0

謝謝大家,我會分別研究你的每一個建議。 – 2012-04-01 06:24:40

回答

0

如果你的代碼實際工作,你可以改變這一部分:

if (max1 == nums2[b]) 
    numInCommon = max1; 

if (max1 == nums2[b] && numInCommon < max1) { 
    numInCommon = max1; 
} 

應該這樣做

0

的問題是,你在外部formax1 = nums1[a];循環,然後簡單地將max1指定爲numInCommon如果max1包含在secon中d數組。 max1總是會成爲第一個數組中的最後一個元素,緊隨其後的是if語句是毫無意義的。

因此,將-11作爲最大數值的原因是因爲-11是第一個數組中的最後一個元素,它也在第二個數組中 - 這就是您的函數計算的內容。

如果max1大於最大共同數字,您應該進行比較,並從外部for循環中刪除不必要的if語句。 max1的一個更好的名稱是temp,如果您只能從for內部循環訪問nums1[a],則這是一個不必要的變量。

編輯:沒有看到作業標籤,嘗試使用上述信息自己對代碼進行更改。

你也應該有一些方法來表明兩個數組之間沒有共同的數字。

+0

嗨,我測試了你的代碼,這是測試用例:[link](http://farm8.staticflickr.com/7244/7034145105_1583017045_z.jpg)。 – 2012-04-01 06:00:09

+0

我想知道如果我能找到第一個數組中的最大數字,比較它,然後將其設置爲0,並重復相同的過程,從而找到第二個最大的數字進行比較等等。 – 2012-04-01 06:03:30

+0

@VũChâu:這是因爲'numInCommon'被初始化爲0和0> 2.您應該將numInCommon初始化爲最小int值,或者更好地使用'boolean'來表示是否找到了共同的數字。我沒有修復代碼中的錯誤,因爲我只是意識到這個問題被標記爲家庭作業。 – AusCBloke 2012-04-01 06:04:45

-1

現在它的工作正常。

package test; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

public class test { 

    public static void main(String[] args) { 


     int a[] = {-1,-2}; 
     int b[] = {-2,-3,-4}; 

     try{ 
      System.out.println(getMaxCommon(a,b)); 
     }catch(Exception e){ 
      System.out.println("Not found common no"); 
     } 
    } 

    public static int getMaxCommon(int[] arr1,int[] arr2) { 

     int arr1Temp[] = arr1.clone(); 
     int arr2Temp[] = arr2.clone(); 

     Arrays.sort(arr1Temp); 
     Arrays.sort(arr2Temp); 

     List<Integer> list1 = new ArrayList<Integer>(); 
     List<Integer> list2 = new ArrayList<Integer>(); 

     for(int i=0;i<arr1Temp.length;i++){ 
      list1.add(arr1Temp[i]); 
     } 

     for(int i=0;i<arr2Temp.length;i++){ 
      list2.add(arr2Temp[i]); 
     } 


     if(arr1Temp.length < arr2Temp.length){ 
      for(int i=arr1Temp.length-1;i>=0;i--){ 
       if(list2.contains(arr1Temp[i])){ 
        return arr1Temp[i]; 
       } 
      } 
     }else{ 
      for(int i=arr2Temp.length-1;i>=0;i--){ 
       System.out.println(); 
       if(list1.contains(arr2Temp[i])){ 
        return arr2Temp[i]; 
       } 
      } 
     } 

     return (Integer) null; 
    } 
} 
+0

他希望兩個數組的交集最大值,而不是一個數組的最大值。 – AusCBloke 2012-04-01 05:54:29

+1

所以如果我有'{4,1}'和'{3,2,1}',返回值爲4和3,那麼我怎樣才能算出共有的最大數爲1? – AusCBloke 2012-04-01 06:00:59

1

了以下工作:

import java.util.Arrays; 

public class Main { 

    public static void main(String[] args) { 
     int[] array1 = new int[]{1, 2, 3, 4, 5, 6, 7, 8}; 
     int[] array2 = new int[]{-1, -2, 9, 5}; 
     System.out.println(getCommonMax(array1, array2)); 
    } 

    static int getCommonMax(int[] array1, int[] array2) { 
     int commonMax = 0; 
     Arrays.sort(array1); 
     for(int i = array1.length -1; i > -1; i--) { 
      if(contains(array2, array1[i])) { 
       commonMax = array1[i]; 
       break; 
      } 
     } 
     return commonMax; 
    } 

    static boolean contains(int[] array, int i) { 
     boolean contains = false; 
     for(int i2 : array) { 
      if(i2 == i) { 
       contains = true; 
       break; 
      } 
     } 
     return contains; 
    } 

} 
+0

爲了改善你的代碼,你可以遍歷數組從右到左,所以找到的第一個元素是最大公共數(數組從最小到最大) – 2012-04-01 06:23:41

+0

但我從右向左遍歷數組;我沒有按降序排序,因爲升序是默認的,這使得OP – Tom 2012-04-01 06:40:45

+0

更容易耶沒有讀好代碼,對不起:) – 2012-04-01 06:42:24