• Map和Set【OJ练习题】


    常用的Map和Set的使用方法

    1.数据去重

    假设有10万个数据,如何去重重复的数据,重复的数据只保留一份

    import java.util.Iterator;
    import java.util.Set;
    import java.util.TreeSet;
    
    public static void main(String[] args) {
           int[] array = {1,2,3,4,5,1,2};
           Set<Integer> set = new TreeSet<>();
            for (int i = 0; i < array.length; i++) {
                set.add(array[i]);
            }
            Iterator<Integer> integers = set.iterator();
            while(integers.hasNext()){
                System.out.print(integers.next() + " ");
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    运行结果:

    1 2 3 4 5

    2.统计出现的次数

    假设有10万个数据,统计每个数据出现的次数

    import java.util.*;
    
    public static void main(String[] args) {
            int[] array = {1,2,3,4,5,5,4,3,2,6};
            Map<Integer,Integer> map = new TreeMap<>();
            for(int a: array){
    
                if(map.get(a) == null){
                    map.put(a,1);
    
                }
                else{
                    int val = map.get(a);
                    map.put(a,val+1);
                }
            }
            Set<Map.Entry<Integer,Integer>> entrySet = map.entrySet();
            for(Map.Entry<Integer,Integer> entry : entrySet){
                System.out.println("数字:" + entry.getKey() + "  "+ "出现的次数:" +entry.getValue());
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    输出结果:

    数字:1 出现的次数:1
    数字:2 出现的次数:2
    数字:3 出现的次数:2
    数字:4 出现的次数:2
    数字:5 出现的次数:2
    数字:6 出现的次数:1

    数组中出现次数超过一半的数字

    描述
    给一个长度为 n 的数组,数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。
    例如输入一个长度为9的数组[1,2,3,2,2,2,5,4,2]。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。

    import java.util.*;
    
    import java.util.Map;
    import java.util.TreeMap;
    public class Solution {
        /**
         * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
         *
         * 
         * @param numbers int整型一维数组 
         * @return int整型
         */
        public int MoreThanHalfNum_Solution (int[] numbers) {
            Map<Integer,Integer> map = new TreeMap<>();
            for(Integer a:numbers){
                if(map.get(a) == null){
                    map.put(a,1);
                }
                else{
                    int tem = map.get(a);
                    map.put(a,tem+1);
                }
            }
            int len = numbers.length;
           for(Map.Entry<Integer,Integer> entry : map.entrySet()){
            if(entry.getValue() > len/2){
                return entry.getKey();
            }
           }
            return -1;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    缺失的第一个正整数

    描述
    给定一个无重复元素的整数数组nums,请你找出其中没有出现的最小的正整数
    在这里插入图片描述

    import java.util.*;
    
    import java.util.Map;
    import java.util.TreeMap;
    public class Solution {
        /**
         * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
         *
         * 
         * @param numbers int整型一维数组 
         * @return int整型
         */
        public int MoreThanHalfNum_Solution (int[] numbers) {
            Map<Integer,Integer> map = new TreeMap<>();
            for(Integer a:numbers){
                if(map.get(a) == null){
                    map.put(a,1);
                }
                else{
                    int tem = map.get(a);
                    map.put(a,tem+1);
                }
            }
            int len = numbers.length;
           for(Map.Entry<Integer,Integer> entry : map.entrySet()){
            if(entry.getValue() > len/2){
                return entry.getKey();
            }
           }
            return -1;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    只出现一次的数字

    描述:

    给你一个 非空 整数数组 nums ,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
    在这里插入图片描述
    思路:
    因为每个元素最多出现两次,出现第一次时将数据添加到Set集合中,第二次出现时将数据从Set集合中删除,最后Set集合里就只剩下只出现一次的元素

    class Solution {
        public int singleNumber(int[] nums) {
            TreeSet<Integer> set = new TreeSet<>();
           for(Integer a : nums){
               if(!set.contains(a)){
                   set.add(a);
               }
               else{
                   set.remove(a);
               }
           }
           for(Integer a : nums){
               if(set.contains(a)){
                   return a;
               }
           }
             return -1;
        }
          
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    随机链表的复制

    描述:
    给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。

    构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。

    例如,如果原链表中有 X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y 。

    返回复制链表的头节点。
    在这里插入图片描述
    思路:
    链表的深拷贝,就是要开辟新的内存空间来存放链表,用Map使链表中的每一个节点一一对应,形成键值对,然后使用Map方法使新生成的节点连接起来;
    在这里插入图片描述

    class Node {
        int val;
        Node next;
        Node random;
    
        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
    */
    
    class Solution {
        public Node copyRandomList(Node head) {
            Map<Node,Node> map = new HashMap<>();
            Node cur = head;
            while(cur != null){
                Node node = new Node(cur.val);
                map.put(cur,node);
                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
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    石头和宝石

    描述:

    给你一个字符串 jewels 代表石头中宝石的类型,另有一个字符串 stones 代表你拥有的石头。 stones 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。

    字母区分大小写,因此 “a” 和 “A” 是不同类型的石头。
    在这里插入图片描述
    思路:
    将宝石存入set集合,然后用contains方法判断石头中的个数

    lass Solution {
        public int numJewelsInStones(String jewels, String stones) {
              TreeSet<Character> set = new TreeSet<>();
            for(char a:jewels.toCharArray()){
                set.add(a);
            }
            int count = 0;
            for(char a: stones.toCharArray()){
                if(set.contains(a)){
                    count++;
                }
            }
            return count;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    【前端实例代码】仅使用 HTML 和 CSS 的动画登录表单(超简单)
    uniApp 引入iview
    Leetcode刷题day6|242.有效的字母异位词 ,349. 两个数组的交集, 202. 快乐数,1. 两数之和
    培养编程素养和代码风格的书籍推荐
    SparkSql批量插入或更新,保存数据到Mysql中
    【设计模式】JAVA Design Patterns——Factory Method(虚拟构造器模式)
    SpringSecurity - 启动流程分析(十二)- ExceptionTranslationFilter 过滤器
    代码优化技巧
    平衡二叉树c语言版
    Linux下时间相关接口
  • 原文地址:https://blog.csdn.net/weixin_65476629/article/details/134061021