当前位置:网站首页>Find out the subscript pair of the specified value and solve it in - C language
Find out the subscript pair of the specified value and solve it in - C language
2022-04-22 00:19:00 【Mr Gao】
Find the subscript pair for the specified value
Here are two arrays of integers nums1 and nums2 , Please implement a data structure that supports the following two types of queries :
Add up , Add a positive integer to the nums2 The subscript corresponding to the element .
Count , Statistics satisfy nums1[i] + nums2[j] A pair of subscripts equal to the specified value (i, j) number (0 <= i < nums1.length And 0 <= j < nums2.length).
Realization FindSumPairs class :
FindSumPairs(int[] nums1, int[] nums2) Using integer arrays nums1 and nums2 initialization FindSumPairs object .
void add(int index, int val) take val Add to nums2[index] On , namely , perform nums2[index] += val .
int count(int tot) Return to satisfaction nums1[i] + nums2[j] == tot Subscript pair of (i, j) number .
Example :
Input :
[“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]]
Output :
[null, 8, null, 2, 1, null, null, 11]
explain :
FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);
findSumPairs.count(7); // return 8 ; Subscript pair (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) Satisfy 2 + 5 = 7 , Subscript pair (5,1), (5,5) Satisfy 3 + 4 = 7
findSumPairs.add(3, 2); // here nums2 = [1,4,5,4,5,4]
findSumPairs.count(8); // return 2 ; Subscript pair (5,2), (5,4) Satisfy 3 + 5 = 8
findSumPairs.count(4); // return 1 ; Subscript pair (5,0) Satisfy 3 + 1 = 4
findSumPairs.add(0, 1); // here nums2 = [2,4,5,4,5,4]
findSumPairs.add(1, 1); // here nums2 = [2,5,5,4,5,4]
findSumPairs.count(7); // return 11 ; Subscript pair (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) Satisfy 2 + 5 = 7 , Subscript pair (5,3), (5,5) Satisfy 3 + 4 = 7
Our conventional solution to this problem is as follows :
typedef struct {
int *num1;
int *num2;
int num1size;
int num2size;
} FindSumPairs;
FindSumPairs* findSumPairsCreate(int* nums1, int nums1Size, int* nums2, int nums2Size) {
FindSumPairs *fp=(FindSumPairs *)malloc(sizeof(FindSumPairs));
int i;
fp->num1=(int *)malloc(sizeof(int)*nums1Size);
fp->num2=(int *)malloc(sizeof(int)*nums2Size);
for(i=0;i<nums1Size;i++){
fp->num1[i]=nums1[i];
}
for(i=0;i<nums2Size;i++){
fp->num2[i]=nums2[i];
}
fp->num1size=nums1Size;
fp->num2size=nums2Size;
return fp;
}
void findSumPairsAdd(FindSumPairs* obj, int index, int val) {
obj->num2[index]=obj->num2[index]+val;
}
int findSumPairsCount(FindSumPairs* obj, int tot) {
int i,j;
int n=0;
for(i=0;i<obj->num1size;i++){
for(j=0;j<obj->num2size;j++){
if( obj->num1[i]+obj->num2[j]==tot)
n++;
}
}
return n;
}
void findSumPairsFree(FindSumPairs* obj) {
}
/** * Your FindSumPairs struct will be instantiated and called as such: * FindSumPairs* obj = findSumPairsCreate(nums1, nums1Size, nums2, nums2Size); * findSumPairsAdd(obj, index, val); * int param_2 = findSumPairsCount(obj, tot); * findSumPairsFree(obj); */
Of course, using a hash table is a good way , You can try it , Map our values to the hash table .
版权声明
本文为[Mr Gao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220011006514.html
边栏推荐
- L1-043 reading room
- Architecture practice battalion - module III - operation
- Privacy computing -- 36 -- federal learning acceleration method
- gorm 操作mysql
- Redis(三):redis集群——主从复制、哨兵、集群
- 容器雲系列之容器技術相關概念介紹
- 2022 Quality Controller - decoration direction - general basic (quality controller) examination question bank and simulation examination
- C# 利用委托事件进行窗体间的传值
- CoDeSys method of reading CSV file (non Excel)
- 比较方便安全的期货开户怎么办理好?
猜你喜欢

LeetCode_62 不同路径

An example of double exponential smoothing method

MSF -- attack MySQL and simple and practical

展平多级双向链表-c语言

RT thread application - using RT thread on stm32l051 (I. new project of wireless temperature and humidity sensor)

活动预告 | 4月23日,多场OpenMLDB精彩分享来袭,不负周末好时光!

Detailed MOS tube knowledge - MOS tube high-end drive and low-end drive analysis, principle and difference

08. 树莓派安装MySQL
![[reading notes] empirical accounting and financial research methods - principle, application and SAS implementation, Lu Guihua](/img/a0/ceb8212bae4d76860f5a70824cd2c8.gif)
[reading notes] empirical accounting and financial research methods - principle, application and SAS implementation, Lu Guihua

开关电源中开关管与二极管EMI抑制方法分析
随机推荐
[Niuke] you must brush the top101-01 linked list for the interview
On the happiness of fishing -- April 20
分布式事务与Seata
Why do requirements change during MES implementation? How to solve it?
LeetCode刷题——字符串
RT-Thread 应用篇 — 在STM32L051上使用 RT-Thread (一、无线温湿度传感器 之 新建项目)
The shadow ticket rebate system developed by uniapp + PHP can operate perfectly
Architecture practice battalion - module III - operation
Kuangshi Research Institute | dark vision network: RGB IR Fusion low illumination imaging method using depth inconsistent a priori
Redis source code linked list (adlist. H and adlist. C) (Part 1)
C# 利用委托事件进行窗体间的传值
Wait(), wait (long), wait (long, int) / notification mechanism notify(), notifyall()
redis源码之链表(adlist.h和adlist.c)(篇一)
Multiple forms of alternate printing implemented by multithreading
slam 单目稠密重建详解
How to introduce Fuyue festival in any web page to destroy small aircraft?
【牛客】面试必刷TOP101——01链表
OJ daily practice - factorial sequence and
msf --攻击MySQL 和简单实用
09. Raspberry pie ASP Net environment configuration