Skip to content

Commit 2de0a9d

Browse files
committed
feat: add solutions to lc problems: No.0217,0232,0380
- No.0217.Contains Duplicate - No.0232.Implement Queue using Stacks - No.0380.Insert Delete GetRandom O(1)
1 parent b4c1b21 commit 2de0a9d

File tree

10 files changed

+552
-12
lines changed

10 files changed

+552
-12
lines changed

solution/0200-0299/0217.Contains Duplicate/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@
4141

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

44+
**方法一:排序**
45+
46+
排序数组,然后两个相邻元素是否相同即可。
47+
48+
**方法二:哈希表**
49+
50+
遍历元素并记录,当第二次出现时,直接返回 `true`
51+
4452
<!-- tabs:start -->
4553

4654
### **Python3**
@@ -135,6 +143,32 @@ var containsDuplicate = function (nums) {
135143
};
136144
```
137145

146+
### **Rust**
147+
148+
```rust
149+
use std::collections::HashSet;
150+
impl Solution {
151+
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
152+
nums.iter().collect::<HashSet<&i32>>().len() != nums.len()
153+
}
154+
}
155+
```
156+
157+
```rust
158+
impl Solution {
159+
pub fn contains_duplicate(mut nums: Vec<i32>) -> bool {
160+
nums.sort();
161+
let n = nums.len();
162+
for i in 1..n {
163+
if nums[i - 1] == nums[i] {
164+
return true
165+
}
166+
}
167+
false
168+
}
169+
}
170+
```
171+
138172
### **...**
139173

140174
```

solution/0200-0299/0217.Contains Duplicate/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,32 @@ var containsDuplicate = function (nums) {
117117
};
118118
```
119119

120+
### **Rust**
121+
122+
```rust
123+
use std::collections::HashSet;
124+
impl Solution {
125+
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
126+
nums.iter().collect::<HashSet<&i32>>().len() != nums.len()
127+
}
128+
}
129+
```
130+
131+
```rust
132+
impl Solution {
133+
pub fn contains_duplicate(mut nums: Vec<i32>) -> bool {
134+
nums.sort();
135+
let n = nums.len();
136+
for i in 1..n {
137+
if nums[i - 1] == nums[i] {
138+
return true
139+
}
140+
}
141+
false
142+
}
143+
}
144+
```
145+
120146
### **...**
121147

122148
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use std::collections::HashSet;
2+
impl Solution {
3+
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
4+
nums.iter().collect::<HashSet<&i32>>().len() != nums.len()
5+
}
6+
}

solution/0200-0299/0232.Implement Queue using Stacks/README.md

+64
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,70 @@ class MyQueue {
237237
*/
238238
```
239239

240+
### **Rust**
241+
242+
```rust
243+
struct MyQueue {
244+
in_stack: Vec<i32>,
245+
out_stack: Vec<i32>,
246+
}
247+
248+
249+
/**
250+
* `&self` means the method takes an immutable reference.
251+
* If you need a mutable reference, change it to `&mut self` instead.
252+
*/
253+
impl MyQueue {
254+
255+
fn new() -> Self {
256+
Self {
257+
in_stack: vec![],
258+
out_stack: vec![],
259+
}
260+
}
261+
262+
fn push(&mut self, x: i32) {
263+
self.in_stack.push(x);
264+
}
265+
266+
fn pop(&mut self) -> i32 {
267+
if self.out_stack.is_empty() {
268+
self.fill_out();
269+
}
270+
self.out_stack.pop().unwrap()
271+
}
272+
273+
fn peek(&mut self) -> i32 {
274+
if self.out_stack.is_empty() {
275+
self.fill_out();
276+
}
277+
*self.out_stack.last().unwrap()
278+
}
279+
280+
fn empty(&self) -> bool {
281+
self.in_stack.is_empty() && self.out_stack.is_empty()
282+
}
283+
284+
fn fill_out(&mut self){
285+
let MyQueue { in_stack, out_stack } = self;
286+
if out_stack.is_empty() {
287+
while !in_stack.is_empty() {
288+
out_stack.push(in_stack.pop().unwrap());
289+
}
290+
}
291+
}
292+
}
293+
294+
/**
295+
* Your MyQueue object will be instantiated and called as such:
296+
* let obj = MyQueue::new();
297+
* obj.push(x);
298+
* let ret_2: i32 = obj.pop();
299+
* let ret_3: i32 = obj.peek();
300+
* let ret_4: bool = obj.empty();
301+
*/
302+
```
303+
240304
### **...**
241305

242306
```

solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md

+64
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,70 @@ class MyQueue {
219219
*/
220220
```
221221

222+
### **Rust**
223+
224+
```rust
225+
struct MyQueue {
226+
in_stack: Vec<i32>,
227+
out_stack: Vec<i32>,
228+
}
229+
230+
231+
/**
232+
* `&self` means the method takes an immutable reference.
233+
* If you need a mutable reference, change it to `&mut self` instead.
234+
*/
235+
impl MyQueue {
236+
237+
fn new() -> Self {
238+
Self {
239+
in_stack: vec![],
240+
out_stack: vec![],
241+
}
242+
}
243+
244+
fn push(&mut self, x: i32) {
245+
self.in_stack.push(x);
246+
}
247+
248+
fn pop(&mut self) -> i32 {
249+
if self.out_stack.is_empty() {
250+
self.fill_out();
251+
}
252+
self.out_stack.pop().unwrap()
253+
}
254+
255+
fn peek(&mut self) -> i32 {
256+
if self.out_stack.is_empty() {
257+
self.fill_out();
258+
}
259+
*self.out_stack.last().unwrap()
260+
}
261+
262+
fn empty(&self) -> bool {
263+
self.in_stack.is_empty() && self.out_stack.is_empty()
264+
}
265+
266+
fn fill_out(&mut self){
267+
let MyQueue { in_stack, out_stack } = self;
268+
if out_stack.is_empty() {
269+
while !in_stack.is_empty() {
270+
out_stack.push(in_stack.pop().unwrap());
271+
}
272+
}
273+
}
274+
}
275+
276+
/**
277+
* Your MyQueue object will be instantiated and called as such:
278+
* let obj = MyQueue::new();
279+
* obj.push(x);
280+
* let ret_2: i32 = obj.pop();
281+
* let ret_3: i32 = obj.peek();
282+
* let ret_4: bool = obj.empty();
283+
*/
284+
```
285+
222286
### **...**
223287

224288
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
struct MyQueue {
2+
in_stack: Vec<i32>,
3+
out_stack: Vec<i32>,
4+
}
5+
6+
7+
/**
8+
* `&self` means the method takes an immutable reference.
9+
* If you need a mutable reference, change it to `&mut self` instead.
10+
*/
11+
impl MyQueue {
12+
13+
fn new() -> Self {
14+
Self {
15+
in_stack: vec![],
16+
out_stack: vec![],
17+
}
18+
}
19+
20+
fn push(&mut self, x: i32) {
21+
self.in_stack.push(x);
22+
}
23+
24+
fn pop(&mut self) -> i32 {
25+
if self.out_stack.is_empty() {
26+
self.fill_out();
27+
}
28+
self.out_stack.pop().unwrap()
29+
}
30+
31+
fn peek(&mut self) -> i32 {
32+
if self.out_stack.is_empty() {
33+
self.fill_out();
34+
}
35+
*self.out_stack.last().unwrap()
36+
}
37+
38+
fn empty(&self) -> bool {
39+
self.in_stack.is_empty() && self.out_stack.is_empty()
40+
}
41+
42+
fn fill_out(&mut self){
43+
let MyQueue { in_stack, out_stack } = self;
44+
if out_stack.is_empty() {
45+
while !in_stack.is_empty() {
46+
out_stack.push(in_stack.pop().unwrap());
47+
}
48+
}
49+
}
50+
}
51+
52+
/**
53+
* Your MyQueue object will be instantiated and called as such:
54+
* let obj = MyQueue::new();
55+
* obj.push(x);
56+
* let ret_2: i32 = obj.pop();
57+
* let ret_3: i32 = obj.peek();
58+
* let ret_4: bool = obj.empty();
59+
*/

0 commit comments

Comments
 (0)