日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode 1865. 找出和为指定值的下标对(哈希)

發(fā)布時(shí)間:2024/7/5 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 1865. 找出和为指定值的下标对(哈希) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 1. 題目
    • 2. 解題

1. 題目

給你兩個(gè)整數(shù)數(shù)組 nums1 和 nums2 ,請(qǐng)你實(shí)現(xiàn)一個(gè)支持下述兩類(lèi)查詢(xún)的數(shù)據(jù)結(jié)構(gòu):

  • 累加 ,將一個(gè)正整數(shù)加到 nums2 中指定下標(biāo)對(duì)應(yīng)元素上。
  • 計(jì)數(shù) ,統(tǒng)計(jì)滿(mǎn)足 nums1[i] + nums2[j] 等于指定值的下標(biāo)對(duì) (i, j) 數(shù)目(0 <= i < nums1.length 且 0 <= j < nums2.length)。

實(shí)現(xiàn) FindSumPairs 類(lèi):

  • FindSumPairs(int[] nums1, int[] nums2) 使用整數(shù)數(shù)組 nums1 和 nums2 初始化 FindSumPairs 對(duì)象。
  • void add(int index, int val) 將 val 加到 nums2[index] 上,即,執(zhí)行 nums2[index] += val 。
  • int count(int tot) 返回滿(mǎn)足 nums1[i] + nums2[j] == tot 的下標(biāo)對(duì) (i, j) 數(shù)目。
示例: 輸入: ["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"] [[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]] 輸出: [null, 8, null, 2, 1, null, null, 11]解釋: FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]); findSumPairs.count(7); // 返回 8 ; 下標(biāo)對(duì) (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) 滿(mǎn)足 2 + 5 = 7 ,下標(biāo)對(duì) (5,1), (5,5) 滿(mǎn)足 3 + 4 = 7 findSumPairs.add(3, 2); // 此時(shí) nums2 = [1,4,5,4,5,4] findSumPairs.count(8); // 返回 2 ;下標(biāo)對(duì) (5,2), (5,4) 滿(mǎn)足 3 + 5 = 8 findSumPairs.count(4); // 返回 1 ;下標(biāo)對(duì) (5,0) 滿(mǎn)足 3 + 1 = 4 findSumPairs.add(0, 1); // 此時(shí) nums2 = [2,4,5,4,5,4] findSumPairs.add(1, 1); // 此時(shí) nums2 = [2,5,5,4,5,4] findSumPairs.count(7); // 返回 11 ;下標(biāo)對(duì) (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) 滿(mǎn)足 2 + 5 = 7 ,下標(biāo)對(duì) (5,3), (5,5) 滿(mǎn)足 3 + 4 = 7提示: 1 <= nums1.length <= 1000 1 <= nums2.length <= 10^5 1 <= nums1[i] <= 10^9 1 <= nums2[i] <= 10^5 0 <= index < nums2.length 1 <= val <= 10^5 1 <= tot <= 10^9 最多調(diào)用 add 和 count 函數(shù)各 1000

來(lái)源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/finding-pairs-with-a-certain-sum
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。

2. 解題

  • nums2 的長(zhǎng)度比較長(zhǎng),對(duì)其數(shù)字進(jìn)行哈希計(jì)數(shù)
  • add 的時(shí)候,更新哈希計(jì)數(shù)
  • count 的時(shí)候,遍歷 nums1 ,在 哈希map 中查找 tot - nums1_i
class FindSumPairs {unordered_map<int,int> m;vector<int> v1, v2; public:FindSumPairs(vector<int>& nums1, vector<int>& nums2) {v1 = nums1;v2 = nums2;for(auto n : nums2)m[n]++;//哈希計(jì)數(shù)}void add(int index, int val) {m[v2[index]]--;//原來(lái)的數(shù)字少一個(gè)v2[index] += val;//更新值m[v2[index]]++;//新的數(shù)字多一個(gè)}int count(int tot) {int ans = 0;for(auto n : v1){if(m.find(tot-n) != m.end())//哈希查找ans += m[tot-n];}return ans;} };

360 ms 72 MB C++


我的CSDN博客地址 https://michael.blog.csdn.net/

長(zhǎng)按或掃碼關(guān)注我的公眾號(hào)(Michael阿明),一起加油、一起學(xué)習(xí)進(jìn)步!

總結(jié)

以上是生活随笔為你收集整理的LeetCode 1865. 找出和为指定值的下标对(哈希)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。