Find Valid Matrix Given Row and Column Sums
给rowSum和colSum的数组,求原矩阵.
这个题主要是因为原来矩阵肯定存在(已知), 然后并且要求填入非负数, 所以我们可以在rowSum和colSum中,找到当前位置的最小值min, 然后减一下即可.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Solution { public int[][] restoreMatrix(int[] rowSum, int[] colSum) { int n = rowSum.length; int m = colSum.length; int[][] res = new int[n][m]; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++){ int min = Math.min(rowSum[i], colSum[j]); res[i][j] = min; rowSum[i] -= min; colSum[j] -= min; } } return res; } } |