news 2026/6/15 20:14:16

(100分)- 二元组个数(Java JS Python)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
(100分)- 二元组个数(Java JS Python)

(100分)- 二元组个数(Java & JS & Python)

题目描述

给定两个数组a,b,若a[i] == b[j] 则称 [i, j] 为一个二元组,求在给定的两个数组中,二元组的个数。

输入描述

第一行输入 m
第二行输入m个数,表示第一个数组

第三行输入 n
第四行输入n个数,表示第二个数组

输出描述

二元组个数。

用例
输入4
1 2 3 4
1
1
输出1
说明二元组个数为 1个
输入6
1 1 2 2 4 5
3
2 2 4
输出5
说明二元组个数为 5 个。
题目解析

很简单的双重for,

/** * * @param {Array} arrM 第二行输入的数组 * @param {Number} m 第一行输入的数字m * @param {Array} arrN 第四行输入的数组 * @param {Number} n 第二行输入的数字n * @returns */ function getResult(arrM, m, arrN, n) { let count = 0; for (let i = 0; i < m; i++) { for (let j = 0; j < n; j++) { if (arrM[i] === arrN[j]) { count++; } } } return count; }

考虑到数量级较大的情况,O(n*m)的时间复杂度可能无法满足要求。

针对这个问题,我的解决方案如下:

  1. 首先统计m数组中每个数字在n数组中出现的次数
  2. 同时统计n数组中每个数字在m数组中出现的次数
  3. 将两个统计结果中相同数字的出现次数相乘
  4. 最后将所有乘积相加得到最终结果

以示例2为例:

  • m数组统计结果:{2:2, 4:1}
  • n数组统计结果:{2:2, 4:1}
  • 计算结果:22 + 11 = 5
JavaScript算法源码
/* JavaScript Node ACM模式 控制台输入获取 */ const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const lines = []; rl.on("line", (line) => { lines.push(line); if (lines.length === 4) { const m = lines[0] - 0; const arrM = lines[1].split(" ").map(Number); const n = lines[2] - 0; const arrN = lines[3].split(" ").map(Number); console.log(getResult(arrM, m, arrN, n)); lines.length = 0; } }); function getResult(arrM, m, arrN, n) { const setM = new Set(arrM); const setN = new Set(arrN); const countM = {}; for (let m of arrM) { if (setN.has(m)) countM[m] ? countM[m]++ : (countM[m] = 1); } const countN = {}; for (let n of arrN) { if (setM.has(n)) countN[n] ? countN[n]++ : (countN[n] = 1); } let count = 0; for (let k in countM) { count += countM[k] * countN[k]; } return count; }
Java算法源码
import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = Integer.parseInt(sc.nextLine()); List<Integer> listM = Arrays.stream(sc.nextLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList()); int n = Integer.parseInt(sc.nextLine()); List<Integer> listN = Arrays.stream(sc.nextLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList()); System.out.println(getResult(listM, listN)); } public static int getResult(List<Integer> listM, List<Integer> listN) { HashSet<Integer> setM = new HashSet<Integer>(listM); HashSet<Integer> setN = new HashSet<Integer>(listN); HashMap<Integer, Integer> countM = new HashMap<>(); for (Integer m : listM) { if (setN.contains(m)) { countM.put(m, countM.getOrDefault(m, 0) + 1); } } HashMap<Integer, Integer> countN = new HashMap<>(); for (Integer n : listN) { if (setM.contains(n)) { countN.put(n, countN.getOrDefault(n, 0) + 1); } } int count = 0; for (Integer k : countM.keySet()) { count += countM.get(k) * countN.get(k); } return count; } }
Python算法源码
# 输入获取 m = int(input()) arrM = list(map(int, input().split())) n = int(input()) arrN = list(map(int, input().split())) # 算法入口 def getResult(arrM, arrN): setM = set(arrM) setN = set(arrN) countM = {} for m in arrM: if m in setN: if countM.get(m) is None: countM[m] = 1 else: countM[m] += 1 countN = {} for n in arrN: if n in setM: if countN.get(n) is None: countN[n] = 1 else: countN[n] += 1 count = 0 for k in countM.keys(): count += countM[k] * countN[k] return count # 算法调用 print(getResult(arrM, arrN))
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/15 12:19:49

PLS-200型电液伺服桥梁伸缩装置试验系统

PLS-200型电液伺服桥梁伸缩装置试验系统 一、功能特点&#xff1a; 1. 电液伺服桥梁伸缩装置试验系统简称为桥梁伸缩缝试验机&#xff0c;用于检测标准中规定的伸缩装置整体性能要求的项目。正常状态以及各种复杂状态下的伸缩装置变位均匀性试验、摩擦阻力试验&#xff0c;实…

作者头像 李华
网站建设 2026/6/15 19:32:36

磁耦合谐振式无线电能传输系统的探索与实现

磁耦合谐振式 无线电能传输电路系统板 LCC-S拓扑补偿网络 发射端电路采用Stm32f103c8t6主控&#xff0c;四路互补带死区的高频PWM与ir2110全桥驱动MOS管。 同时利用LCC器件谐振&#xff0c;所有参数确定和计算由maxwell和simulink计算得出。 接收电路利用S谐振网络补偿。 同时输…

作者头像 李华
网站建设 2026/6/15 8:40:17

探索双闭环 PI 控制的移相全桥变换器

双闭环PI控制的移相全桥变换器。 下图为仿真模型图&#xff0c;4个开关管对应的pwm波形图以及输出电压电流波形图和闭环性能测试输出波形图。 在电力电子领域&#xff0c;双闭环 PI 控制的移相全桥变换器可是个相当重要的存在。今天咱就来唠唠它&#xff0c;顺便结合一些仿真模…

作者头像 李华
网站建设 2026/6/15 8:38:37

P8448 [LSOT-1] 暴龙的土豆

记录72 #include<bits/stdc.h> using namespace std; int main(){long long t,n,cnt;cin>>t;while(t--){cnt0;cin>>n;for(long long i2;i*i*i<n;i){while(n%(i*i*i)0){cnt;n/i*i*i;} }cout<<cnt<<endl;}return 0; }题目传送门https://www.lu…

作者头像 李华
网站建设 2026/6/15 8:37:30

45545634

555454546

作者头像 李华