Open carloscn opened 1 year ago
等价的对数:
1) 只有一种类型,统计等价对数; 2) 有多重类型的等价,统计各自的等价对数,然后加和。
制作函数 is_equal(),比较两个数组是否等价。 遍历数组即可。
fn is_equal(a:&Vec<i32>, b:&Vec<i32>) -> bool
{
if a.len() != b.len() && a.len() != 2{
return false;
}
if (a[0] == b[0] && a[1] == b[1]) ||
(a[0] == b[1] && a[1] == b[0]) {
return true;
}
return false;
}
pub fn num_equiv_domino_pairs(dominoes: Vec<Vec<i32>>) -> i32
{
let mut ret:i32 = 0;
if dominoes.len() < 2 {
return ret;
}
let mut i:usize = 0;
let mut j:usize = 0;
for i in 0..dominoes.len() {
for j in (i + 1)..dominoes.len() {
if is_equal(&dominoes[i], &dominoes[j]) {
ret += 1;
}
}
}
return ret;
}
问题描述
给你一个由一些多米诺骨牌组成的列表 dominoes。
如果其中某一张多米诺骨牌可以通过旋转 0 度或 180 度得到另一张多米诺骨牌,我们就认为这两张牌是等价的。
形式上,dominoes[i] = [a, b] 和 dominoes[j] = [c, d] 等价的前提是 a==c 且 b==d,或是 a==d 且 b==c。
在 0 <= i < j < dominoes.length 的前提下,找出满足 dominoes[i] 和 dominoes[j] 等价的骨牌对 (i, j) 的数量。
示例:
输入:dominoes = [[1,2],[2,1],[3,4],[5,6]] 输出:1
输入:[[1,2],[2,1],[1,2],[1,2]] 输出:6
提示:
1 <= dominoes.length <= 40000 1 <= dominoes[i][j] <= 9
来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/number-of-equivalent-domino-pairs。