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, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 0, 0, 0, 1, 0 },
{ 0, 1, 1, 0, 0, 0, 1, 1, 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(m2));
}
4.并查集
(1)对于传入的值有个包装类
(2)对于并查集类的初始化:
HashMap<V,Element> elementMap,
HashMap<Element,Element>fatherMap;
HashMap<Element,Integer> rankMap;//放头结点所在集合的大小,合并的时候用
(3)三个方法:findHead(Element element),isSameSet(V a, V b),union(V a, V b)
public static class Element<V> {
public V value;
public Element(V value){
this.value=value;
}
}
public static class UnionFindSet<V> {
public HashMap<V,Element<V>> elementMap;
public HashMap<Element<V>,Element<V>> fatherMap;
public HashMap<Element<V>,Integer> rankMap;
public UnionFindSet(List<V> list) {
elementMap=new HashMap<>();
fatherMap=new HashMap<>();
rankMap=new HashMap<>();
for (V v : list) {
Element<V> vElement = new Element<>(v);
elementMap.put(v,vElement);
fatherMap.put(vElement,vElement);
rankMap.put(vElement,1);
}
}
//给定一个ele,往上一直找,并把代表元素返回
private Element<V> findHead(Element<V> element) {
Stack<Element> stack=new Stack<>();
while(fatherMap.get(element)!=element){
stack.push(element);
element=fatherMap.get(element);
}
while(!stack.isEmpty()){
fatherMap.put(stack.pop(),element);
}
return element;
}
public boolean isSameSet(V a, V b) {
if(elementMap.containsKey(a)&&elementMap.containsKey(b)) {
Element<V> vElementA = elementMap.get(a);
Element<V> vElementB = elementMap.get(b);
if (findHead(vElementA) == findHead(vElementB)) {
return true;
}
}
return false;
}
public void union(V a, V b) {
if(elementMap.containsKey(a)&&elementMap.containsKey(b)){
Element<V> vElementA = elementMap.get(a);
Element<V> vElementB = elementMap.get(b);
Element<V> aF = findHead(vElementA);
Element<V> bF = findHead(vElementB);
if (aF != bF) {
Element<V> big=rankMap.get(aF)>rankMap.get(bF)?aF:bF;
Element<V> small=big==aF?bF:aF;
fatherMap.put(small,big);
rankMap.put(big,rankMap.get(aF)+rankMap.get(bF));
rankMap.remove(small);
}
}
}
}
public static void main(String[] args) {
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
UnionFindSet<Integer> integerUnionFindSet = new UnionFindSet<>(list);
integerUnionFindSet.union(1,2);
integerUnionFindSet.union(2,3);
System.out.println(integerUnionFindSet.isSameSet(1, 3));
System.out.println(integerUnionFindSet.isSameSet(1, 2));
}
本文介绍了哈希表的遍历方法,包括键、值及键值对的遍历;展示了如何设计一个随机池结构,用于快速插入和删除元素并实现随机获取;探讨了岛问题的解决方案,即如何遍历二维矩阵并标记已访问的元素;最后讲解了并查集的概念,包括其数据结构和find、isSameSet、union等核心操作的应用。这些内容深入浅出地展示了数据结构与算法在实际问题中的应用。

被折叠的 条评论
为什么被折叠?



