题目链接:1605. 给定行和列的和求可行矩阵
方法:贪心
解题思路
参考:思路?一个动画秒懂!附优化写法(Python/Java/C++/Go)
代码
class Solution {
public:
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
int m = rowSum.size(), n = colSum.size();
vector<vector<int>> ans(m, vector<int>(n));
for (int i = 0, j = 0; i < m, j < n; ) {
int rs = rowSum[i], cs = colSum[j];
if (rs < cs) {
colSum[j] -= rs;
ans[i ++ ][j] = rs;
} else {
rowSum[i] -=cs;
ans[i][j ++ ] = cs;
}
}
return ans;
}
};
标签:colSum,rs,1605,rowSum,矩阵,行和列,vector,ans,cs
From: https://www.cnblogs.com/lxycoding/p/17299502.html