zoukankan      html  css  js  c++  java
  • [LeetCode] 1512. Number of Good Pairs

    Given an array of integers nums.

    A pair (i,j) is called good if nums[i] == nums[j] and i < j.

    Return the number of good pairs.

    Example 1:

    Input: nums = [1,2,3,1,1,3]
    Output: 4
    Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
    

    Example 2:

    Input: nums = [1,1,1,1]
    Output: 6
    Explanation: Each pair in the array are good.
    

    Example 3:

    Input: nums = [1,2,3]
    Output: 0

    Constraints:

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

    题意是给一个数组,求的是数组中坐标不同但是值相同的pair有多少对。

    暴力解很好想,就是类似于two sum一样的两层for循环。由于数据量不是很大,这个思路是不超时的。

    时间O(n^2)

    空间O(1)

    Java实现

     1 class Solution {
     2     public int numIdenticalPairs(int[] nums) {
     3         // corner case
     4         if (nums == null || nums.length == 0) {
     5             return 0;
     6         }
     7         
     8         // normal case
     9         int res = 0;
    10         for (int i = 0; i < nums.length - 1; i++) {
    11             for (int j = i + 1; j < nums.length; j++) {
    12                 if (nums[i] == nums[j]) {
    13                     res++;
    14                 }
    15             }
    16         }
    17         return res;
    18     }
    19 }

    JavaScript实现

     1 /**
     2  * @param {number[]} nums
     3  * @return {number}
     4  */
     5 var numIdenticalPairs = function(nums) {
     6     let res = 0;
     7     for (let i = 0; i < nums.length - 1; i++) {
     8         for (let j = i + 1; j < nums.length; j++) {
     9             if (nums[i] == nums[j]) {
    10                 res++;
    11             }
    12         }
    13     }
    14     return res;
    15 };

    优化一些的思路是用hashmap。一开始扫描数组的时候遇到的数字一定没有在hashmap出现过,所以就正常存储;之后遇到的数字很有可能在hashmap中出现过,所以判断如果存在过,就往res上累加map.get(nums[i]) - 1。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public int numIdenticalPairs(int[] nums) {
     3         int res = 0;
     4         Map<Integer, Integer> map = new HashMap<>();
     5         for (int num : nums) {
     6             map.put(num, map.getOrDefault(num, 0) + 1);
     7             res += map.get(num) - 1;
     8         }
     9         return res;
    10     }
    11 }

    JavaScript实现

     1 /**
     2  * @param {number[]} nums
     3  * @return {number}
     4  */
     5 var numIdenticalPairs = function(nums) {
     6     let res = 0;
     7     let map = new Map();
     8     for (let num of nums) {
     9         if (!map.has(num)) {
    10             map.set(num, 1);
    11         } else {
    12             map.set(num, map.get(num) + 1);
    13             res += map.get(num) - 1;
    14         }
    15     }
    16     return res;
    17 };

    LeetCode 题目总结

  • 相关阅读:
    HDU 1874 畅通工程续(dijkstra)
    HDU 2112 HDU Today (map函数,dijkstra最短路径)
    HDU 2680 Choose the best route(dijkstra)
    HDU 2066 一个人的旅行(最短路径,dijkstra)
    关于测评机,编译器,我有些话想说
    测评机的优化问题 时间控制
    CF Round410 D. Mike and distribution
    数字三角形2 (取模)
    CF Round410 C. Mike and gcd problem
    CF Round 423 D. High Load 星图(最优最简构建)
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13291325.html
Copyright © 2011-2022 走看看