-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# 第五章 栈与队列part01 | ||
|
||
今日任务: | ||
● 理论基础 | ||
● 232.用栈实现队列 | ||
● 225. 用队列实现栈 | ||
理论基础 | ||
|
||
了解一下 栈与队列的内部实现机智,文中是以C++为例讲解的。 | ||
|
||
文章讲解:https://programmercarl.com/%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html | ||
|
||
## 232.用栈实现队列 | ||
|
||
大家可以先看视频,了解一下模拟的过程,然后写代码会轻松很多。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html | ||
|
||
|
||
## 225. 用队列实现栈 | ||
|
||
可以大家惯性思维,以为还要两个队列来模拟栈,其实只用一个队列就可以模拟栈了。 | ||
|
||
建议大家掌握一个队列的方法,更简单一些,可以先看视频讲解 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.html | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# 232. 用栈实现队列 | ||
|
||
## 题目描述 | ||
|
||
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty): | ||
|
||
实现 MyQueue 类: | ||
|
||
void push(int x) 将元素 x 推到队列的末尾 | ||
int pop() 从队列的开头移除并返回元素 | ||
int peek() 返回队列开头的元素 | ||
boolean empty() 如果队列为空,返回 true ;否则,返回 false | ||
说明: | ||
|
||
你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。 | ||
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。 | ||
|
||
## 解题思路 | ||
|
||
```rust | ||
|
||
|
||
struct MyQueue { | ||
is: Vec<i32>, | ||
os: Vec<i32>, | ||
} | ||
|
||
|
||
/** | ||
* `&self` means the method takes an immutable reference. | ||
* If you need a mutable reference, change it to `&mut self` instead. | ||
*/ | ||
impl MyQueue { | ||
|
||
fn new() -> Self { | ||
Self { is: vec![], os: vec![] } | ||
} | ||
|
||
fn o2i(&mut self) { | ||
while let Some(i) = self.os.pop() { | ||
self.is.push(i); | ||
} | ||
} | ||
|
||
fn i2o(&mut self) { | ||
while let Some(i) = self.is.pop() { | ||
self.os.push(i); | ||
} | ||
} | ||
|
||
fn push(&mut self, x: i32) { | ||
self.o2i(); | ||
self.is.push(x); | ||
} | ||
|
||
fn pop(&mut self) -> i32 { | ||
self.i2o(); | ||
self.os.pop().unwrap() | ||
} | ||
|
||
fn peek(&mut self) -> i32 { | ||
self.i2o(); | ||
self.os.last().copied().unwrap() | ||
} | ||
|
||
fn empty(&self) -> bool { | ||
self.is.is_empty() && self.os.is_empty() | ||
} | ||
} | ||
``` | ||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# 225. 用队列实现栈 | ||
|
||
## 题目描述 | ||
|
||
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。 | ||
|
||
实现 MyStack 类: | ||
|
||
void push(int x) 将元素 x 压入栈顶。 | ||
int pop() 移除并返回栈顶元素。 | ||
int top() 返回栈顶元素。 | ||
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。 | ||
|
||
|
||
## 解题思路 | ||
|
||
```rust | ||
|
||
struct MyStack { | ||
q: std::collections::VecDeque<i32>, | ||
} | ||
|
||
|
||
/** | ||
* `&self` means the method takes an immutable reference. | ||
* If you need a mutable reference, change it to `&mut self` instead. | ||
*/ | ||
impl MyStack { | ||
|
||
fn new() -> Self { | ||
Self { q: std::collections::VecDeque::new() } | ||
} | ||
|
||
fn push(&mut self, x: i32) { | ||
self.q.push_back(x); | ||
} | ||
|
||
fn pop(&mut self) -> i32 { | ||
let n = self.q.len(); | ||
for _ in 1..n { | ||
let x = self.q.pop_front().unwrap(); | ||
self.q.push_back(x); | ||
} | ||
self.q.pop_front().unwrap() | ||
} | ||
|
||
fn top(&mut self) -> i32 { | ||
let n = self.q.len(); | ||
for _ in 1..n { | ||
let x = self.q.pop_front().unwrap(); | ||
self.q.push_back(x); | ||
} | ||
let x = self.q.pop_front().unwrap(); | ||
self.q.push_back(x); | ||
return x; | ||
} | ||
|
||
fn empty(&self) -> bool { | ||
self.q.is_empty() | ||
} | ||
} | ||
|
||
``` | ||
## 学习感想 |