GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leet-code/Java/HashWithArray.java at master · awangdev/leet-code

Expand file tree

/

HashWithArray.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
executable file
·
69 lines (55 loc) · 1.21 KB
/

HashWithArray.java

Copy path

File metadata and controls

executable file
·
69 lines (55 loc) · 1.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
E
```
/*
Self Test:
Implement HashTable with just array and integer.
Thoughts:
A simple approach is to % size of the array, if the key exist, move 1 slot over.
A bug will be: when exceeds the size of array, there will be no avialable space,
and it'll run into error.
Inspired here :http://www.algolist.net/Data_structures/Hash_table/Simple_example
1. create a entry class.
2. hash the key, and put Entry into that hased index.
*/
Class Entry{
int key;
int value;
public Entry(int key, int value) {
this.key = key;
this.value = value;
}
public getKey(){
return this.key;
}
public getValue() {
return this.value;
}
}
Class HashMap {
int[] table;
int SIZE = 128;
public HashMap(){
table = new int[SIZE];
for (int i = 0; i < SIZE; i++) {
table[i] = null;
}
}
public void put(int key, int value){
int hash = key % SIZE;
while (table[hash] != null && table[hash].getKey() != key) {
hash = (hash + 1) % SIZE;
}
table[key] = new Entry(key, value);
}
public int get(int key) {
int hash = key % SIZE;
while (table[hash] != null && table[hash].getKey() != key) {
hash = (hash + 1) % SIZE;
}
if (table[hash] == null) {
return -1;
}
return table[hash].getValue();
}
}
```
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.