首页 > 编程语言 > 详细

华为机试—二维数组列最小值

时间:2015-01-11 14:53:10      阅读:390      评论:0      收藏:0      [点我收藏+]

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

实现核心算法,不需要使用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] + " ");
		}
	}
}

技术分享


华为机试—二维数组列最小值

原文:http://blog.csdn.net/wtyvhreal/article/details/42610627

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!