Skip to content

好数对的数目

仲灏2022-11-11约 1 分钟

<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/7ad13c.md for this page in Markdown format</div>

好数对的数目

CategoryDifficultyLikesDislikes
algorithmsEasy (84.69%)101-

<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Tags</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><a href="https://leetcode.com/tag/Unknown" title="https://leetcode.com/tag/Unknown" style="color: var(--vscode-textLink-foreground); text-decoration: none;"><code style="color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></a></p></details>

<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Companies</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><code style="color: var(--vscode-textPreformat-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></p></details>

给你一个整数数组 nums

如果一组数字 (i,j) 满足 nums[i] == nums[j]i < j ,就可以认为这是一组 好数对

返回好数对的数目。

示例 1:

输入:nums = [1,2,3,1,1,3]
输出:4
解释:有 4 组好数对,分别是 (0,3), (0,4), (3,4), (2,5) ,下标从 0 开始

示例 2:

输入:nums = [1,1,1,1]
输出:6
解释:数组中的每组数字都是好数对

示例 3:

输入:nums = [1,2,3]
输出:0

提示:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Discussion | Solution


方法一:暴力统计 思路与算法

对于每个 a_ia i

,枚举所有的 a_j (j > i)a j

(j>i),检查是否满足 a_i = a_ja i

=a j

,如果是就计入答案。

代码

C++JavaPython3

class Solution { public: int numIdenticalPairs(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; } }; 复杂度分析

时间复杂度:O(n^2)O(n 2 )。 空间复杂度:O(1)O(1)。 方法二:组合计数 思路与算法

用哈希表统计每个数在序列中出现的次数,假设数字 kk 在序列中出现的次数为 vv,那么满足题目中所说的 {\rm nums}[i] = {\rm nums}[j] = k(i < j)nums[i]=nums[j]=k(i<j) 的 (i, j)(i,j) 的数量就是 \frac{v(v - 1)}{2} 2 v(v−1)

,即 kk 这个数值对答案的贡献是 \frac{v(v - 1)}{2} 2 v(v−1)

。我们只需要把所有数值的贡献相加,即可得到答案。

代码

C++JavaPython3

class Solution { public: int numIdenticalPairs(vector<int>& nums) { unordered_map <int, int> m; for (int num: nums) { ++m[num]; }

    int ans = 0;
    for (const auto &[k, v]: m) {
        ans += v * (v - 1) / 2;
    }

    return ans;
}

}; 复杂度分析

时间复杂度:O(n)O(n)。 空间复杂度:O(n)O(n),即哈希表使用到的辅助空间的空间代价。


js
/*
 * @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
 * @Date: 2022-11-11 10:49:58
 * @LastEditTime: 2022-11-11 11:02:46
 * @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
 * @Description:  
 * @FilePath: /loan-home/Users/izhaong/izhaong/Project_me/leetcode/1512.好数对的数目.js
 */
/*
 * @lc app=leetcode.cn id=1512 lang=javascript
 *
 * [1512] 好数对的数目
 *
 * https://leetcode.cn/problems/number-of-good-pairs/description/
 *
 * algorithms
 * Easy (84.69%)
 * Likes:    101
 * Dislikes: 0
 * Total Accepted:    70.3K
 * Total Submissions: 83.1K
 * Testcase Example:  '[1,2,3,1,1,3]'
 *
 * 给你一个整数数组 nums 。
 *
 * 如果一组数字 (i,j) 满足 nums[i] == nums[j] 且 i < j ,就可以认为这是一组 好数对 。
 *
 * 返回好数对的数目。
 *
 *
 *
 * 示例 1:
 *
 * 输入:nums = [1,2,3,1,1,3]
 * 输出:4
 * 解释:有 4 组好数对,分别是 (0,3), (0,4), (3,4), (2,5) ,下标从 0 开始
 *
 *
 * 示例 2:
 *
 * 输入:nums = [1,1,1,1]
 * 输出:6
 * 解释:数组中的每组数字都是好数对
 *
 * 示例 3:
 *
 * 输入:nums = [1,2,3]
 * 输出:0
 *
 *
 *
 *
 * 提示:
 *
 *
 * 1 <= nums.length <= 100
 * 1 <= nums[i] <= 100
 *
 *
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var numIdenticalPairs = function (nums) {
  let sum = 0;
  for (let i = 0; i < nums.length; i++) {
    const iv = nums[i];

    for (let j = i + 1; j < nums.length; j++) {
      const jv = nums[j];
      if (jv === iv) {
        sum++;
      }
    }
  }
  return sum;
};
var numIdenticalPairs1 = function (nums) {
  let sum = 0;
  const numMap = new Map();
  for (let i = 0; i < nums.length; i++) {
    const e = nums[i];
    const getNum = numMap.get(e);
    if (getNum) {
      numMap.set(e, getNum + 1);
    } else {
      numMap.set(e, 1);
    }
  }
  for (const [k, v] of numMap) {
    sum += (v * (v - 1)) / 2;
  }
  return sum;
};
// @lc code=end