File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class ListNode {
2+ int key , val ;
3+ ListNode next ;
4+ public ListNode (int key , int val , ListNode next ) {
5+ this .key = key ;
6+ this .val = val ;
7+ this .next = next ;
8+ }
9+ }
10+ class MyHashMap {
11+ static final int size = 19997 ;
12+ static final int mult = 12582917 ;
13+ ListNode [] data ;
14+ public MyHashMap () {
15+ this .data = new ListNode [size ];
16+ }
17+ private int hash (int key ) {
18+ return (int )((long )key * mult % size );
19+ }
20+ public void put (int key , int val ) {
21+ remove (key );
22+ int h = hash (key );
23+ ListNode node = new ListNode (key , val , data [h ]);
24+ data [h ] = node ;
25+ }
26+ public int get (int key ) {
27+ int h = hash (key );
28+ ListNode node = data [h ];
29+ for (; node != null ; node = node .next )
30+ if (node .key == key ) return node .val ;
31+ return -1 ;
32+ }
33+ public void remove (int key ) {
34+ int h = hash (key );
35+ ListNode node = data [h ];
36+ if (node == null ) return ;
37+ if (node .key == key ) data [h ] = node .next ;
38+ else for (; node .next != null ; node = node .next )
39+ if (node .next .key == key ) {
40+ node .next = node .next .next ;
41+ return ;
42+ }
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments