网站备案密码重置,微信的微网站模板下载安装,自动写论文神器app,做网站首页与分页什么样子day27【LeetCode】454. 四数相加 II
1.题目描述
附上题目链接#xff1a;四数相加 II
给你四个整数数组 nums1、nums2、nums3 和 nums4 #xff0c;数组长度都是 n #xff0c;请你计算有多少个元组 (i, j, k, l) 能满足#xff1a;
0 i, j, k, l nnums1[i] …day27【LeetCode】454. 四数相加 II
1.题目描述
附上题目链接四数相加 II
给你四个整数数组 nums1、nums2、nums3 和 nums4 数组长度都是 n 请你计算有多少个元组 (i, j, k, l) 能满足
0 i, j, k, l nnums1[i] nums2[j] nums3[k] nums4[l] 0
示例 1
输入nums1 [1,2], nums2 [-2,-1], nums3 [-1,2], nums4 [0,2]
输出2
解释
两个元组如下
1. (0, 0, 0, 1) - nums1[0] nums2[0] nums3[0] nums4[1] 1 (-2) (-1) 2 0
2. (1, 1, 0, 0) - nums1[1] nums2[1] nums3[0] nums4[0] 2 (-1) (-1) 0 0示例 2
输入nums1 [0], nums2 [0], nums3 [0], nums4 [0]
输出12.题解
c
写法一
class Solution {
public:int fourSumCount(vectorint nums1, vectorint nums2, vectorint nums3, vectorint nums4) {unordered_mapint,int mymap;int count0;for(int a : nums1){for(int b : nums2){mymap[ab];}}for(int c:nums3){for(int d:nums4){if(mymap.find(0-(cd)) ! mymap.end()){count mymap[0-(cd)];}}}return count;}
};解读
定义了一个 unordered_mapint, int 类型的哈希表 mymap用于存储两个数组中元素的和以及对应的出现次数。遍历 nums1 和 nums2 两个数组将两个数组中元素两两相加的结果存入哈希表 mymap 中。再次遍历 nums3 和 nums4 两个数组在遍历过程中判断哈希表中是否存在 -(cd) 这个值若存在则将对应的数量累加到 count 中。最终返回 count即满足条件的两两相加等于 0 的组合数量。
写法二
public:int fourSumCount(vectorint nums1, vectorint nums2, vectorint nums3, vectorint nums4) {unordered_mapint,int mymap;int count0;for(int a : nums1){for(int b : nums2){mymap[ab];}}for(int c:nums3){for(int d:nums4){if(mymap.count(-c-d)){count mymap[-c-d];}}}return count;}
};本质是一样的只不过在寻找对应数的时候用的是count函数 以及简化了写法。
python
(版本一)使用字典
class Solution:def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) - int:mymap dict()count 0for a in nums1:for b in nums2:if ab in mymap:mymap[ab] 1else:mymap[ab] 1for c in nums3:for d in nums4:if -c-d in mymap:count mymap[-c-d]return count第一个if那儿可以换成下面这种写法一行搞定。
mymap[ab] mymap.get(ab, 0) 1(版本二)使用defaultdict
class Solution:def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) - int:rec defaultdict(int)count 0for i in nums1:for j in nums2:rec[ij] 1for i in nums3:for j in nums4:count rec.get((-i-j),0)return count(版本三)使用counter
class Solution:def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) - int:rec collections.Counter(ij for i in nums1 for j in nums2)count 0for i in nums3:for j in nums4:if -i-j in rec:count rec[-i-j]return countok了就到这里叭
如果觉得作者写的不错求给博主一个大大的点赞支持一下你们的支持是我更新的最大动力
如果觉得作者写的不错求给博主一个大大的点赞支持一下你们的支持是我更新的最大动力
如果觉得作者写的不错求给博主一个大大的点赞支持一下你们的支持是我更新的最大动力 文章转载自: http://www.morning.hwbmn.cn.gov.cn.hwbmn.cn http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.zcxjg.cn.gov.cn.zcxjg.cn http://www.morning.mtgkq.cn.gov.cn.mtgkq.cn http://www.morning.dkfb.cn.gov.cn.dkfb.cn http://www.morning.dmfdl.cn.gov.cn.dmfdl.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.lsfzq.cn.gov.cn.lsfzq.cn http://www.morning.gswfs.cn.gov.cn.gswfs.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.tjsxx.cn.gov.cn.tjsxx.cn http://www.morning.fnczn.cn.gov.cn.fnczn.cn http://www.morning.qsy39.cn.gov.cn.qsy39.cn http://www.morning.rxpp.cn.gov.cn.rxpp.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.lndongguan.com.gov.cn.lndongguan.com http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.qqbw.cn.gov.cn.qqbw.cn http://www.morning.mfxcg.cn.gov.cn.mfxcg.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.dcmnl.cn.gov.cn.dcmnl.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.qrmyd.cn.gov.cn.qrmyd.cn http://www.morning.sgbjh.cn.gov.cn.sgbjh.cn http://www.morning.hsgxj.cn.gov.cn.hsgxj.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.tndhm.cn.gov.cn.tndhm.cn http://www.morning.ywxln.cn.gov.cn.ywxln.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.spfh.cn.gov.cn.spfh.cn http://www.morning.rcdmp.cn.gov.cn.rcdmp.cn http://www.morning.tymnr.cn.gov.cn.tymnr.cn http://www.morning.zlff.cn.gov.cn.zlff.cn http://www.morning.yhwxn.cn.gov.cn.yhwxn.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.hqsnt.cn.gov.cn.hqsnt.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.yqsr.cn.gov.cn.yqsr.cn http://www.morning.jzyfy.cn.gov.cn.jzyfy.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.txlnd.cn.gov.cn.txlnd.cn http://www.morning.zsrdp.cn.gov.cn.zsrdp.cn http://www.morning.bmtyn.cn.gov.cn.bmtyn.cn http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.dhqyh.cn.gov.cn.dhqyh.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn http://www.morning.pcngq.cn.gov.cn.pcngq.cn http://www.morning.srbbh.cn.gov.cn.srbbh.cn http://www.morning.rdxnt.cn.gov.cn.rdxnt.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.bfycr.cn.gov.cn.bfycr.cn http://www.morning.zcwzl.cn.gov.cn.zcwzl.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.pcngq.cn.gov.cn.pcngq.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.shawls.com.cn.gov.cn.shawls.com.cn http://www.morning.xqgh.cn.gov.cn.xqgh.cn http://www.morning.njftk.cn.gov.cn.njftk.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.nkjpl.cn.gov.cn.nkjpl.cn http://www.morning.fbmrz.cn.gov.cn.fbmrz.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.tkrdg.cn.gov.cn.tkrdg.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn http://www.morning.fbnsx.cn.gov.cn.fbnsx.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.zqnmp.cn.gov.cn.zqnmp.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.thmlt.cn.gov.cn.thmlt.cn