Skip to content

Commit 375a1c7

Browse files
committed
feat: add solutions to leetcode problems: No.0705,No.0706
1 parent 74411c8 commit 375a1c7

File tree

8 files changed

+369
-6
lines changed

8 files changed

+369
-6
lines changed

solution/0700-0799/0705.Design HashSet/README.md

+58-1
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,79 @@ hashSet.contains(2);    // 返回 false (已经被删除)
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45+
数组实现。
46+
4547
<!-- tabs:start -->
4648

4749
### **Python3**
4850

4951
<!-- 这里可写当前语言的特殊实现逻辑 -->
5052

5153
```python
54+
class MyHashSet:
55+
56+
def __init__(self):
57+
"""
58+
Initialize your data structure here.
59+
"""
60+
self.data = [False] * 1000001
61+
62+
def add(self, key: int) -> None:
63+
self.data[key] = True
64+
65+
def remove(self, key: int) -> None:
66+
self.data[key] = False
5267

68+
def contains(self, key: int) -> bool:
69+
"""
70+
Returns true if this set contains the specified element
71+
"""
72+
return self.data[key]
73+
74+
75+
76+
# Your MyHashSet object will be instantiated and called as such:
77+
# obj = MyHashSet()
78+
# obj.add(key)
79+
# obj.remove(key)
80+
# param_3 = obj.contains(key)
5381
```
5482

5583
### **Java**
5684

5785
<!-- 这里可写当前语言的特殊实现逻辑 -->
5886

5987
```java
60-
88+
class MyHashSet {
89+
90+
private boolean[] data;
91+
92+
/** Initialize your data structure here. */
93+
public MyHashSet() {
94+
data = new boolean[1000001];
95+
}
96+
97+
public void add(int key) {
98+
data[key] = true;
99+
}
100+
101+
public void remove(int key) {
102+
data[key] = false;
103+
}
104+
105+
/** Returns true if this set contains the specified element */
106+
public boolean contains(int key) {
107+
return data[key];
108+
}
109+
}
110+
111+
/**
112+
* Your MyHashSet object will be instantiated and called as such:
113+
* MyHashSet obj = new MyHashSet();
114+
* obj.add(key);
115+
* obj.remove(key);
116+
* boolean param_3 = obj.contains(key);
117+
*/
61118
```
62119

63120
### **...**

solution/0700-0799/0705.Design HashSet/README_EN.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,68 @@ hashSet.contains(2); &nbsp;&nbsp;&nbsp;// returns false (already removed)
5757
### **Python3**
5858

5959
```python
60+
class MyHashSet:
6061

62+
def __init__(self):
63+
"""
64+
Initialize your data structure here.
65+
"""
66+
self.data = [False] * 1000001
67+
68+
def add(self, key: int) -> None:
69+
self.data[key] = True
70+
71+
def remove(self, key: int) -> None:
72+
self.data[key] = False
73+
74+
def contains(self, key: int) -> bool:
75+
"""
76+
Returns true if this set contains the specified element
77+
"""
78+
return self.data[key]
79+
80+
81+
82+
# Your MyHashSet object will be instantiated and called as such:
83+
# obj = MyHashSet()
84+
# obj.add(key)
85+
# obj.remove(key)
86+
# param_3 = obj.contains(key)
6187
```
6288

6389
### **Java**
6490

6591
```java
66-
92+
class MyHashSet {
93+
94+
private boolean[] data;
95+
96+
/** Initialize your data structure here. */
97+
public MyHashSet() {
98+
data = new boolean[1000001];
99+
}
100+
101+
public void add(int key) {
102+
data[key] = true;
103+
}
104+
105+
public void remove(int key) {
106+
data[key] = false;
107+
}
108+
109+
/** Returns true if this set contains the specified element */
110+
public boolean contains(int key) {
111+
return data[key];
112+
}
113+
}
114+
115+
/**
116+
* Your MyHashSet object will be instantiated and called as such:
117+
* MyHashSet obj = new MyHashSet();
118+
* obj.add(key);
119+
* obj.remove(key);
120+
* boolean param_3 = obj.contains(key);
121+
*/
67122
```
68123

69124
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class MyHashSet {
2+
3+
private boolean[] data;
4+
5+
/** Initialize your data structure here. */
6+
public MyHashSet() {
7+
data = new boolean[1000001];
8+
}
9+
10+
public void add(int key) {
11+
data[key] = true;
12+
}
13+
14+
public void remove(int key) {
15+
data[key] = false;
16+
}
17+
18+
/** Returns true if this set contains the specified element */
19+
public boolean contains(int key) {
20+
return data[key];
21+
}
22+
}
23+
24+
/**
25+
* Your MyHashSet object will be instantiated and called as such:
26+
* MyHashSet obj = new MyHashSet();
27+
* obj.add(key);
28+
* obj.remove(key);
29+
* boolean param_3 = obj.contains(key);
30+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class MyHashSet:
2+
3+
def __init__(self):
4+
"""
5+
Initialize your data structure here.
6+
"""
7+
self.data = [False] * 1000001
8+
9+
def add(self, key: int) -> None:
10+
self.data[key] = True
11+
12+
def remove(self, key: int) -> None:
13+
self.data[key] = False
14+
15+
def contains(self, key: int) -> bool:
16+
"""
17+
Returns true if this set contains the specified element
18+
"""
19+
return self.data[key]
20+
21+
22+
# Your MyHashSet object will be instantiated and called as such:
23+
# obj = MyHashSet()
24+
# obj.add(key)
25+
# obj.remove(key)
26+
# param_3 = obj.contains(key)

solution/0700-0799/0706.Design HashMap/README.md

+68-2
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,88 @@ hashMap.get(2); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 返回 -1 (未找到
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
数组实现。
47+
4648
<!-- tabs:start -->
4749

4850
### **Python3**
4951

5052
<!-- 这里可写当前语言的特殊实现逻辑 -->
5153

5254
```python
53-
55+
class MyHashMap:
56+
57+
def __init__(self):
58+
"""
59+
Initialize your data structure here.
60+
"""
61+
self.data = [-1] * 1000001
62+
63+
def put(self, key: int, value: int) -> None:
64+
"""
65+
value will always be non-negative.
66+
"""
67+
self.data[key] = value
68+
69+
def get(self, key: int) -> int:
70+
"""
71+
Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
72+
"""
73+
return self.data[key]
74+
75+
def remove(self, key: int) -> None:
76+
"""
77+
Removes the mapping of the specified value key if this map contains a mapping for the key
78+
"""
79+
self.data[key] = -1
80+
81+
82+
83+
# Your MyHashMap object will be instantiated and called as such:
84+
# obj = MyHashMap()
85+
# obj.put(key,value)
86+
# param_2 = obj.get(key)
87+
# obj.remove(key)
5488
```
5589

5690
### **Java**
5791

5892
<!-- 这里可写当前语言的特殊实现逻辑 -->
5993

6094
```java
61-
95+
class MyHashMap {
96+
97+
private int[] data;
98+
99+
/** Initialize your data structure here. */
100+
public MyHashMap() {
101+
data = new int[1000001];
102+
Arrays.fill(data, -1);
103+
}
104+
105+
/** value will always be non-negative. */
106+
public void put(int key, int value) {
107+
data[key] = value;
108+
}
109+
110+
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
111+
public int get(int key) {
112+
return data[key];
113+
}
114+
115+
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
116+
public void remove(int key) {
117+
data[key] = -1;
118+
}
119+
}
120+
121+
/**
122+
* Your MyHashMap object will be instantiated and called as such:
123+
* MyHashMap obj = new MyHashMap();
124+
* obj.put(key,value);
125+
* int param_2 = obj.get(key);
126+
* obj.remove(key);
127+
*/
62128
```
63129

64130
### **...**

solution/0700-0799/0706.Design HashMap/README_EN.md

+66-2
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,77 @@ hashMap.get(2); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// returns -1 (not foun
5757
### **Python3**
5858

5959
```python
60-
60+
class MyHashMap:
61+
62+
def __init__(self):
63+
"""
64+
Initialize your data structure here.
65+
"""
66+
self.data = [-1] * 1000001
67+
68+
def put(self, key: int, value: int) -> None:
69+
"""
70+
value will always be non-negative.
71+
"""
72+
self.data[key] = value
73+
74+
def get(self, key: int) -> int:
75+
"""
76+
Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
77+
"""
78+
return self.data[key]
79+
80+
def remove(self, key: int) -> None:
81+
"""
82+
Removes the mapping of the specified value key if this map contains a mapping for the key
83+
"""
84+
self.data[key] = -1
85+
86+
87+
88+
# Your MyHashMap object will be instantiated and called as such:
89+
# obj = MyHashMap()
90+
# obj.put(key,value)
91+
# param_2 = obj.get(key)
92+
# obj.remove(key)
6193
```
6294

6395
### **Java**
6496

6597
```java
66-
98+
class MyHashMap {
99+
100+
private int[] data;
101+
102+
/** Initialize your data structure here. */
103+
public MyHashMap() {
104+
data = new int[1000001];
105+
Arrays.fill(data, -1);
106+
}
107+
108+
/** value will always be non-negative. */
109+
public void put(int key, int value) {
110+
data[key] = value;
111+
}
112+
113+
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
114+
public int get(int key) {
115+
return data[key];
116+
}
117+
118+
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
119+
public void remove(int key) {
120+
data[key] = -1;
121+
}
122+
}
123+
124+
/**
125+
* Your MyHashMap object will be instantiated and called as such:
126+
* MyHashMap obj = new MyHashMap();
127+
* obj.put(key,value);
128+
* int param_2 = obj.get(key);
129+
* obj.remove(key);
130+
*/
67131
```
68132

69133
### **...**

0 commit comments

Comments
 (0)