给你一个整数数组 nums 。 如果一组数字 (i,j) 满足 nums[i] == nums[j] 且 i < j ,就可以认为这是一组 好数对 。 返回好数对的数目。 来源:力扣(Le

执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户 内存消耗:36 MB, 在所有 Java 提交中击败了93.38% 的用户

解题思路

数组对应下表存储对应值,先加结果,后自增
代码

class Solution {
    public int numIdenticalPairs(int[] nums) {
        int ans=0;
        int []temp=new int [100];
        for(int n:nums){
            ans+=temp[n-1]++;
        }
        return ans;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45942124/article/details/108928475