zoukankan      html  css  js  c++  java
  • 1128. 等价多米诺骨牌对的数量

    给你一个由一些多米诺骨牌组成的列表 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) 的数量。

    • 1 <= dominoes.length <= 40000
    • 1 <= dominoes[i][j] <= 9

    1. 暴力法超时

    2.组成一个两位数,其中第一位大于第二位,那么所有满足条件的映射到一个二位数上,每个二位数组成的对子个数是C(n,2),也就是(n-1)*n/2,也可以理解为每次出现新的,都和旧的个数构成对数,所以加上旧的的个数

    class Solution {
        public int numEquivDominoPairs(int[][] dominoes) {
            List<Integer> list=new ArrayList();
            int count=0;
            int []num=new int[100];
            for(int i=0;i<dominoes.length;i++){
                int a=Math.min(dominoes[i][0],dominoes[i][1]);
                int b=Math.max(dominoes[i][0],dominoes[i][1]);
                int x=b*10+a;
                count+=num[x];//新出现的一个重复和前面的x的个数都组成一对,所以加上之前的num[x]
                num[x]++;
    
            }
            
            return count;
        }
    }
  • 相关阅读:
    常见问题汇总
    python的正则表达式
    Python 程序读取外部文件、网页的编码与JSON格式的转化
    第三方仓库PyPI
    文件名称一定不要设置为某些模块名称,会引起冲突!
    迟来的博客
    FENLIQI
    fenye
    Notif
    phpv6_css
  • 原文地址:https://www.cnblogs.com/jieyi/p/14330760.html
Copyright © 2011-2022 走看看