news 2026/4/16 13:04:48

(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/4/16 13:01:37

7. 基于三菱PLC的3×4立体车库组态系统

7基于三菱PLC组态王34立体车库组态系统立体车库这玩意儿现在真是遍地开花&#xff0c;但要让12个车位在3层4列里自动腾挪可没看起来那么轻松。今天咱们就唠唠怎么用三菱PLC和组态王搭出个稳定运行的立体车库控制系统&#xff0c;手把手教你避开那些新手必踩的坑。硬件选型&…

作者头像 李华
网站建设 2026/3/19 7:54:17

风储调频技术:真实可靠的储能模型与使用保障

风储调频&#xff0c;储能调频&#xff0c;保证真实&#xff0c;模型如图&#xff0c;保证正常使用 风电场输出功率看天吃饭这事儿&#xff0c;大伙儿都懂。风速突然抽风&#xff0c;电网频率直接坐过山车。这时候储能系统就得像个救火队员&#xff0c;抄起充放电的大锤稳住局…

作者头像 李华
网站建设 2026/3/24 3:00:29

UG NX修补: 曲面和实体缝合

设计过程中可能会遇到一些曲面需要跟实体进行缝合&#xff0c;那么如何实现现曲面和实体缝合呢&#xff1f;

作者头像 李华
网站建设 2026/4/14 8:52:34

P10570 [JRKSJ R8] 网球

记录73 #include<bits/stdc.h> using namespace std; long long gcd(long long a,long long b){return b?gcd(b,a%b):a; } int main(){int T;long long a,b,c,t;cin>>T;while(T--){cin>>a>>b>>c;tgcd(a,b);a/t;b/t;tmin(a,b);if(c%t0) c/t;els…

作者头像 李华
网站建设 2026/4/15 22:17:21

WordPress中if语句判断字段是否存在并输出内容

在WordPress中可以使用if语句判断字段是否存在并输出内容。基于你的需求&#xff0c;三个社交图标的完整判断代码如下&#xff1a; <?php // 微博图标 - 判断 weibo 字段 $weibo of_get_option(weibo); if (!empty($weibo)) : ?><a href"<?php echo esc…

作者头像 李华
网站建设 2026/4/15 15:46:43

三亚精选十大海鲜美食推荐,让你的味蕾一次满足

三亚的美食文化丰富多样&#xff0c;尤其以海鲜和湘菜的结合备受欢迎。此地的海鲜不仅新鲜可口&#xff0c;还有具地方特色的湘菜。比如&#xff0c;三亚柠檬酸菜鱼、冬笋炒腊肉和湘味炒海鲜等美食&#xff0c;非常值得尝试。此外&#xff0c;无论是脆皮烧鸡还是湖南血鸭&#…

作者头像 李华