news 2026/3/4 17:04:43

(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/3/4 9:47:18

《深入理解Java虚拟机》| 运行时数据区与OOM异常

摘要&#xff1a;本文系统解析《深入理解Java虚拟机》中的运行时数据区与OOM异常&#xff0c;包括程序计数器、虚拟机栈、堆内存、方法区与直接内存&#xff0c;同时讲解OOM异常等解决方案。 书摘内容 运行时数据区域 运行时数据区是 JVM 的内存核心&#xff0c;分为线程共享区…

作者头像 李华
网站建设 2026/3/2 6:04:35

leetcode 925. Long Pressed Name 长按键入-耗时100

Problem: 925. Long Pressed Name 长按键入 耗时100%&#xff0c;双指针&#xff0c;若左右字符相等l; r&#xff0c;若r > 0 && typed[r-1]typed[r]则r&#xff0c;否则false&#xff0c;最后若while(r < n2 && typed[r-1]typed[r]) r;&#xff0c;if(l…

作者头像 李华
网站建设 2026/3/3 23:09:22

如何获得Agentic AI提示工程架构师认证?备考与实战经验

Agentic AI提示工程架构师认证全攻略&#xff1a;备考计划实战经验核心技巧 1. 标题 (Title) 从入门到持证&#xff1a;Agentic AI提示工程架构师认证备考实战指南Agentic AI提示工程架构师认证之路&#xff1a;我如何3个月从零基础到通过考试&#xff1f;成为AI架构师的敲门…

作者头像 李华
网站建设 2026/3/4 7:44:57

14.僵尸孤儿-进程状态

一个pcb既属于全局链表又属于这个队列这个队列也是双链表&#xff0c;只不过先进先出FIFO运行状态就是正在被cpu运行&#xff0c;或者准备好了随时等待被调度。 包括运行和就绪。一个进程要去读键盘就要转去另一个队列&#xff0c;如果这个设备没有活跃&#xff0c;就没有读到键…

作者头像 李华
网站建设 2026/3/4 8:56:35

MATLAB鸟鸣识别系统截图](https://example.com/gui_screenshot.png

基于MATLAB的鸟鸣识别系统gui 传统声音特征&#xff0c;如MFCC机器学习分类器 分类器有支持向量积svm&#xff0c;邻近knn&#xff0c;分类树tree等 深度学习方法主要是先提取语谱图&#xff0c;有短时傅立叶时频图&#xff0c;和梅尔时频图 然后用googlenet进行深度学习训练 结…

作者头像 李华