Problem-solving ideas:
1 by means of the variable i, traversing the original array to dominoes [i] [0] and the dominoes [i] [1] as the smaller number of ten, as a larger number of bits
2. The new figures from the first step into a new array, while the current record number of new emerging digital
3. Since the digital array is greater than 0 and less than 10, the maximum value of the new number is 99.
4. from 0 to 99 traversing new array, if the number 1 is greater than the current number appears, the whole arrangement is calculated, the formula: n * (n-1) / 2
Time complexity of O (n)
Space complexity O (100)
Code:
class Solution {
public int numEquivDominoPairs(int[][] dominoes) {
int len=dominoes.length;
int cnt=0;
int k=0;
int[] res=new int[100];
for(int i=0;i<len;++i){
int temp=0;
//较小的数作为十位,较大的作为个位
if(dominoes[i][0]<dominoes[i][1]){
temp+=dominoes[i][0]*10+dominoes[i][1];
}
else{
temp+=dominoes[i][1]*10+dominoes[i][0];
}
res[temp]++;
}
//找相同的数字的对数
for(int i=0;i<100;++i){
if(res[i]>1){
cnt+=res[i]*(res[i]-1)/2;
}
}
return cnt;
}
}