mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #1724 from Jack-Zhang-1314/master
update 0225.用队列实现栈.md about rust
This commit is contained in:
@ -1018,7 +1018,7 @@ class MyStack {
|
||||
}
|
||||
}
|
||||
```
|
||||
> 单对列
|
||||
> 单队列
|
||||
```php
|
||||
class MyStack {
|
||||
public $queue;
|
||||
@ -1051,6 +1051,44 @@ class MyStack {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> rust:单队列
|
||||
|
||||
```rust
|
||||
struct MyStack {
|
||||
queue: Vec<i32>,
|
||||
}
|
||||
|
||||
impl MyStack {
|
||||
fn new() -> Self {
|
||||
MyStack { queue: vec![] }
|
||||
}
|
||||
|
||||
fn push(&mut self, x: i32) {
|
||||
self.queue.push(x);
|
||||
}
|
||||
|
||||
fn pop(&mut self) -> i32 {
|
||||
let len = self.queue.len() - 1;
|
||||
for _ in 0..len {
|
||||
let tmp = self.queue.remove(0);
|
||||
self.queue.push(tmp);
|
||||
}
|
||||
self.queue.remove(0)
|
||||
}
|
||||
|
||||
fn top(&mut self) -> i32 {
|
||||
let res = self.pop();
|
||||
self.queue.push(res);
|
||||
res
|
||||
}
|
||||
|
||||
fn empty(&self) -> bool {
|
||||
self.queue.is_empty()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
Reference in New Issue
Block a user