-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2352.rs
36 lines (31 loc) · 881 Bytes
/
p2352.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#[test]
fn test() {
let grid1: Vec<Vec<i32>> = vec![vec![3, 2, 1], vec![1, 7, 6], vec![2, 7, 7]];
assert_eq!(equal_pairs(grid1), 1);
let grid2: Vec<Vec<i32>> = vec![
vec![3, 1, 2, 2],
vec![1, 4, 4, 5],
vec![2, 4, 2, 2],
vec![2, 4, 2, 2],
];
assert_eq!(equal_pairs(grid2), 3);
}
pub fn equal_pairs(grid: Vec<Vec<i32>>) -> i32 {
use std::collections::HashMap;
// 将行加入hashmap
let mut map: HashMap<&Vec<i32>, i32> = HashMap::new();
grid.iter()
.for_each(|row| *map.entry(row).or_insert(0) += 1);
let mut count: i32 = 0;
// 遍历列
for j in 0..grid.len() {
let mut col: Vec<i32> = vec![];
for i in 0..grid.len() {
col.push(grid[i][j]);
}
if map.contains_key(&col) {
count += map.get(&col).unwrap();
}
}
count
}