难度:Easy
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: 1Constraints:1 <= dominoes.length <= 400001 <= dominoes[i][j] <= 9
可以先按大小排个序,然后建立一个map,统计次数大于1的元素个数,每个元素的对数为C(2,n)
class Solution {public:int numEquivDominoPairs(vector<vector<int>>& dominoes) {int res=0;if(dominoes.size()<2) return res;for(auto &v: dominoes)if(v[0]>v[1]) swap(v[0],v[1]);map<vector<int>, int> domin_map;for(auto v:dominoes)domin_map[v]++;for(auto v:domin_map)if(v.second >1) res+= v.second * (v.second -1);return res/2;}};
执行用时 :68 ms, 在所有 C++ 提交中击败了76.11%的用户 内存消耗 :23.5 MB, 在所有 C++ 提交中击败了100.00%的用户