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

算法练习(4):牛客在线编程05 哈希

package jz.bm;import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;public class bm5 {/*** BM50 两数之和*/public int[] twoSum (int[] numbers, int target) {int[] res = new int[2];HashMap<Integer, Integer> map = new HashMap<>();for (int i = 0; i < numbers.length; i++) {if (map.containsKey(target - numbers[i])) {res[0] = map.get(target - numbers[i]) + 1;res[1] = i + 1;return res;} else {map.put(numbers[i], i);}}return res;}/*** BM51 数组中出现次数超过一半的数字*/public int MoreThanHalfNum_Solution(int [] array) {HashMap<Integer, Integer> map = new HashMap<>();for (int i = 0; i < array.length; i++) {if (map.containsKey(array[i])) {map.put(array[i], map.get(array[i]) + 1);} else {map.put(array[i], 1);}if (map.get(array[i]) > array.length / 2) {return array[i];}}return array[0];}/*** BM52 数组中只出现一次的两个数字*/public int[] FindNumsAppearOnce (int[] array) {int[] res = new int[2];HashMap<Integer, Integer> map = new HashMap<>();for (int k : array) {if (map.containsKey(k)) {map.put(k, 2);} else {map.put(k, 1);}}ArrayList<Integer> list = new ArrayList<>();for (int j : array) {if (map.get(j) == 1) {list.add(j);}}res[0] = Math.min(list.get(0), list.get(1));res[1] = Math.max(list.get(0), list.get(1));return res;}/*** BM53 缺失的第一个正整数*/public int minNumberDisappeared (int[] nums) {Arrays.sort(nums);HashMap<Integer, Integer> map = new HashMap<>();for (int i = 0; i < nums.length; i++) {map.put(nums[i], i);}for (int i = 1; i < Integer.MAX_VALUE; i++) {if (!map.containsKey(i)) {return i;}}return -1;}/*** BM54 三数之和*/public ArrayList<ArrayList<Integer>> threeSum(int[] num) {ArrayList<ArrayList<Integer>> lists = new ArrayList<>();Arrays.sort(num);HashMap<Integer, Integer> map = new HashMap<>();for (int i = 0; i < num.length; i++) {map.put(num[i], i);}int a, b, c;for (int i = 0; i < num.length; i = map.get(a) + 1) {a = num[i];for (int j = i + 1; j < num.length; j = map.get(b) + 1) {b = num[j];c = -a - b;if (map.containsKey(c) && map.get(c) > j) {ArrayList<Integer> list = new ArrayList<>();list.add(a);list.add(b);list.add(c);lists.add(list);}}}return lists;}
}
http://www.lryc.cn/news/105883.html

相关文章:

  • 数字信号处理——频谱分析
  • [软件工程] 架构映射战略设计方案模板
  • Springboot MongoDB 事务
  • SAP自建表日志
  • ansible-kubeadm在线安装单masterk8s v1.19-v1.20版本
  • mongodb docker 及常用命令
  • 最新版本mac版Idea 激活Jerbel实现热部署
  • 基于Ubuntu22.04部署bcache模式ceph
  • 根据URL批量下载文件并压缩成zip文件
  • 机器学习笔记之优化算法(六)线搜索方法(步长角度;非精确搜索;Glodstein Condition)
  • Ant Design Pro 封装网络请求
  • 命令模式——请求发送者与接收者解耦
  • css 利用模糊属性 制作水滴
  • 怎么才能提升自己工作能力?
  • Android Framework 之 Zygote
  • 二叉树的中序遍历 LeetCode热题100
  • IOS + Appium自动化教程
  • 100个精选Python实战项目案例,在线无偿分享
  • JSON语法
  • PostMan+Jmeter+QTP工具介绍及安装
  • 2023电赛E题视觉部分
  • 算法工程师岗位面试必备,讲透深度学习面试题,详解人工智能生成式任务与AI大模型面试题
  • JVM基础篇-虚拟机栈
  • KepwareEX配置API REST接口
  • 【python】python求解矩阵的转置(详细讲解)
  • Mr. Cappuccino的第56杯咖啡——Mybatis拦截器
  • 容器化安装环境EFK搭建
  • 基于 Debian GNU/Linux 12 “书虫 “的Neptune 8.0 “Juna “来了
  • GDAL C++ API 学习之路 (4) Spatial Reference System篇 OGRSpatialReference类
  • RS232转Profinet网关rs232转网口需要如何设置