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