【哈希表】HashSet HashMap LeetCode习题

目录

136.只出现一次的数字

[137.只出现一次的数字 ||](#137.只出现一次的数字 ||)

217.存在重复元素

[219.存在重复元素 ||](#219.存在重复元素 ||)

771.宝石与石头

旧键盘(牛客)


首先需要导包 import java.utli.*;

表中常用的是前两个,时间复杂度低。O(1)

Set<E> set = new HashSet<>();

set.contains(E);

set.add(E);

set.remove(E);

返回值都为boolean
Map<K,V> map = new HashMap<>(); 键值对 (key , value)

map.put(K,V);

map.get(K); 返回值为V

|-------------|--------|-----------|---------------------------------------|
| | 背后 | 时间复杂度 | 常用方法 |
| HashSet | 哈希表 | O(1) | add() remove() contains() |
| HashMap | 哈希表 | O(1) | put() get() |
| TreeSet | 二叉搜索树 | O(logn) | |
| TreeMap | 二叉搜索树 | O(logn) | |

136.只出现一次的数字

使用异或的特点 或HashSet的特点都行

0^A=A ; A^A=0

算法代码

java 复制代码
class Solution {
    public int singleNumber(int[] nums) {
        
        // 1 使用异或   0^A=A   A^A=0
        /*int tmp = 0;
        for(int i=0;i<nums.length;i++) {
            tmp = tmp^nums[i];
        }
        return tmp;*/

       // 2 使用HashSet的特点 不重复
        Set<Integer> set = new HashSet<>();   
        for(int i=0;i<nums.length;i++) {
            if(set.contains(nums[i])) {
                set.remove(nums[i]);
            }else {
                set.add(nums[i]);
            }
        }
        for(int i=0;i<nums.length;i++) {
            if(set.contains(nums[i])) {
                return nums[i];
            }
        }
        return -1;
    }
}

137.只出现一次的数字 ||

依旧是用 HashSet 或 异或 也能做

java 复制代码
class Solution {
    public int singleNumber(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for(int i=0;i<nums.length;i++) {
            if(set.contains(nums[i])) {
                set.remove(nums[i]);
            }else {
                set.add(nums[i]);
            }
        }

        for(int i=0;i<nums.length;i++) {
            if(set.contains(nums[i])) {
                return nums[i];
            }
        }

        return -1;
    }
}

217.存在重复元素

首先想到HashSet,HashMap都可以做,但时间复杂度上HashSet更适合。O(1)

java 复制代码
class Solution {
    public boolean containsDuplicate(int[] nums) {
        /*Map<Integer,Integer> map = new HashMap<>();
        for(int i:nums) {
            if(map.get(i) == null) {
                map.put(i,1);
            }else{
                return true;
            }
        }

        return false;*/


        Set<Integer> set = new HashSet<>();
        for(int i:nums) {
            if(!set.add(i)) {
                return true;
            }
        }
        return false;

    }
}

219.存在重复元素 ||

首先想到HashSet,HashMap,但不同的是

这道题由于与元素和索引都有关,故 MapSet 更适合。

java 复制代码
import java.util.*;
class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer,Integer> map = new HashMap<>();
        
        for(int i=0;i<nums.length;i++) {
            if(map.get(nums[i]) == null) {
                map.put(nums[i],i);
                
            }else {
                if(Math.abs(i-map.get(nums[i])) <= k) {
                    return true;
                }else{
                     map.put(nums[i],i);
                }
                
            }
        }
        return false;
    }
}

771.宝石与石头

使用 HashSet TreeSet 都行,

不过 HashSet 背后是 二叉搜索树**,时间复杂度是** O(logn)。

TreeSet 背后是 哈希表**,时间复杂度是** O(1)

相同点都是 存储的值 不重复的。
故以后刷题就用 HashSet HashMap,背后都是 哈希表,时间复杂度低。

jewels都是唯一的。

java 复制代码
class Solution {
    public int numJewelsInStones(String jewels, String stones) {
        int count = 0;
        HashSet<Character> hash = new HashSet<>();
        //TreeSet<Character> hash = new TreeSet<>();  
        for(int i=0;i<jewels.length();i++) {
            hash.add(jewels.charAt(i));
        }

        for(int i=0;i<stones.length();i++) {
            if(hash.contains(stones.charAt(i))) {
                count++;
            }
        }
        return count;

    }
}

旧键盘(牛客)

依旧使用hashSet

题目要求字母是大写输出,故先变为大写,再把少的存入HashSet。再用方法contains()进行比较。

java 复制代码
import java.util.Scanner;
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
 
        while (in.hasNext()) {
            String a = in.nextLine();
            String b = in.nextLine();
            func(a,b);
        }
    }
    public static void func(String s1,String s2) {
        HashSet<Character> hash1 = new HashSet<>();
        //HashSet<Character> hash2 = new HashSet<>();
        for(char ch:s2.toUpperCase().toCharArray()) {
            hash1.add(ch);
        }
        for(char ch:s1.toUpperCase().toCharArray()) {
            if(!hash1.contains(ch)) {
                hash1.add(ch);
                System.out.print(ch);
            }
        }
    }
 
}
相关推荐
季明洵7 分钟前
C语言实现单链表
c语言·开发语言·数据结构·算法·链表
shandianchengzi12 分钟前
【小白向】错位排列|图文解释公考常见题目错位排列的递推式Dn=(n-1)(Dn-2+Dn-1)推导方式
笔记·算法·公考·递推·排列·考公
I_LPL12 分钟前
day26 代码随想录算法训练营 回溯专题5
算法·回溯·hot100·求职面试·n皇后·解数独
Yeats_Liao13 分钟前
评估体系构建:基于自动化指标与人工打分的双重验证
运维·人工智能·深度学习·算法·机器学习·自动化
怒放吧德德14 分钟前
后端 Mock 实战:Spring Boot 3 实现入站 & 出站接口模拟
java·后端·设计
only-qi16 分钟前
leetcode19. 删除链表的倒数第N个节点
数据结构·链表
cpp_250117 分钟前
P9586 「MXOI Round 2」游戏
数据结构·c++·算法·题解·洛谷
浅念-21 分钟前
C语言编译与链接全流程:从源码到可执行程序的幕后之旅
c语言·开发语言·数据结构·经验分享·笔记·学习·算法
biyezuopinvip26 分钟前
基于Spring Boot的企业网盘的设计与实现(任务书)
java·spring boot·后端·vue·ssm·任务书·企业网盘的设计与实现
脸大是真的好~30 分钟前
EasyExcel的使用
java·excel