前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >力扣算法题之好数对的数目

力扣算法题之好数对的数目

作者头像
用户11036582
发布2024-03-24 12:46:44
840
发布2024-03-24 12:46:44
举报

给大家分享一句我很喜欢我话: 知不足而奋进,望远山而前行!!! 铁铁们,成功的路上必然是孤独且艰难的,但是我们不可以放弃,远山就在前方,但我们能力仍然不足,所有我们更要奋进前行!!! 今天我们更新了xxxxxxx内容, 🎉 欢迎大家关注🔍点赞👍收藏⭐️留言📝

题目描述:

本题代码:

代码语言:javascript
复制
#include <iostream>
#include <vector>
#include <cstring>

class Solution {
public:
    int numIdenticalPairs(std::vector<int>& nums) {
        int hash[101] = {0};  // 初始化哈希表为0
        int ans = 0;

        for(int i = 0; i < nums.size(); i++) {
            ans += hash[nums[i]];  // 将哈希表中 nums[i] 对应的值累加到答案中
            ++hash[nums[i]];  // 更新哈希表中 nums[i] 的出现次数
        }

        return ans;
    }
};

int main() {
    Solution solution;
    std::vector<int> nums = {1, 2, 3, 1, 1, 2, 4}; // 测试数据
    int result = solution.numIdenticalPairs(nums);

    std::cout << "Number of Identical Pairs: " << result << std::endl;

    return 0;
}#include <iostream>
#include <vector>
#include <cstring>

class Solution {
public:
    int numIdenticalPairs(std::vector<int>& nums) {
        int hash[101] = {0};  // 初始化哈希表为0
        int ans = 0;

        for(int i = 0; i < nums.size(); i++) {
            ans += hash[nums[i]];  // 将哈希表中 nums[i] 对应的值累加到答案中
            ++hash[nums[i]];  // 更新哈希表中 nums[i] 的出现次数
        }

        return ans;
    }
};

int main() {
    Solution solution;
    std::vector<int> nums = {1, 2, 3, 1, 1, 2, 4}; // 测试数据
    int result = solution.numIdenticalPairs(nums);

    std::cout << "Number of Identical Pairs: " << result << std::endl;

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述:
  • 本题代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档