Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
abace60
correct typos
StoneLyu May 7, 2019
f62deb8
Add translations
StoneLyu May 7, 2019
7d5f538
新增HTTP/1.0, HTTP/1.1, HTTP/2的对比
StoneLyu May 10, 2019
978f0e6
增加HTTP/1.1和HTTP/2的性能对比的Demo
StoneLyu May 10, 2019
badd1b1
翻译大部分基础数据结构,但是REACT部分没有翻译
StoneLyu May 10, 2019
45c53b5
update description of `HTTP/1.0`
StoneLyu May 29, 2019
53bd464
add WIP tag, update some translation details
StoneLyu May 29, 2019
7f6d697
Merge remote-tracking branch 'azl/master'
StoneLyu Jun 3, 2019
a888630
change 'fundamental' to 'basic'
StoneLyu Jun 5, 2019
76689b7
remove some trivial details about HTTP/1.0
StoneLyu Jun 5, 2019
83b48c0
Merge remote-tracking branch 'azl/master'
StoneLyu Jun 6, 2019
78d4f4b
Merge remote-tracking branch 'azl/master'
StoneLyu Jun 17, 2019
6e2add0
Merge remote-tracking branch 'azl/master'
StoneLyu Jun 20, 2019
1cb943e
error correction
StoneLyu Jun 21, 2019
2871ad1
translation for DP
StoneLyu Jun 21, 2019
2993775
Merge remote-tracking branch 'azl/master'
StoneLyu Jun 21, 2019
987d03b
translation for string problems
StoneLyu Jun 21, 2019
0526ab4
Merge remote-tracking branch 'azl/master'
StoneLyu Jun 21, 2019
10b87c1
Merge remote-tracking branch 'azl/master'
StoneLyu Jun 22, 2019
1574660
Merge remote-tracking branch 'azl/master'
StoneLyu Jul 8, 2019
aa90d5e
Merge remote-tracking branch 'azl/master'
StoneLyu Jul 9, 2019
da21132
error correction
StoneLyu Jul 11, 2019
f4f1e53
Merge remote-tracking branch 'azl/master'
StoneLyu Jul 12, 2019
5445058
Merge remote-tracking branch 'azl/master'
StoneLyu Jul 31, 2019
d8def59
Merge remote-tracking branch 'azl/master'
StoneLyu Aug 2, 2019
d124351
Merge remote-tracking branch 'azl/master'
StoneLyu Aug 11, 2019
7a66d89
correct wrongly typed
StoneLyu Aug 12, 2019
922dca8
translation for binary-tree-traversal.md
StoneLyu Aug 12, 2019
28d05ef
Merge remote-tracking branch 'azl/master'
StoneLyu Aug 12, 2019
33a2ee5
correct wrongly typed
StoneLyu Aug 12, 2019
e0136d0
update README.en.md
StoneLyu Aug 12, 2019
49affbd
update description of Huffman encoding
StoneLyu Aug 13, 2019
2cb016b
Merge remote-tracking branch 'azl/master'
StoneLyu Aug 13, 2019
8db781f
update file path of Binary-tree-traversal-en.md.
StoneLyu Aug 13, 2019
83acb43
correct wrongly typed
StoneLyu Aug 13, 2019
2806f2d
update paths of thinkings
StoneLyu Aug 13, 2019
0903e52
translation for basic-algorithm.md (Draft)
StoneLyu Aug 13, 2019
5c77911
translation for bloom-filter.md
StoneLyu Aug 13, 2019
66b122a
solve conflict in README.en.md
StoneLyu Aug 13, 2019
e2c3094
Merge remote-tracking branch 'azl/master'
StoneLyu Aug 13, 2019
111743c
Merge remote-tracking branch 'azl/master'
StoneLyu Aug 26, 2019
ba9572d
Merge remote-tracking branch 'azl/master'
StoneLyu Oct 9, 2019
c41748f
Merge remote-tracking branch 'azl/master'
StoneLyu Oct 24, 2019
f8edeb3
Merge remote-tracking branch 'azl/master'
StoneLyu Dec 14, 2019
366b72c
add java solution with inorder traversing
StoneLyu Dec 24, 2019
fc054c2
Update 230.kth-smallest-element-in-a-bst.md
azl397985856 Dec 24, 2019
779f714
add java implementations.
StoneLyu Dec 28, 2019
94db592
add java implementations
StoneLyu Dec 28, 2019
fc7a4cd
Merge remote-tracking branch 'refs/remotes/origin/master'
StoneLyu Dec 28, 2019
7e059d8
Merge remote-tracking branch 'azl/master'
StoneLyu Dec 28, 2019
dbd8e45
Update 94.binary-tree-inorder-traversal.md
azl397985856 Dec 30, 2019
b60184d
Update 98.validate-binary-search-tree.md
azl397985856 Dec 30, 2019
0eca3dd
add java implementation to 19
StoneLyu Dec 31, 2019
b66258d
Merge remote-tracking branch 'azl/master'
StoneLyu Dec 31, 2019
070c144
Merge branch 'master' of https://github.com/StoneLyu/leetcode
StoneLyu Dec 31, 2019
3d4cc23
fix conflict
StoneLyu Jan 1, 2020
8de4f14
fix typo
StoneLyu Jan 1, 2020
189450b
Merge remote-tracking branch 'azl/master'
StoneLyu Jan 1, 2020
3087fdb
fix typo
StoneLyu Jan 1, 2020
141d408
add java implementation. add more explanation.
StoneLyu Jan 2, 2020
fd048e9
Merge remote-tracking branch 'azl/master'
StoneLyu Jan 2, 2020
9055c6e
Update 232.implement-queue-using-stacks.md
azl397985856 Jan 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update 232.implement-queue-using-stacks.md
  • Loading branch information
azl397985856 authored Jan 3, 2020
commit 9055c6e0b45357e849e6fd7e3d7c36b1fdddf9a6
30 changes: 13 additions & 17 deletions problems/232.implement-queue-using-stacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ You may assume that all operations are valid (for example, no pop or peek operat

## 思路

这道题目是让我们用栈来模拟实现队列。 我们直到栈和队列都是一种受限的数据结构
这道题目是让我们用栈来模拟实现队列。 我们知道栈和队列都是一种受限的数据结构
栈的特点是只能在一端进行所有操作,队列的特点是只能在一端入队,另一端出队。

在这里我们可以借助另外一个栈,也就是说用两个栈来实现队列的效果。这种做法的时间复杂度和空间复杂度都是O(n)。
Expand Down Expand Up @@ -62,22 +62,6 @@ push之前是这样的:

- 在push的时候利用辅助栈(双栈)

## 为什么我们要用两个stack来实现一个queue

使用两个栈来替代一个队列的实现是为了在多进程中分开对同一个队列对读写操作。一个栈是用来读的,另一个是用来写的。当且仅当读栈满时或者写栈为空时,读写操作才会发生冲突。

当只有一个线程对栈进行读写操作的时候,总有一个栈是空的。在多线程应用中,如果我们只有一个队列,为了线程安全,我们在读或者写队列的时候都需要锁住整个队列。而在两个栈的实现中,只要写入栈不为空,那么`push`操作的锁就不会影响到`pop`。

## when should we use two stacks to implement a queue?

[reference](https://leetcode.com/problems/implement-queue-using-stacks/discuss/64284/Do-you-know-when-we-should-use-two-stacks-to-implement-a-queue)

The application for this implementation is to separate read & write of a queue in multi-processing. One of the stack is for read, and another is for write. They only interfere each other when the former one is full or latter is empty.

When there's only one thread doing the read/write operation to the stack, there will always one stack empty. However, in a multi-thread application, if we have only one queue, for thread-safety, either read or write will lock the whole queue. In the two stack implementation, as long as the second stack is not empty, push operation will not lock the stack for pop.

[further reading](https://stackoverflow.com/questions/2050120/why-use-two-stacks-to-make-a-queue/2050402#2050402)

## 代码

* 语言支持:JS, Python, Java
Expand Down Expand Up @@ -254,3 +238,15 @@ class MyQueue {
## 扩展
- 类似的题目有用队列实现栈,思路是完全一样的,大家有兴趣可以试一下。
- 栈混洗也是借助另外一个栈来完成的,从这点来看,两者有相似之处。

## 延伸阅读

实际上现实中也有使用两个栈来实现队列的情况,那么为什么我们要用两个stack来实现一个queue?

其实使用两个栈来替代一个队列的实现是为了在多进程中分开对同一个队列对读写操作。一个栈是用来读的,另一个是用来写的。当且仅当读栈满时或者写栈为空时,读写操作才会发生冲突。

当只有一个线程对栈进行读写操作的时候,总有一个栈是空的。在多线程应用中,如果我们只有一个队列,为了线程安全,我们在读或者写队列的时候都需要锁住整个队列。而在两个栈的实现中,只要写入栈不为空,那么`push`操作的锁就不会影响到`pop`。

- [reference](https://leetcode.com/problems/implement-queue-using-stacks/discuss/64284/Do-you-know-when-we-should-use-two-stacks-to-implement-a-queue)

- [further reading](https://stackoverflow.com/questions/2050120/why-use-two-stacks-to-make-a-queue/2050402#2050402)