栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

有关哈希表练习题整理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有关哈希表练习题整理

1、两数之和

思路:使用哈希表,将值作为key,下标作为value存入,因为是两数之和,所以另一个数一定是目标-某个数,如果另一个数在哈希表里即可返回下标。

    public int[] twoSum(int[] nums, int target) {
        HashMap hashMap = new HashMap();

        for(int i = 0 ; i < nums.length ; i++){
            int other = target - nums[i];
            if(hashMap.containsKey(other)){
                return new int[]{hashMap.get(other), i};
            }
            //值作为哈希key 下标作为value
            hashMap.put(nums[i],i);
        }
        return new int[0];
    }
2、只出现一次的数字

 思路一:使用哈希表将每个数字出现的次数作为value,数字为key存入哈希表,再次循环哈希表,判断如果频率为1则找出

    public static int singleNumber(int[] nums) {
        HashMap hashMap = new HashMap<>();
        for(int i = 0 ; i < nums.length ; i++){
            if(hashMap.containsKey(nums[i])){
                Integer count = hashMap.get(nums[i]);
                hashMap.put(nums[i],++count);
            }else{
                hashMap.put(nums[i],1);
            }
        }
        for (Integer key : hashMap.keySet()) {
            if(hashMap.get(key)==1){
                return key;
            }
        }
        return 0;
    }

思路二:抑或运算

1、因为任意数和0进行异或运算都等于其自身

2、任意数和自己做抑或都为零

3、最后剩下的那个数肯定和0进行抑或等于自身

class Solution {
    public int singleNumber(int[] nums) {
        int single = 0;
        for(int num : nums){
           single ^= num;
        }
        return single;
    }
}
3、前K个高频元素

 思路一:将数字和频率作为key和value存入哈希表,再次循环做一个下标为频率的list哈希表,list存入对应频率的数字,下标>=k的数字即是答案

    public static int[] topKFrequent(int[] nums, int k) {
        ArrayList restList = new ArrayList<>();
        HashMap hashMap = new HashMap<>();
        int max = 1;
        for(int num : nums){
            if(hashMap.containsKey(num)){
                Integer count = hashMap.get(num);
                max = count+1 > max ? count+1 : max; //最高频率做下面数组的size
                hashMap.put(num,++count);
            }else{
                hashMap.put(num,1);
            }
        }
        List[] list = new List[max+1];
        for (Integer key : hashMap.keySet()) {
            int index = hashMap.get(key);
            if(list[index] == null){
                list[index] = new ArrayList<>();
            }
            list[index].add(key);
        }
        for(int i = list.length-1 ; i >= k; i--){
            List integers = list[i];
            if(integers != null){
                for(int num : integers){
                    restList.add(num);
                }
            }

        }
        int[] resArr = new int[restList.size()];
        for(int i = 0 ; i < resArr.length ; i++){
            resArr[i] = restList.get(i);
        }
        return resArr;
    }

思路二:以数字-频率为key和value做哈希表,在利用小顶堆,根据频率大小存入数字,因为是队列二叉树,频率最小的数字会放在根节点,每次存入的时候先和k进行比较再决定放不放入。

因为k 的取值范围是 [1, 数组中不相同的元素的个数],所以小顶堆的最大容量就是k。

    public static int[] topKFrequent2(int[] nums, int k) {
        HashMap hashMap = new HashMap<>();
        for(int num : nums){
            if(hashMap.containsKey(num)){
                Integer count = hashMap.get(num);
                hashMap.put(num,++count);
            }else{
                hashMap.put(num,1);
            }
        }

        //小顶堆
        PriorityQueue priorityQueue = new PriorityQueue<>(new Comparator() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //频率小的往上放
                return hashMap.get(o1) - hashMap.get(o2);
            }
        });
        for (Integer key : hashMap.keySet()) {
            if(priorityQueue.size() < k){
                priorityQueue.offer(key);
                //对比如果频率大于k再放入小顶堆
            }else if(hashMap.get(key) > hashMap.get(priorityQueue.peek())){
                priorityQueue.poll();
                priorityQueue.offer(key);
            }
        }
        int [] res = new int [k];
        int ind = 0;
        while(!priorityQueue.isEmpty()){
            res[ind++] = priorityQueue.poll();
        }

        return res;
    }
4、无重复字符的最长字符串

 思路一:用哈希表来确定是否重复字符,记录下标,字符串长度等于重复的下标-最开始的下标,最开始的下标用滑动窗口确定

    public static int lengthOfLongestSubstring(String s) {
        HashMap hashMap = new HashMap<>();
        int max = 0;
        int left = 0;
        //abcaa
        for(int i = 0 ; i  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号