【java数据结构】HashMapOJ练习题

【java数据结构】HashMapOJ练习题

博客最后附有整篇博客的全部代码!!!

一、只出现一次的数字

只出现一次的数字

思路:

  1. 先遍历一遍数组,将所有元素全部存到HashMap集合中
  2. 再遍历一遍数组,获取每个值对应的values值,判断是否等于1

代码:

java 复制代码
 public int singleNumber(int[] nums) {
        HashMap<Integer,Integer> hashMap=new HashMap<>();
        for(int i=0;i<nums.length;i++){
            if(hashMap.containsKey(nums[i])){
                hashMap.put(nums[i],hashMap.get(nums[i])+1);
            }else{
                hashMap.put(nums[i],1);
            }
        }
        for(int i=0;i<nums.length;i++){
            if(hashMap.containsKey(nums[i])){
                if(hashMap.get(nums[i])==1){
                    return nums[i];
                }
            }
        }
        return -1;
    }

二 、随机链表的复制

随机链表的复制

思路:

  1. 定义HashMap来存储新老节点的映射关系
  2. 再遍历一遍,然后通过老节点的map.get().next和map.get().random获得新节点的next和random,最后再通过老节点的map.get(cur.next)和map.get(cur.random)映射出应赋值给新节点的next和random的值

map.get(cur).next = map.get(cur.next);

map.get(cur).random =map.get(cur.random);

代码:

java 复制代码
        public Node copyRandomList(Node head) {
            Node cur = head;
            HashMap<Node, Node> map = new HashMap<>();
            while (cur != null) {
                Node newNode = new Node(cur.val);
                map.put(cur,newNode);
                cur = cur.next;
            }
            cur=head;
            while (cur != null) {
                map.get(cur).next = map.get(cur.next);
                map.get(cur).random = map.get(cur.random);
                cur=cur.next;
            }
            return map.get(head);
        }

三 、宝石与石头

宝石与石头

思路:

  1. 现将字符串转化为字符数组
  2. 将石头的每个字符存储进HashMap中,然后遍历宝石,如果HashMap中存在宝石,通过map.get()获取values值,进行count++;最后返回count。

代码:

java 复制代码
    public int numJewelsInStones(String jewels, String stones) {
        char[] jewelChars = jewels.toCharArray();//宝石
        char[] stonesChars = stones.toCharArray();//石头
        int count = 0;
        HashMap<Character,Integer> map=new HashMap<>();
        for(int i = 0; i < stonesChars.length; i++){
            if(map.containsKey(stonesChars[i])){
                map.put(stonesChars[i],map.get(stonesChars[i])+1);
            }else{
                map.put(stonesChars[i],1);
            }
        }
        for(int i = 0; i<jewelChars.length; i++){
            if(map.containsKey(jewelChars[i])){
                count+=map.get(jewelChars[i]);
            }
        }
        return count;
    }

四、坏键盘打字

坏键盘打字

思路:

  1. 将所有的字符串全部进行大写转化
  2. 将输出的字存储进HashMap的map1集合中
  3. 遍历打印的字,并且只要求输出一遍我们将出现过的字符可以重新放到一个HashMap的map2集合中,判断map1和map2两个如果都不包含则进行打印。

代码:

java 复制代码
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str1= sc.nextLine();
        String str2=sc.nextLine();
        faultyKeyboard(str1,str2);
    }
    public static void faultyKeyboard(String str1,String str2){
        String str3=str1.toUpperCase();
        String str4=str2.toUpperCase();
        char[] str5=str3.toCharArray();
        char[] str6=str4.toCharArray();
        HashMap<Character,Integer> map1=new HashMap<>();
        HashMap<Character,Integer> map2=new HashMap<>();
        for (int i = 0; i < str6.length; i++) {
            if(map1.containsKey(str6[i])){
                map1.put(str6[i],map1.get(str6[i])+1);
            }else{
                map1.put(str6[i],1);
            }
        }
        for(int i=0;i<str5.length;i++){
            if(!map1.containsKey(str5[i])&&!map2.containsKey(str5[i])){
                map2.put(str5[i],1);
                System.out.print(str5[i]);
            }
        }
    }

五、前K个高频单词

前K个高频单词

思路:

  1. 将每个单词存储进HashMap的map集合
  2. 建立小根堆(在未建立好K大小的小根堆的时候,这个时候如果遇到频率相同的单词,需要j建立成大根堆)
  3. 遍历map,先建立好k个大小的小跟堆,堆顶元素小于入堆元素,则插入入堆元素;堆顶元素等于入堆元素,则判断单词大小,小的入堆

代码:

java 复制代码
    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String, Integer> map = new HashMap<>();
        for (String word : words) {
            map.put(word, map.getOrDefault(word, 0) + 1);
        }

        //2. 建立小根堆
        PriorityQueue<Map.Entry<String,Integer>> minHeap = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public 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());
            }
        });
        //3.遍历map
        for(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);
                }else if(top.getValue().compareTo(entry.getValue()) == 0) {
                    if(top.getKey().compareTo(entry.getKey()) > 0) {
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }
            }
        }
        ArrayList<String> list = new ArrayList<>();

        for (int i = 0; i < k; i++) {
            Map.Entry<String,Integer> tmp = minHeap.poll();
            list.add(tmp.getKey());
        }
        Collections.reverse(list);
        return list;
    }

此篇博客的全部代码!!!

相关推荐
慕y2743 分钟前
Java学习第十五部分——MyBatis
java·学习·mybatis
fie88894 分钟前
浅谈几种js设计模式
开发语言·javascript·设计模式
A__tao6 分钟前
SQL 转 Java 实体类工具
java·数据库·sql
喝可乐的布偶猫11 分钟前
Java类变量(静态变量)
java·开发语言·jvm
TDengine (老段)35 分钟前
TDengine STMT2 API 使用指南
java·大数据·物联网·时序数据库·iot·tdengine·涛思数据
喝可乐的布偶猫1 小时前
韩顺平之第九章综合练习-----------房屋出租管理系统
java·开发语言·ide·eclipse
Code季风1 小时前
深入理解微服务中的服务注册与发现(Consul)
java·运维·微服务·zookeeper·架构·go·consul
光军oi1 小时前
java微服务(Springboot篇)——————IDEA搭建第一个Springboot入门项目
java·spring boot·微服务
江山如画,佳人北望1 小时前
C#程序入门
开发语言·windows·c#
future14122 小时前
C#每日学习日记
java·学习·c#