java 求解二维数组列最小值

比较二维数组列最小值,组成一个新数组返回。

实现核心算法,不需要使用IO

输入:{{5,6,1,16},{7,3,9}}

输出:{1,3}

import java.util.Arrays;

public class Col {

  public static int[] getColMin(int a[][]) {
    int[] res = new int[a.length];
    for (int i = 0; i < a.length; i++) {
      int[] s = a[i];
      Arrays.sort(s);
      res[i] = s[0];
    }
    return res;
  }

  public static void main(String args[]) {
    // 写测试方法
    int[][] a = { { 5, 6, 1, 16 }, { 7, 3, 9 }, { 2, 4, 56 } };
    int[] ss = getColMin(a);
    for (int i = 0; i < ss.length; i++) {
      System.out.print(ss[i] + " ");
    }
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

02-02 14:31
查看更多