首页 > 其他分享 >leetcode-1128-easy

leetcode-1128-easy

时间:2023-05-18 20:35:13浏览次数:44  
标签:1128 map dominoe int domino num easy dominoes leetcode

Number of Equivalent Domino Pairs

Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a == c and b == d), or (a == d and b == c) - that is, one domino can be rotated to be equal to another domino.

Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j].

Example 1:

Input: dominoes = [[1,2],[2,1],[3,4],[5,6]]
Output: 1
Example 2:

Input: dominoes = [[1,2],[1,2],[1,1],[1,2],[2,2]]
Output: 3
Constraints:

1 <= dominoes.length <= 4 * 104
dominoes[i].length == 2
1 <= dominoes[i][j] <= 9

思路一:用 map 映射,原先想用 x * 10 + y 来作 map 的 key,但是 bug 比较多,最后直接用 string 一把梭了

    public static int numEquivDominoPairs(int[][] dominoes) {
        int ans = 0;

        Map<String, Integer> map = new HashMap<>();

        for (int[] dominoe : dominoes) {

            if (dominoe[0] > dominoe[1]) {
                map.compute(dominoe[0] + "," + dominoe[1], (k, v) -> v == null ? 1 : v + 1);
            } else {
                map.compute(dominoe[1] + "," + dominoe[0], (k, v) -> v == null ? 1 : v + 1);
            }
        }

        for (Map.Entry<String, Integer> e : map.entrySet()) {
            if (e.getValue() <= 1) {
                continue;
            }

            ans += e.getValue() * (e.getValue() - 1);
        }


        return ans / 2;
    }

思路二:补上官方的解

    public int numEquivDominoPairs(int[][] dominoes) {
        int[] num = new int[100];
        int ret = 0;
        for (int[] domino : dominoes) {
            int val = domino[0] < domino[1] ? domino[0] * 10 + domino[1] : domino[1] * 10 + domino[0];
            ret += num[val];
            num[val]++;
        }
        return ret;
    }

标签:1128,map,dominoe,int,domino,num,easy,dominoes,leetcode
From: https://www.cnblogs.com/iyiluo/p/17413211.html

相关文章

  • leetcode-1422-easy
    MaximumScoreAfterSplittingaStringGivenastringsofzerosandones,returnthemaximumscoreaftersplittingthestringintotwonon-emptysubstrings(i.e.leftsubstringandrightsubstring).Thescoreaftersplittingastringisthenumberofze......
  • leetcode-1295-easy
    FindNumberswithEvenNumberofDigitsGivenanarraynumsofintegers,returnhowmanyofthemcontainanevennumberofdigits.Example1:Input:nums=[12,345,2,6,7896]Output:2Explanation:12contains2digits(evennumberofdigits).345cont......
  • leetcode-1103-easy
    DistributeCandiestoPeopleWedistributesomenumberofcandies,toarowofn=num_peoplepeopleinthefollowingway:Wethengive1candytothefirstperson,2candiestothesecondperson,andsoonuntilwegivencandiestothelastperson.Th......
  • #yyds干货盘点# LeetCode程序员面试金典:相交链表
    1.简述:给你两个单链表的头节点 headA和headB,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回null。图示两个链表在节点c1开始相交:题目数据保证整个链式结构中不存在环。注意,函数返回结果后,链表必须保持其原始结构。自定义评测:评测系统的输入......
  • #yyds干货盘点# LeetCode程序员面试金典:从中序与后序遍历序列构造二叉树
    题目:给定两个整数数组inorder和postorder,其中inorder是二叉树的中序遍历,postorder是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。 示例1:输入:inorder=[9,3,15,20,7],postorder=[9,15,7,20,3]输出:[3,9,20,null,null,15,7]示例2:输入:inorder=[-1],postorder......
  • 动态规划算法基础及leetcode例题
    01基础理论题型:动规基础(斐波那契数列or爬楼梯);背包问题;打家劫舍;股票问题;子序列问题动规误区:只要看懂递推就ok(递推公式只是一部分)解决动态规划应该要思考的几步:状态转移的DP数组以及下标的含义递推公式DP数组为何初始化遍历顺序打印DP数组02例题基础题目509.斐波那......
  • leetcode:二叉树的最大深度
    题目描述给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树[3,9,20,null,null,15,7],3/\920/\157返回它的最大深度 3。题目链接:104.二叉树......
  • Leetcode-Easy 806. Number of Lines To Write String
    题目描述给一个字符串S,从左到右将它们排列行,每行最大长度为100,,同时给定一个数组withds,widths[0]对应着a的宽度,widths[1]对应着b的宽度,...,widths[25]对应着z的宽度。求:至少需要多少行以及最后一行的长度下面是一个实例:Example:Input:widths=[10,10,10,10,10,10,1......
  • leetcode 1321 餐館營業額變化增長
    leetcode1321餐館營業額變化增長selectdistinctc2.visited_on,(selectsum(amount)fromCustomerc1wherec1.visited_on<=c2.visited_onandc1.visited_on>=date_sub(c2.visited_on,interval6day))asamount,round((selectsum(amount)fromCustomerc1......
  • 二刷Leetcode-Days04
    数组:/***27.移除元素*@paramnums*@paramval*@return很多考察数组、链表、字符串等操作的面试题,都使用双指针法。*/publicintremoveElement(int[]nums,intval){intleft=0;for(inti=0;i<nums.l......