835. 图像重叠
This commit is contained in:
+28
-8
@@ -1,22 +1,42 @@
|
|||||||
|
use crate::arr::make_matrix;
|
||||||
|
|
||||||
mod arr;
|
mod arr;
|
||||||
mod list;
|
mod list;
|
||||||
|
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn is_rectangle_overlap(rec1: Vec<i32>, rec2: Vec<i32>) -> bool {
|
fn check(img1: &Vec<Vec<i32>>, img2: &Vec<Vec<i32>>, dx: i32, dy: i32) -> i32 {
|
||||||
// [1] -- [3], [1] -- [3]
|
let (length, width) = (img2.len() as i32, img2[0].len() as i32);
|
||||||
let a = !(rec1[2] <= rec2[0] || rec2[2] <= rec1[0]);
|
let mut ans= 0;
|
||||||
let b = !(rec1[1] >= rec2[3] || rec2[1] >= rec1[3]);
|
for i in 0..length {
|
||||||
if a && b {
|
for j in 0..width {
|
||||||
return true;
|
let x = i + dx;
|
||||||
|
let y = j + dy;
|
||||||
|
if x < 0 || y < 0 || x >= length || y>= width {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
false
|
if img1[x as usize][y as usize] == 1 && img2[i as usize][j as usize] == 1 {
|
||||||
|
ans += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ans
|
||||||
|
}
|
||||||
|
pub fn largest_overlap(img1: Vec<Vec<i32>>, img2: Vec<Vec<i32>>) -> i32 {
|
||||||
|
let (len, width) = (img1.len() as i32, img1[0].len() as i32);
|
||||||
|
let mut ans = 0;
|
||||||
|
for i in -len + 1..len {
|
||||||
|
for j in -width + 1..width {
|
||||||
|
ans = ans.max(Self::check(&img1, &img2, i, j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ans
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!(
|
println!(
|
||||||
"{:?}",
|
"{:?}",
|
||||||
Solution::is_rectangle_overlap(vec![0, 0, 2, 2], vec![1, 1, 3, 3])
|
Solution::largest_overlap(make_matrix("[[1,1,0],[0,1,0],[0,1,0]]"), make_matrix("[[0,0,0],[1,0,1],[0,0,1]]"))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user