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

使用Set和Map解题思路

前言

Set和Map这两种数据结构,在解决一些题上,效率很高。跟大家简单分享一些题以及如何使用Set和Map去解决这些题目。

题目链接

136. 只出现一次的数字 - 力扣(LeetCode)

138. 随机链表的复制 - 力扣(LeetCode)

旧键盘 (20)__牛客网

692. 前K个高频单词 - 力扣(LeetCode)

解题思路

一些题目的代码实现 

import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class Test1 {//获取单词出现了多少次public static void main(String[] args) {String[] words={"Maybe","Maybe","hello","happy","sadness"};Map<String,Integer> map=countword(words);//使用map.entrySet()遍历mapSet<Map.Entry<String,Integer>> entrySet=map.entrySet();//entrySet里面放的是Map.Entry<k,v>类型的for(Map.Entry<String,Integer> s:entrySet){System.out.println("key "+s.getKey()+" "+"val "+s.getValue());}}private static Map<String,Integer> countword(String[] words) {Map<String,Integer> map=new HashMap<>();//统计每个单词出现了多少次for(String s:words){if(map.get(s)==null){//则没有出现过一次map.put(s,1);}else{int val=map.get(s);map.put(s,val+1);}}return map;}
}
import java.util.HashSet;
import java.util.Locale;
import java.util.Scanner;
import java.util.Set;public class Test2 {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) {//这个是应该被输入的String a = in.nextLine();//这个是实际上被输入的String b = in.nextLine();a=a.toUpperCase();b=b.toUpperCase();Set<Character> set=new HashSet<>();for(int i=0;i<b.length();i++){char ch=b.charAt(i);set.add(ch);}Set<Character> set1=new HashSet<>();for(int i=0;i<a.length();i++){char ch=a.charAt(i);if(!set.contains(ch)&&!set1.contains(ch)){set1.add(ch);System.out.print(ch);}}}}
}
import java.util.*;public class Test3 {public List<String> topKFrequent(String[] words, int k) {Map<String,Integer> map=new HashMap<>();//统计单词出现的次数for(String word:words){if(map.get(word)==null){map.put(word,1);}else{int val=map.get(word);map.put(word,val+1);}}//成为Top-k问题,创建小根堆PriorityQueue<Map.Entry<String,Integer>> minHeap=new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {@Overridepublic int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {if(o1.getValue().compareTo(o2.getValue())==0){return o2.getKey().compareTo(o1.getKey());}return o1.getValue().compareTo(o2.getValue());}});//遍历mapfor(Map.Entry<String,Integer> entry:map.entrySet()){if(minHeap.size()<k){minHeap.offer(entry);}else{Map.Entry<String,Integer> top=minHeap.peek();if(top.getValue().compareTo(entry.getValue())<0){minHeap.poll();minHeap.offer(entry);}if(top.getValue().compareTo(entry.getValue())==0){if(top.getKey().compareTo(entry.getKey())>0){minHeap.poll();minHeap.offer(entry);}}}}//此时的大根堆里面一定是前k个高频单词List<String> list=new ArrayList<>();for(int i=0;i<k;i++){Map.Entry<String,Integer> tmp=minHeap.poll();list.add(tmp.getKey());}//Collections专门用来处理集合Collections.reverse(list);return list;}public static void main(String[] args) {}
}

结语 

再见~

 

 

 

 

相关文章:

  • 25.4.30数据结构|并查集 路径压缩
  • 企业经营系统分类及功能详解
  • Java状态机实战:打造高扩展性的订单流程引擎(含源码详解与快照设计)
  • Memory Bank 不够用?Cline 全新 CRCT:省 token,依赖关系自行追踪
  • OpenGL-ES 学习(13) ---- Shader 编译和程序对象
  • 生产级RAG系统一些经验总结
  • 构建强大垂直领域AI数据能力
  • C++11新特性_自动类型推导_decltype
  • 第3篇:请求参数处理与数据校验
  • Narendra自适应控制器设计
  • 掉馅饼,八分之一到二分之一:《分析模式》漫谈59
  • Linux架构篇、第1章_02源码编译安装Apache HTTP Server 最新稳定版本是 2.4.62
  • 笔试专题(十三)
  • 59常用控件_QComboBox的使用
  • 渗透测试中的那些“水洞”:分析与防御
  • 【2025五一数学建模竞赛A题】 支路车流量推测问题|建模过程+完整代码论文全解全析
  • 2025五一数学建模C题完整分析论文(共36页)(含模型、可运行代码、数据)
  • 段永平浙大访谈精华:长期主义的知行合一
  • socket-IO复用技术
  • 治理和管理的区别
  • 美国防部监察机构扩大“群聊门”事件调查范围
  • 黄宾虹诞辰160周年|一次宾翁精品的大集结
  • 江西省公安厅警务保障部原主任辛卫平主动交代问题,正接受审查调查
  • 【社论】人工智能,年轻的事业
  • 我国成功发射卫星互联网低轨卫星
  • 流浪猫给车主造成困扰,长春一小区拟投药应对?律师:此举欠妥