左神_基础提升班_01_哈希函数与哈希表等

本文介绍了哈希表的遍历方法,包括键、值及键值对的遍历;展示了如何设计一个随机池结构,用于快速插入和删除元素并实现随机获取;探讨了岛问题的解决方案,即如何遍历二维矩阵并标记已访问的元素;最后讲解了并查集的概念,包括其数据结构和find、isSameSet、union等核心操作的应用。这些内容深入浅出地展示了数据结构与算法在实际问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.哈希函数与哈希表

//遍历键的集合
for(String key: hashMap.keySet()){
   System.out.println(key);
}
//遍历值的集合
for(String value:hashMap.values()){
   System.out.println(value);
}
//遍历hashMap集合
Set<Character> set=hashMap.keySet()
for(Character ch : set){
    System.out.println(ch+ch.get(ch));
}
//通过entrySet遍历
for(Map.Entry<String,String> entry : hashMap.entrySet()){
   String key=entry.getKey();
   String value=entry.getValue();
   System.out.println(key+"  "+value);
}
//通过entrySet集合删除元素,必须先收集键值,不能直接删
List<String> remoceList=new ArrayList<>();
for (Map.Entry<String, String> entry : hashMap.entrySet()) {
   if(!entry.getValue().equals("1")){
      remoceList.add(entry.getKey());
   }
}
for (String s : remoceList) {
   hashMap.remove(s);
}

2.设计RandomPool结构

	public static class Pool<K> {
		private HashMap<K, Integer> keyIndexMap;
		private HashMap<Integer, K> indexKeyMap;
		private int size;

		public Pool() {
			this.keyIndexMap = new HashMap<K, Integer>();
			this.indexKeyMap = new HashMap<Integer, K>();
			this.size = 0;
		}

		public void insert(K key) {
			if (!this.keyIndexMap.containsKey(key)) {
				this.keyIndexMap.put(key, this.size);
				this.indexKeyMap.put(this.size++, key);
			}
		}

		public void delete(K key) {
			if (this.keyIndexMap.containsKey(key)) {
				int deleteIndex = this.keyIndexMap.get(key);
				int lastIndex = --this.size;
				K lastKey = this.indexKeyMap.get(lastIndex);
				this.keyIndexMap.put(lastKey, deleteIndex);
				this.indexKeyMap.put(deleteIndex, lastKey);
				this.keyIndexMap.remove(key);
				this.indexKeyMap.remove(lastIndex);
			}
		}

		public K getRandom() {
			if (this.size == 0) {
				return null;
			}
			int randomIndex = (int) (Math.random() * this.size); // 0 ~ size -1
			return this.indexKeyMap.get(randomIndex);
		}

	}

	public static void main(String[] args) {
		Pool<String> pool = new Pool<String>();
		pool.insert("zuo");
		pool.insert("cheng");
		pool.insert("yun");
		System.out.println(pool.getRandom());
		System.out.println(pool.getRandom());
		System.out.println(pool.getRandom());
		System.out.println(pool.getRandom());
		System.out.println(pool.getRandom());
		System.out.println(pool.getRandom());

	}

3.岛问题
遍历,若为1,则感染成为2

	public static int countIslands(int[][] m) {
		if (m == null || m[0] == null) {
			return 0;
		}
		int N = m.length;
		int M = m[0].length;
		int res = 0;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				if (m[i][j] == 1) {
					res++;
					infect(m, i, j, N, M);
				}
			}
		}
		return res;
	}

	public static void infect(int[][] m, int i, int j, int N, int M) {
		if (i < 0 || i >= N || j < 0 || j >= M || m[i][j] != 1) {
			return;
		}
		//i,j没越界,并且当前位置为1
		m[i][j] = 2;
		infect(m, i + 1, j, N, M);
		infect(m, i - 1, j, N, M);
		infect(m, i, j + 1, N, M);
		infect(m, i, j - 1, N, M);
	}

	public static void main(String[] args) {
		int[][] m1 = {  { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
				        { 0, 1, 1, 1, 0, 1, 1, 1, 0 }, 
				        { 0, 1, 1, 1, 0, 0, 0, 1, 0 },
				        { 0, 1, 1, 0, 0, 0, 0, 0, 0 }, 
				        { 0, 0, 0, 0, 0, 1, 1, 0, 0 }, 
				        { 0, 0, 0, 0, 1, 1, 1, 0, 0 },
				        { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, };
		System.out.println(countIslands(m1));

		int[][] m2 = {  { 0, 0, 0, 0