java 求解二維數組列最小值
比較二維數組列最小值,組成一個新數組返回。
實現核心算法,不需要使用IO
輸入:{{5,6,1,16},{7,3,9}}
輸出:{1,3}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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