class MyHashMap{ int N = 100003; int[] h; public MyHashMap(){ h = new int[N]; Arrays.fill(h,Integer.MIN_VALUE); } public void insert(int x){ int k = find(x); h[k] = x; } public boolean query(int x){ int k = find(x); return h[k]==x; } private int find(int x){ int k = hash(x); while(h[k]!=Integer.MIN_VALUE&&h[k]!=x){ k++; if(k==N) k = 0; } return k; } private int hash(int x){ return (x%N+N)%N;//哈希算法 } }
java手写hashmap
最新推荐文章于 2023-12-10 16:14:21 发布