-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode706.java
More file actions
130 lines (118 loc) · 3.78 KB
/
LeetCode706.java
File metadata and controls
130 lines (118 loc) · 3.78 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
public class LeetCode706 {
public static void main(String[] args) {
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1); // myHashMap 现在为 [[1,1]]
System.out.println(myHashMap);
myHashMap.put(2, 2); // myHashMap 现在为 [[1,1], [2,2]]
System.out.println(myHashMap);
myHashMap.get(1); // 返回 1 ,myHashMap 现在为 [[1,1], [2,2]]
System.out.println(myHashMap);
myHashMap.get(3); // 返回 -1(未找到),myHashMap 现在为 [[1,1], [2,2]]
System.out.println(myHashMap);
myHashMap.put(2, 1); // myHashMap 现在为 [[1,1], [2,1]](更新已有的值)
System.out.println(myHashMap);
myHashMap.get(2); // 返回 1 ,myHashMap 现在为 [[1,1], [2,1]]
System.out.println(myHashMap);
myHashMap.remove(2); // 删除键为 2 的数据,myHashMap 现在为 [[1,1]]
System.out.println(myHashMap);
myHashMap.get(2); // 返回 -1(未找到),myHashMap 现在为 [[1,1]]
System.out.println(myHashMap);
}
}
class MyHashMap {
class Node {
int key;
int value;
Node next;
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
Node[] buckets;
int size;
public MyHashMap() {
this.buckets = new Node[16];
this.size = 0;
}
public void put(int key, int value) {
// NOTE: 新的相同index的元素插在队尾(插在队首代码可以更简洁)
if (this.size >= this.buckets.length * 0.75) {
expand();
}
int index = Integer.valueOf(key).hashCode() % buckets.length;
Node cur = this.buckets[index];
if (cur == null) {
this.buckets[index] = new Node(key, value);
this.size++;
return;
}
while (true) {
if (cur.key == key) {
cur.value = value;
return;
}
if (cur.next == null) {
cur.next = new Node(key, value);
this.size++;
return;
}
cur = cur.next;
}
}
public int get(int key) {
int index = Integer.valueOf(key).hashCode() % buckets.length;
Node cur = this.buckets[index];
while (cur != null) {
if (cur.key == key) {
return cur.value;
}
cur = cur.next;
}
return -1;
}
public void remove(int key) {
int index = Integer.valueOf(key).hashCode() % buckets.length;
Node cur = this.buckets[index];
Node pre;
if (cur == null) {
return;
}
if (cur.key == key) {
this.buckets[index] = cur.next;
} else {
while (cur.next != null) {
pre = cur;
cur = cur.next;
if (cur.key == key) {
pre.next = cur.next;
}
}
}
}
private void expand() {
Node[] oldBuckets = this.buckets;
this.buckets = new Node[buckets.length * 2];
for (int i = 0; i < oldBuckets.length; i++) {
Node cur = oldBuckets[i];
while (cur != null) {
put(cur.key, cur.value);
cur = cur.next;
}
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[ ");
for (int i = 0; i < buckets.length; i++) {
Node cur = buckets[i];
while (cur != null) {
sb.append(String.format("[%d, %d],", cur.key, cur.value));
cur = cur.next;
}
}
sb.deleteCharAt(sb.length() - 1);
sb.append(" ]");
return sb.toString();
}
}