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

373. 查找和最小的 K 对数字

参考的这个博客:
https://zhuanlan.zhihu.com/p/457239781
然后看这个代码我想到了另外一种方法,就是一步一步往里加元组 ( i , j ) (i,j) (i,j),看代码就知道了,不过需要做一步去重,去重不能用 i n t [ ] int[] int[]做,转换成字符串做比较好。

class Solution {public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {List<List<Integer>> ans = new ArrayList<>();PriorityQueue<int[]> q = new PriorityQueue<>((a,b)->((nums1[a[0]]+nums2[a[1]])-(nums1[b[0]]+nums2[b[1]])));// for(int i=0;i<nums1.length;i++){//     q.add(new int[]{i,0});// }q.add(new int[]{0,0});HashMap<String,Integer> mp = new HashMap<String,Integer>();mp.put((Arrays.toString(new int[]{0,0})),1);while(ans.size()<k && !q.isEmpty()){int[] poll = q.poll();int a = poll[0];int b = poll[1];ArrayList<Integer> arr = new ArrayList<Integer>();arr.add(nums1[a]);arr.add(nums2[b]);ans.add(arr);if(b+1<nums2.length && !mp.containsKey(Arrays.toString(new int[]{a,b+1}))){mp.put((Arrays.toString(new int[]{a,b+1})),1); q.add(new int[]{a,b+1});} if(a+1<nums1.length && !mp.containsKey(Arrays.toString(new int[]{a+1,b}))){q.add(new int[]{a+1,b}); mp.put((Arrays.toString(new int[]{a+1,b})),1);} if(a+1<nums1.length && b+1<nums2.length && !mp.containsKey(Arrays.toString(new int[]{a+1,b+1}))){q.add(new int[]{a+1,b+1});mp.put((Arrays.toString(new int[]{a+1,b+1})),1);}   }return ans;}
}
http://www.lryc.cn/news/495238.html

相关文章:

  • 常用函数的使用错题汇总
  • uniapp手机端一些坑记录
  • 2024学习之前端微信小程序开发教程,从入门到精通-含基础+实战+源码code
  • netconf 代码架构
  • 蒙特卡洛方法(Monte Carlo,MC)
  • python学习笔记8-函数2
  • 电商项目高级篇06-缓存
  • 使用 `aircrack-ng`扫描、获取握手包
  • 基于大数据python 酒店数据分析可视化大屏系统(源码+LW+部署讲解+数据库+ppt)
  • uniapp中父组件调用子组件方法
  • STL算法之set相关算法
  • vscode中json文件的注释飘红
  • 【微服务】SpringBoot 整合Redis Stack 构建本地向量数据库相似性查询
  • 三:安装服务-controller node
  • 自定义类型: 结构体、枚举 、联合
  • Bert+CRF的NER实战
  • 永久停用PostgreSQL 归档功能
  • 《数字图像处理基础》学习07-图像几何变换之最近邻插值法放大图像
  • pip安装库时报错(请求超时)
  • XPath表达式详解及其在Web开发中的应用
  • Qt中Socket网络编程
  • 【05】Selenium+Python 两种文件上传方式(AutoIt)
  • Python网络编程
  • openssl生成ca证书
  • Oracle RAC 环境下数据文件误建在本地目录的处理过程
  • 新质驱动·科东软件受邀出席2024智能网联+低空经济暨第二届湾区汽车T9+N闭门会议
  • windows11 使用体验记录
  • 202页MES项目需求方案深入解读,学习MES系统设计规划
  • 前端css实例
  • YOLO的框架及版本迭代