Compare commits

...

2 Commits

Author SHA1 Message Date
li_chx d7f5b98838
2906. 构造乘积矩阵 2026-03-24 11:10:10 +08:00
li_chx f1f47604f8
1594. 矩阵的最大非负积 2026-03-23 09:31:50 +08:00
1 changed files with 32 additions and 31 deletions

View File

@ -1,43 +1,44 @@
struct Solution; struct Solution;
mod arr; mod arr;
impl Solution { impl Solution {
fn is_same(mat: &Vec<Vec<i32>>, target: &Vec<Vec<i32>>) -> bool { fn get_product(a: i32, b: i32) -> i32 {
for i in 0..mat.len() { ((a as i64 * b as i64) % 12345) as i32
for j in 0..mat[i].len() { }
if mat[i][j] != target[i][j] { pub fn construct_product_matrix(mut grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
return false; let mut arr = vec![0; grid.len() * grid[0].len()];
let mut ans = vec![0; grid.len() * grid[0].len()];
for i in 0..grid.len() {
for j in 0..grid[0].len() {
arr[i * grid[0].len() + j] = grid[i][j];
} }
} }
ans[0] = arr[0];
for i in 1..arr.len() {
ans[i] = Solution::get_product(arr[i], ans[i - 1]);
} }
true let mut last = arr[ans.len() - 1];
for i in (1..arr.len()-1).rev() {
let x = arr[i];
arr[i] = Solution::get_product(ans[i-1], last);
last = Solution::get_product(last, x)
} }
pub fn find_rotation(mat: Vec<Vec<i32>>, target: Vec<Vec<i32>>) -> bool { arr[0] = last % 12345;
let mut v = vec![vec![0; mat[0].len()]; mat.len()]; arr[ans.len() - 1] = ans[ans.len() - 2] % 12345;
if Solution::is_same(&mat, &target) { for i in 0..grid.len() {
return true; for j in 0..grid[0].len() {
} grid[i][j] = arr[i * grid[0].len() + j];
for x in 1..4 {
for i in 0..mat.len() {
for j in 0..mat[i].len() {
let (mut py, mut px) = (i, j);
for _ in 0..x {
(px, py) = (mat.len() - 1 - py, px)
}
v[py][px] = mat[i][j];
} }
} }
if Self::is_same(&v, &target) { grid
return true;
}
}
false
} }
} }
fn main() { fn main() {
let result = Solution::find_rotation( let result = Solution::construct_product_matrix(arr::make_matrix(r#"[[1,2],[3,4]]"#));
arr::make_matrix(r#"[[1,0,0],[1,0,1],[0,0,1]]"#),
arr::make_matrix(r#"[[0,1,1],[0,0,0],[1,1,0]]"#),
);
println!("{:?}", result); println!("{:?}", result);
} }
/*
[[1,4,4,0]
,[-2,0,0,1]
,[1,-1,1,1]]
*/