2012-04-01 62 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