classSolution { public: intnumIdenticalPairs(vector<int>& nums){ int ans = 0; for (int i = 0; i < nums.size(); ++i) { for (int j = i + 1; j < nums.size(); ++j) { if (nums[i] == nums[j]) { ++ans; } } } return ans; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { publicintnumIdenticalPairs(int[] nums) { intans=0; for (inti=0; i < nums.length; ++i) { for (intj= i + 1; j < nums.length; ++j) { if (nums[i] == nums[j]) { ++ans; } } } return ans; } }
[sol1-Python3]
1 2 3 4 5 6 7 8
classSolution: defnumIdenticalPairs(self, nums: List[int]) -> int: ans = 0 for i inrange(len(nums)): for j inrange(i + 1, len(nums)): if nums[i] == nums[j]: ans += 1 return ans