From 1774f5e8926020a6efe5ab35f159a3a6dd0d4638 Mon Sep 17 00:00:00 2001 From: "yaoyuan2.chu" Date: Tue, 4 May 2021 00:04:31 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90feat=E3=80=91:=E5=89=91=E6=8C=87offer?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E6=B7=BB=E5=8A=A0=EF=BC=8Cmd=E4=B8=ADjsx?= =?UTF-8?q?=E8=AF=AD=E6=B3=95=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/LeetCode/1 - 100/_category_.json | 2 +- docs/LeetCode/100 - 200/_category_.json | 2 +- .../{ => CodingInterviews}/StackAndQueue/.DS_Store | Bin .../StackAndQueue/MaxValueOfQueue.md | 8 ++++---- docs/LeetCode/CodingInterviews/_category_.json | 4 ++++ docs/LeetCode/_category_.json | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) rename docs/LeetCode/{ => CodingInterviews}/StackAndQueue/.DS_Store (100%) rename docs/LeetCode/{ => CodingInterviews}/StackAndQueue/MaxValueOfQueue.md (89%) mode change 100755 => 100644 create mode 100644 docs/LeetCode/CodingInterviews/_category_.json diff --git a/docs/LeetCode/1 - 100/_category_.json b/docs/LeetCode/1 - 100/_category_.json index 063c1af..7d660de 100644 --- a/docs/LeetCode/1 - 100/_category_.json +++ b/docs/LeetCode/1 - 100/_category_.json @@ -1,4 +1,4 @@ { "label": "1 - 100", - "position": 2 + "position": 3 } diff --git a/docs/LeetCode/100 - 200/_category_.json b/docs/LeetCode/100 - 200/_category_.json index 0e3c226..6b996cf 100644 --- a/docs/LeetCode/100 - 200/_category_.json +++ b/docs/LeetCode/100 - 200/_category_.json @@ -1,4 +1,4 @@ { "label": "100 - 200", - "position": 3 + "position": 4 } diff --git a/docs/LeetCode/StackAndQueue/.DS_Store b/docs/LeetCode/CodingInterviews/StackAndQueue/.DS_Store similarity index 100% rename from docs/LeetCode/StackAndQueue/.DS_Store rename to docs/LeetCode/CodingInterviews/StackAndQueue/.DS_Store diff --git a/docs/LeetCode/StackAndQueue/MaxValueOfQueue.md b/docs/LeetCode/CodingInterviews/StackAndQueue/MaxValueOfQueue.md old mode 100755 new mode 100644 similarity index 89% rename from docs/LeetCode/StackAndQueue/MaxValueOfQueue.md rename to docs/LeetCode/CodingInterviews/StackAndQueue/MaxValueOfQueue.md index 8e1d761..ad89551 --- a/docs/LeetCode/StackAndQueue/MaxValueOfQueue.md +++ b/docs/LeetCode/CodingInterviews/StackAndQueue/MaxValueOfQueue.md @@ -64,7 +64,7 @@ class MaxQueue { 本着先实现,后优化的原则,最容易想到的解决方案是这样的: -用一个队列保存所有的数据,这样保证了push和pop操作是正常执行的。然后将这个队列从大到小排列到另一个数据结构中,这样执行max_value方法时输出数组第一项即可。删除队列中数据的时候,用来排序的数据结构中的对应数据也应该删掉。 +用一个队列保存所有的数据,这样保证了push和pop操作是正常执行的。然后将这个队列从大到小排列到另一个数据结构中,这样执行max_value方法时输出数组第一项即可。删除队列中数据的时候,用来排序的数据结构中的对应数据也应该删掉。 于是乎很容易写出这样的代码: @@ -112,7 +112,7 @@ class MaxQueue { } ~~~ -接下来考虑着三个TODO项如何去解决。首先要确定的是用什么数据结构保存sortedDatas。 +接下来考虑着三个TODO项如何去解决。首先要确定的是用什么数据结构保存sortedDatas。 | 数据结构 | 优劣分析 | 结论 | | -------- | ------------------------------------------------------------ | ---------- | @@ -122,13 +122,13 @@ class MaxQueue { | 队列 | 单调递减的队列。遇到的问题和栈一致。 | | | 树 | 最大二叉树能实现,但是树在内存中也是以数组的形式保存的,问题和数组一致。 | :no_entry: | -所以说,要么用栈,要么用数组来保存sortedDatas中的数据。所以决定采用双端队列来处理sortedDatas,因为**双端队列能同时满足栈、队列的所有操作**。下一个问题便是……如何给这个双端队列进行排序。 +所以说,要么用栈,要么用数组来保存sortedDatas中的数据。所以决定采用双端队列来处理sortedDatas,因为**双端队列能同时满足栈、队列的所有操作**。下一个问题便是……如何给这个双端队列进行排序。 堆排序、快速排序的时间复杂度是O(n lg n);其他的线性排序算法时间复杂度是O(n)。这些都不太满足题目的要求。所以这里我想到了一种可能性:**有些数是不是没有参与排序**?如果队列中有n个数字,参与排序的数字小于n,那么排序的时间复杂度会更小的。那么,**什么样的数字可能不用参与排序呢**?max\_value方法要输出的是最大的数,所以最大的数字是一定参与排序的,即比较小的数字可以不用参与排序——我只管最大的数,其他的数字顺序对与否甚至数据的有无,都不会影响到最后的结果。 所以进一步的解决方案是这样的: -每次有新的数据插入的时候,将sortedDatas中的数据从后往前跟入参比较,如果小于入参,就直接舍弃掉,直到遇到比入参大的数字时再将入参存入队列中。 +每次有新的数据插入的时候,将sortedDatas中的数据从后往前跟入参比较,如果小于入参,就直接舍弃掉,直到遇到比入参大的数字时再将入参存入队列中。 这样的好处显而易见:小于入参的数字不会影响到最大值的输出。另一方面,队列中的数据来得比现在这个入参早,所以小于入参的数字会比这个入参更早的被删除掉。即,小于入参的数字永远不可能作为最大值背输出出来。 diff --git a/docs/LeetCode/CodingInterviews/_category_.json b/docs/LeetCode/CodingInterviews/_category_.json new file mode 100644 index 0000000..c35d1bd --- /dev/null +++ b/docs/LeetCode/CodingInterviews/_category_.json @@ -0,0 +1,4 @@ +{ + "label": "剑指Offer", + "position": 2 +} diff --git a/docs/LeetCode/_category_.json b/docs/LeetCode/_category_.json index 26c3473..b3c750c 100644 --- a/docs/LeetCode/_category_.json +++ b/docs/LeetCode/_category_.json @@ -1,4 +1,4 @@ { "label": "LeetCode刷题", - "position": 3 + "position": 2 }