news 2026/10/2 9:28:12

(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/9/30 10:47:36

【课程设计/毕业设计】基于SpringBoot校园生活服务小程序基于springboot+小程序的高校生活互助平台小程序【附源码、数据库、万字文档】

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/9/27 18:17:41

leetcode 923. 3Sum With Multiplicity 三数之和的多种可能

Problem: 923. 3Sum With Multiplicity 三数之和的多种可能 排序&#xff0c;哈希表记录每个数字的频次&#xff0c;双循环&#xff0c;拿到两个数字&#xff0c;最后一个数字相减得到&#xff0c;然后通过排列组合计算答案&#xff0c;需要考虑是否存在这样的情况&#xff0c;…

作者头像 李华
网站建设 2026/10/2 2:51:33

《Java并发编程的艺术》| 并发关键字与 JMM 核心规则

摘要&#xff1a;本篇文章围绕 Java 并发编程核心&#xff0c;梳理了 volatile、synchronized的实现原理与特性 &#xff1b;同时详解了 JMM&#xff0c;需配合 volatile、synchronized等工具&#xff0c;才能实现多线程下共享变量的原子性、可见性和有序性保障。 第2章 Java并…

作者头像 李华
网站建设 2026/9/30 10:27:26

93基于三菱PLC和组态王的兰花灌溉控制系统的农业农田应用

93基于三菱PLC和组态王的兰花灌溉控制系统的农业农田 兰花这种傲娇的植物&#xff0c;浇多了烂根&#xff0c;浇少了干枯&#xff0c;传统人工浇水能把种植户逼疯。去年在云南花卉基地看到师傅们凌晨三点打着手电筒浇水&#xff0c;我就琢磨着用三菱FX3U PLC搭个自动灌溉系统&…

作者头像 李华
网站建设 2026/9/27 18:40:39

专升本高数资源合集

2022专升本数学全程班&#xff08;完结&#xff09; 文件大小: 45.4GB内容特色: 2022专升本数学全程录播&#xff0c;45GB高清适用人群: 专科升本人群、数学基础薄弱需系统复习者核心价值: 考点全覆盖真题精讲&#xff0c;一站式冲刺高分下载链接: https://pan.quark.cn/s/05f…

作者头像 李华