当前位置: 首页 > news >正文

(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))
http://www.jsqmd.com/news/359096/

相关文章:

  • 计算机小程序毕设实战-基于SpringBoot中小学家校通系统的设计与实现springboot+小程序的家校通程序设计与实现【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • ubuntu启用ssh (广域网访问)(IPV6访问)
  • 要不然让ai研究原神的界面也行,比如写个skill文件按下某个按键会进入什么界面,不给坐标,搞个程序识别按钮给个固定标签
  • (100分)- 等和子数组最小和(Java JS Python)
  • 【课程设计/毕业设计】基于微信小程序的校园信息交流平台基于springboot+小程序的高校校园信息交流平台小程序设计与实现【附源码、数据库、万字文档】
  • 内网共享神器,手机电脑一键互传大文件
  • (100分)- 端口合并(Java JS Python)
  • 【课程设计/毕业设计】基于springboot+小程序的家校通程序设计与实现消息推送、班级管理、作业管理、考勤管理、成绩管理【附源码、数据库、万字文档】
  • (100分)- 单词倒序(Java JS Python)
  • 小程序毕设项目:基于springboot+小程序的高校校园信息交流平台小程序设计与实现(源码+文档,讲解、调试运行,定制等)
  • 小程序毕设项目:基于springboot+小程序的家校通程序设计与实现(源码+文档,讲解、调试运行,定制等)
  • (100分)- 单向链表中间节点(Java JS Python)
  • (100分)- 打印机队列(Java JS Python)
  • 创业三年,记录来时路
  • jwt和oauth2的原理、特点、区别及使用场景
  • 计算机小程序毕设实战-基于springboot+小程序的高校生活互助平台小程序基于SpringBoot的高校报修与互助平台小程序【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • 戴尔服务器常用设置
  • 如何在 Teams 中添加一个页面
  • 【课程设计/毕业设计】基于SpringBoot校园生活服务小程序基于springboot+小程序的高校生活互助平台小程序【附源码、数据库、万字文档】
  • STC15F204EA概述
  • 对于tarjan的思考
  • 小程序毕设项目:基于springboot+小程序的高校生活互助平台小程序(源码+文档,讲解、调试运行,定制等)
  • Python快速入门——学习笔记(持续更新中~)
  • 2月8日-(OpenSpec规范)
  • 《深入理解Java虚拟机》| 运行时数据区与OOM异常
  • 小程序计算机毕设之基于springboot+小程序的高校生活互助平台小程序基于SpringBoot校园生活服务小程序(完整前后端代码+说明文档+LW,调试定制等)
  • 【毕业设计】基于springboot+小程序的高校生活互助平台小程序(源码+文档+远程调试,全bao定制等)
  • Kconfig测试
  • 【计算机毕业设计案例】基于springboot+小程序的高校生活互助平台小程序校园互助性小程序的设计与开发(程序+文档+讲解+定制)
  • 《分布式追踪Span-业务标识融合:端到端业务可观测手册》