|
|
|
|
# 1605. 给定行和列的和求可行矩阵
|
|
|
|
|
|
|
|
|
|
### description:
|
|
|
|
|
|
|
|
|
|
给你两个非负整数数组 `rowSum` 和 `colSum` ,其中 `rowSum[i]` 是二维矩阵中第 `i` 行元素的和, `colSum[j]` 是第 `j` 列元素的和。换言之你不知道矩阵里的每个元素,但是你知道每一行和每一列的和。请找到大小为 `rowSum.length x colSum.length` 的任意 **非负整数** 矩阵,且该矩阵满足 `rowSum` 和 `colSum` 的要求。请你返回任意一个满足题目要求的二维矩阵,题目保证存在 **至少一个** 可行矩阵。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
代码: restore_matrix为正确写法。
|
|
|
|
|
|
|
|
|
|
````rust
|
|
|
|
|
use std::cmp;
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let mut row_sum:Vec<i32> = vec![5, 7, 10];
|
|
|
|
|
let mut col_sum = vec![8, 6, 8];
|
|
|
|
|
// let matrix = restore_matrix(&mut row_sum.clone(), &mut col_sum);
|
|
|
|
|
let matrix = restore_matrix2(row_sum.clone(), col_sum); // col_sum 失去所有权,下面无法使用。row_sum可以使用。
|
|
|
|
|
for i in matrix{
|
|
|
|
|
for j in i{
|
|
|
|
|
print!("{} ", j);
|
|
|
|
|
}
|
|
|
|
|
println!();
|
|
|
|
|
}
|
|
|
|
|
println!(" - - - - - ");
|
|
|
|
|
for &i in &row_sum{
|
|
|
|
|
print!("{} ", i);
|
|
|
|
|
// print!("{} ", i==0); //用于判断类型。是&i32还是i32
|
|
|
|
|
}
|
|
|
|
|
// println!();
|
|
|
|
|
// for &i in &col_sum{
|
|
|
|
|
// print!("{} ", i);
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn restore_matrix(row_sum:&mut [i32], col_sum:&mut [i32]) -> Vec<Vec<i32>> {
|
|
|
|
|
// let (n, m) = (row_sum.len() as i32, col_sum.len() as i32); // len()返回usize
|
|
|
|
|
// let (i, j) = (0, 0);
|
|
|
|
|
|
|
|
|
|
let (n, m) = (row_sum.len(), col_sum.len()); // len()返回usize
|
|
|
|
|
let (mut i, mut j) = (0usize, 0usize);
|
|
|
|
|
let mut matrix = vec![vec![0; m]; n]; //这里正好需要usize
|
|
|
|
|
while i < n && j < m {
|
|
|
|
|
let v = cmp::min(row_sum[i], col_sum[j]);
|
|
|
|
|
matrix[i][j] = v;
|
|
|
|
|
row_sum[i] -= v;
|
|
|
|
|
col_sum[j] -= v;
|
|
|
|
|
if 0==row_sum[i] {
|
|
|
|
|
i+=1;
|
|
|
|
|
}
|
|
|
|
|
if 0==col_sum[j] {
|
|
|
|
|
j+=1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
matrix
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn restore_matrix2(mut row_sum: Vec<i32>, mut col_sum: Vec<i32>) -> Vec<Vec<i32>> {
|
|
|
|
|
// let (n, m) = (row_sum.len() as i32, col_sum.len() as i32); // len()返回usize
|
|
|
|
|
// let (i, j) = (0, 0);
|
|
|
|
|
|
|
|
|
|
let (n, m) = (row_sum.len(), col_sum.len()); // len()返回usize
|
|
|
|
|
let (mut i, mut j) = (0usize, 0usize);
|
|
|
|
|
let mut matrix = vec![vec![0; m]; n]; //这里正好需要usize
|
|
|
|
|
while i < n && j < m {
|
|
|
|
|
let v = cmp::min(row_sum[i], col_sum[j]);
|
|
|
|
|
matrix[i][j] = v;
|
|
|
|
|
row_sum[i] -= v;
|
|
|
|
|
col_sum[j] -= v;
|
|
|
|
|
if 0==row_sum[i] {
|
|
|
|
|
i+=1;
|
|
|
|
|
}
|
|
|
|
|
if 0==col_sum[j] {
|
|
|
|
|
j+=1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
matrix
|
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
图:
|
|
|
|
|
|
|
|
|
|
![image-20230314113447242](C:\Users\10074\AppData\Roaming\Typora\typora-user-images\image-20230314113447242.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c++代码:
|
|
|
|
|
|
|
|
|
|
```c++
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
class Solution{
|
|
|
|
|
public:
|
|
|
|
|
std::vector<std::vector<int>> restoreMatrix(std::vector<int>& rowSum, std::vector<int>& colSum){
|
|
|
|
|
int n = rowSum.size(), m = colSum.size();
|
|
|
|
|
std::cout << n <<std::endl;
|
|
|
|
|
std::cout << m <<std::endl;
|
|
|
|
|
std::cout << "- - -" <<std::endl;
|
|
|
|
|
std::vector<std::vector<int>> matrix(n, std::vector<int>(m, 0));
|
|
|
|
|
int i = 0, j = 0;
|
|
|
|
|
while (i < n && j < m){
|
|
|
|
|
int v = std::min(rowSum[i], colSum[j]);
|
|
|
|
|
matrix[i][j] = v;
|
|
|
|
|
rowSum[i] -= v;
|
|
|
|
|
colSum[j] -= v;
|
|
|
|
|
if(0==rowSum[i]){
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
if(0==colSum[j]){
|
|
|
|
|
++j;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return matrix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
int main() {
|
|
|
|
|
std::vector<int> rowSum;
|
|
|
|
|
std::vector<int> colSum;
|
|
|
|
|
rowSum.push_back(5); rowSum.push_back(7); rowSum.push_back(10);
|
|
|
|
|
colSum.push_back(8); colSum.push_back(6); colSum.push_back(8);
|
|
|
|
|
Solution *so = new Solution();
|
|
|
|
|
std::vector<std::vector<int>> res = so ->restoreMatrix(rowSum, colSum);
|
|
|
|
|
|
|
|
|
|
std::vector<int> vecTmp;
|
|
|
|
|
for(std::vector<std::vector<int>>::iterator iter = res.begin(); iter != res.end(); iter++){
|
|
|
|
|
vecTmp = *iter;
|
|
|
|
|
for(std::vector<int>::iterator it = vecTmp.begin(); it != vecTmp.end(); it++){
|
|
|
|
|
std::cout<< *it << " ";
|
|
|
|
|
}
|
|
|
|
|
std::cout<< std::endl;
|
|
|
|
|
}
|
|
|
|
|
std::cout << " - - - " <<std::endl;
|
|
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
|
for (i = 0; i < res.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
for(j = 0; j < res[0].size(); j++)
|
|
|
|
|
std::cout << res[i][j] << " ";
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|