当前位置: 首页 > wzjs >正文

企业网站案列哔哩哔哩免费网站观看

企业网站案列,哔哩哔哩免费网站观看,手机网站开发需求文档,wordpress评论特效1865. 找出和为指定值的下标对 给你两个整数数组 nums1 和 nums2 ,请你实现一个支持下述两类查询的数据结构: 累加 ,将一个正整数加到 nums2 中指定下标对应元素上。计数 ,统计满足 nums1[i] nums2[j] 等于指定值的下标对 (i, …

1865. 找出和为指定值的下标对

给你两个整数数组 nums1nums2 ,请你实现一个支持下述两类查询的数据结构:

  1. 累加 ,将一个正整数加到 nums2 中指定下标对应元素上。
  2. 计数 ,统计满足 nums1[i] + nums2[j] 等于指定值的下标对 (i, j) 数目(0 <= i < nums1.length0 <= j < nums2.length)。
    实现 FindSumPairs 类:
  • FindSumPairs(int[] nums1, int[] nums2) 使用整数数组 nums1nums2 初始化 FindSumPairs 对象。
  • void add(int index, int val)val 加到 nums2[index] 上,即,执行 nums2[index] += val
  • int count(int tot) 返回满足 nums1[i] + nums2[j] == tot 的下标对 (i, j) 数目。
    示例:
    输入:
["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 ; 下标对 (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) 满足 2 + 5 = 7 ,下标对 (5,1), (5,5) 满足 3 + 4 = 7
findSumPairs.add(3, 2); // 此时 nums2 = [1,4,5,4,5,4]
findSumPairs.count(8); // 返回 2 ;下标对 (5,2), (5,4) 满足 3 + 5 = 8
findSumPairs.count(4); // 返回 1 ;下标对 (5,0) 满足 3 + 1 = 4
findSumPairs.add(0, 1); // 此时 nums2 = [2,4,5,4,5,4]
findSumPairs.add(1, 1); // 此时 nums2 = [2,5,5,4,5,4]
findSumPairs.count(7); // 返回 11 ;下标对 (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) 满足 2 + 5 = 7 ,下标对 (5,3), (5,5) 满足 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
  • 最多调用 addcount 函数各 1000

思路

注意,nums1和nums2的长度范围是不一样的,nums1很短,我们可以利用这个特点,遍历nums1,和nums2的counter,这样不会超时

type FindSumPairs struct {  nums1 []int       // nums1.length <= 1000  nums2 []int       // nums2.length <= 10e6  cnt2  map[int]int // counter of nums2  
}  func Constructor(nums1 []int, nums2 []int) FindSumPairs {  var cnt2 = make(map[int]int)  for _, x := range nums2 {  cnt2[x]++  }  return FindSumPairs{nums1, nums2, cnt2}  
}  func (this *FindSumPairs) Add(index int, val int) {  this.cnt2[this.nums2[index]]--  this.nums2[index] += val  this.cnt2[this.nums2[index]]++  
}  func (this *FindSumPairs) Count(tot int) int {  var res int  for _, x := range this.nums1 {  res += this.cnt2[tot-x]  }  return res  
}  /**  * Your FindSumPairs object will be instantiated and called as such: * obj := Constructor(nums1, nums2); * obj.Add(index,val); * param_2 := obj.Count(tot); */

也可以把nums1和nums2都转为counter

type FindSumPairs struct {  nums1 []int       // nums1.length <= 1000  nums2 []int       // nums2.length <= 10e6  cnt1  map[int]int // counter of nums1  cnt2  map[int]int // counter of nums2  
}  func Constructor(nums1 []int, nums2 []int) FindSumPairs {  var cnt1 = make(map[int]int)  var cnt2 = make(map[int]int)  for _, x := range nums2 {  cnt2[x]++  }  for _, x := range nums1 {  cnt1[x]++  }  return FindSumPairs{nums1, nums2, cnt1, cnt2}  
}  func (this *FindSumPairs) Add(index int, val int) {  this.cnt2[this.nums2[index]]--  this.nums2[index] += val  this.cnt2[this.nums2[index]]++  
}  func (this *FindSumPairs) Count(tot int) int {  var res int  for k, v := range this.cnt1 {  res += v * (this.cnt2[tot-k])  }  return res  
}

文章转载自:

http://BDRUJ7AE.sLpcL.cn
http://YxPFTYa3.sLpcL.cn
http://9TIXOJDn.sLpcL.cn
http://D2esHggP.sLpcL.cn
http://Fo3hqa6X.sLpcL.cn
http://K51AOpdm.sLpcL.cn
http://6eM3wbpp.sLpcL.cn
http://qK0vV3d9.sLpcL.cn
http://nxvdPzPn.sLpcL.cn
http://6ugT8CDL.sLpcL.cn
http://0p0KJ39l.sLpcL.cn
http://jMoswwG6.sLpcL.cn
http://rHlu0q1E.sLpcL.cn
http://2nsOMcb3.sLpcL.cn
http://8YadEakT.sLpcL.cn
http://udGD7gYb.sLpcL.cn
http://6Utq4CFH.sLpcL.cn
http://cLLDpjhA.sLpcL.cn
http://6XePjXFs.sLpcL.cn
http://SizHpepF.sLpcL.cn
http://PKgs7U3n.sLpcL.cn
http://dcbxWOZ7.sLpcL.cn
http://tLo5WDeZ.sLpcL.cn
http://y8XJ76Hw.sLpcL.cn
http://KWtCcTsw.sLpcL.cn
http://LAKA6jtb.sLpcL.cn
http://W4bz7HdW.sLpcL.cn
http://XGAlbXRD.sLpcL.cn
http://EGHaPNru.sLpcL.cn
http://laSDtRnb.sLpcL.cn
http://www.dtcms.com/wzjs/769066.html

相关文章:

  • 如何做适合手机访问的网站成立公司注意事项
  • 开源seo软件百度seo快速提升排名
  • 2017建设厅网站手机网站 优化
  • 寿光网站制作运营管理系统
  • 网站开发用到的技术优化网站改版
  • 一诺互联 网站建设响应式网站公司
  • 苏中建设 网站网站前台与后台建设的先后次序
  • 攀枝花英文网站建设网站开发技术报告模板
  • 在沈阳做一个展示网站多少钱看不到图片 wordpress
  • 建设网站技术方案电子商务网站建设需要多少钱
  • 违反建设投诉网站举报网站宣传内容
  • 天津市建设行业联合会网站广州网站建设 全包
  • 做代金券的网站网站错误页面模板
  • 建立网站的公司做一个网站要怎么做
  • 网站怎么做搜索引擎优化wordpress更改忘记密码样式
  • 东莞网站制作公司是什么小红书网站开发形式选择
  • 泉州企业自助建站上海交通大学网站建设
  • 建设门户网站申请报告怎么实现网站建设报价方案
  • 查询网站流量的网址装饰设计做什么的
  • 开发个微网站多少钱做视频找空镜头那个网站比较全
  • 网站备案资质国内做网站最好的公司
  • 公司网站高端网站建设苏州建设
  • 安徽合肥做网站的公司有哪些软文生成器
  • 福州市城乡建设局网站手机下载视频网站模板下载失败
  • 用dw做的网站怎么发到网上熬夜必备黄
  • 电商网站建设网seo搜索推广费用多少
  • 网站建设论文选题怎么查看网站点击量
  • 青岛建设管理局网站做影视网站存储视频会侵权吗
  • 阳澄湖大闸蟹网站建设关键词优化策略
  • 做培训的网站地旺建设官方网站