Skip to content

Commit 5a05d29

Browse files
authored
feat: add rust solution to lc problem: No.0225 (#1247)
1 parent 4a7a4a4 commit 5a05d29

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

solution/0200-0299/0225.Implement Stack using Queues/README.md

+54
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,60 @@ private:
204204
*/
205205
```
206206
207+
### **Rust**
208+
209+
```rust
210+
use std::collections::VecDeque;
211+
212+
struct MyStack {
213+
/// There could only be two status at all time
214+
/// 1. One contains N elements, the other is empty
215+
/// 2. One contains N - 1 elements, the other contains exactly 1 element
216+
q_1: VecDeque<i32>,
217+
q_2: VecDeque<i32>,
218+
// Either 1 or 2, originally begins from 1
219+
index: i32,
220+
}
221+
222+
impl MyStack {
223+
fn new() -> Self {
224+
Self {
225+
q_1: VecDeque::new(),
226+
q_2: VecDeque::new(),
227+
index: 1,
228+
}
229+
}
230+
231+
fn move_data(&mut self) {
232+
// Always move from q1 to q2
233+
assert!(self.q_2.len() == 1);
234+
while !self.q_1.is_empty() {
235+
self.q_2.push_back(self.q_1.pop_front().unwrap());
236+
}
237+
let tmp = self.q_1.clone();
238+
self.q_1 = self.q_2.clone();
239+
self.q_2 = tmp;
240+
}
241+
242+
fn push(&mut self, x: i32) {
243+
self.q_2.push_back(x);
244+
self.move_data();
245+
}
246+
247+
fn pop(&mut self) -> i32 {
248+
self.q_1.pop_front().unwrap()
249+
}
250+
251+
fn top(&mut self) -> i32 {
252+
*self.q_1.front().unwrap()
253+
}
254+
255+
fn empty(&self) -> bool {
256+
self.q_1.is_empty()
257+
}
258+
}
259+
```
260+
207261
### **Go**
208262

209263
```go

solution/0200-0299/0225.Implement Stack using Queues/README_EN.md

+54
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,60 @@ private:
180180
*/
181181
```
182182
183+
### **Rust**
184+
185+
```rust
186+
use std::collections::VecDeque;
187+
188+
struct MyStack {
189+
/// There could only be two status at all time
190+
/// 1. One contains N elements, the other is empty
191+
/// 2. One contains N - 1 elements, the other contains exactly 1 element
192+
q_1: VecDeque<i32>,
193+
q_2: VecDeque<i32>,
194+
// Either 1 or 2, originally begins from 1
195+
index: i32,
196+
}
197+
198+
impl MyStack {
199+
fn new() -> Self {
200+
Self {
201+
q_1: VecDeque::new(),
202+
q_2: VecDeque::new(),
203+
index: 1,
204+
}
205+
}
206+
207+
fn move_data(&mut self) {
208+
// Always move from q1 to q2
209+
assert!(self.q_2.len() == 1);
210+
while !self.q_1.is_empty() {
211+
self.q_2.push_back(self.q_1.pop_front().unwrap());
212+
}
213+
let tmp = self.q_1.clone();
214+
self.q_1 = self.q_2.clone();
215+
self.q_2 = tmp;
216+
}
217+
218+
fn push(&mut self, x: i32) {
219+
self.q_2.push_back(x);
220+
self.move_data();
221+
}
222+
223+
fn pop(&mut self) -> i32 {
224+
self.q_1.pop_front().unwrap()
225+
}
226+
227+
fn top(&mut self) -> i32 {
228+
*self.q_1.front().unwrap()
229+
}
230+
231+
fn empty(&self) -> bool {
232+
self.q_1.is_empty()
233+
}
234+
}
235+
```
236+
183237
### **Go**
184238

185239
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::collections::VecDeque;
2+
3+
struct MyStack {
4+
/// There could only be two status at all time
5+
/// 1. One contains N elements, the other is empty
6+
/// 2. One contains N - 1 elements, the other contains exactly 1 element
7+
q_1: VecDeque<i32>,
8+
q_2: VecDeque<i32>,
9+
// Either 1 or 2, originally begins from 1
10+
index: i32,
11+
}
12+
13+
impl MyStack {
14+
fn new() -> Self {
15+
Self {
16+
q_1: VecDeque::new(),
17+
q_2: VecDeque::new(),
18+
index: 1,
19+
}
20+
}
21+
22+
fn move_data(&mut self) {
23+
// Always move from q1 to q2
24+
assert!(self.q_2.len() == 1);
25+
while !self.q_1.is_empty() {
26+
self.q_2.push_back(self.q_1.pop_front().unwrap());
27+
}
28+
let tmp = self.q_1.clone();
29+
self.q_1 = self.q_2.clone();
30+
self.q_2 = tmp;
31+
}
32+
33+
fn push(&mut self, x: i32) {
34+
self.q_2.push_back(x);
35+
self.move_data();
36+
}
37+
38+
fn pop(&mut self) -> i32 {
39+
self.q_1.pop_front().unwrap()
40+
}
41+
42+
fn top(&mut self) -> i32 {
43+
*self.q_1.front().unwrap()
44+
}
45+
46+
fn empty(&self) -> bool {
47+
self.q_1.is_empty()
48+
}
49+
}

0 commit comments

Comments
 (0)