mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
update 0232.用栈实现队列.md about rust
This commit is contained in:
@ -622,6 +622,48 @@ class MyQueue() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
rust:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
struct MyQueue {
|
||||||
|
stack_in: Vec<i32>,
|
||||||
|
stack_out: Vec<i32>,
|
||||||
|
}
|
||||||
|
impl MyQueue {
|
||||||
|
|
||||||
|
fn new() -> Self {
|
||||||
|
MyQueue {
|
||||||
|
stack_in: Vec::new(),
|
||||||
|
stack_out: Vec::new(),
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push(&mut self, x: i32) {
|
||||||
|
self.stack_in.push(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pop(&mut self) -> i32 {
|
||||||
|
if self.stack_out.is_empty(){
|
||||||
|
while !self.stack_in.is_empty() {
|
||||||
|
self.stack_out.push(self.stack_in.pop().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.stack_out.pop().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn peek(&mut self) -> i32 {
|
||||||
|
let res = self.pop();
|
||||||
|
self.stack_out.push(res);
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
fn empty(&self) -> bool {
|
||||||
|
self.stack_in.is_empty() && self.stack_out.is_empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user