mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 13:16:28 +08:00
deploy
This commit is contained in:
parent
bb1e36dcdb
commit
9cdda7ae49
3 changed files with 237 additions and 3 deletions
|
@ -2346,7 +2346,135 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">linkedlist_deque.swift</span><pre><span></span><code><a id="__codelineno-18-1" name="__codelineno-18-1" href="#__codelineno-18-1"></a>
|
<div class="highlight"><span class="filename">linkedlist_deque.swift</span><pre><span></span><code><a id="__codelineno-18-1" name="__codelineno-18-1" href="#__codelineno-18-1"></a><span class="cm">/* 双向链表结点 */</span>
|
||||||
|
<a id="__codelineno-18-2" name="__codelineno-18-2" href="#__codelineno-18-2"></a><span class="kd">class</span> <span class="nc">ListNode</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-3" name="__codelineno-18-3" href="#__codelineno-18-3"></a> <span class="kd">var</span> <span class="nv">val</span><span class="p">:</span> <span class="nb">Int</span> <span class="c1">// 结点值</span>
|
||||||
|
<a id="__codelineno-18-4" name="__codelineno-18-4" href="#__codelineno-18-4"></a> <span class="kd">var</span> <span class="nv">next</span><span class="p">:</span> <span class="n">ListNode</span><span class="p">?</span> <span class="c1">// 后继结点引用(指针)</span>
|
||||||
|
<a id="__codelineno-18-5" name="__codelineno-18-5" href="#__codelineno-18-5"></a> <span class="kd">var</span> <span class="nv">prev</span><span class="p">:</span> <span class="n">ListNode</span><span class="p">?</span> <span class="c1">// 前驱结点引用(指针)</span>
|
||||||
|
<a id="__codelineno-18-6" name="__codelineno-18-6" href="#__codelineno-18-6"></a>
|
||||||
|
<a id="__codelineno-18-7" name="__codelineno-18-7" href="#__codelineno-18-7"></a> <span class="kd">init</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="nb">Int</span><span class="p">)</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-8" name="__codelineno-18-8" href="#__codelineno-18-8"></a> <span class="kc">self</span><span class="p">.</span><span class="n">val</span> <span class="p">=</span> <span class="n">val</span>
|
||||||
|
<a id="__codelineno-18-9" name="__codelineno-18-9" href="#__codelineno-18-9"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-10" name="__codelineno-18-10" href="#__codelineno-18-10"></a><span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-11" name="__codelineno-18-11" href="#__codelineno-18-11"></a>
|
||||||
|
<a id="__codelineno-18-12" name="__codelineno-18-12" href="#__codelineno-18-12"></a><span class="cm">/* 基于双向链表实现的双向队列 */</span>
|
||||||
|
<a id="__codelineno-18-13" name="__codelineno-18-13" href="#__codelineno-18-13"></a><span class="kd">class</span> <span class="nc">LinkedListDeque</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-14" name="__codelineno-18-14" href="#__codelineno-18-14"></a> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">front</span><span class="p">:</span> <span class="n">ListNode</span><span class="p">?</span> <span class="c1">// 头结点 front</span>
|
||||||
|
<a id="__codelineno-18-15" name="__codelineno-18-15" href="#__codelineno-18-15"></a> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">rear</span><span class="p">:</span> <span class="n">ListNode</span><span class="p">?</span> <span class="c1">// 尾结点 rear</span>
|
||||||
|
<a id="__codelineno-18-16" name="__codelineno-18-16" href="#__codelineno-18-16"></a> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">queSize</span><span class="p">:</span> <span class="nb">Int</span> <span class="c1">// 双向队列的长度</span>
|
||||||
|
<a id="__codelineno-18-17" name="__codelineno-18-17" href="#__codelineno-18-17"></a>
|
||||||
|
<a id="__codelineno-18-18" name="__codelineno-18-18" href="#__codelineno-18-18"></a> <span class="kd">init</span><span class="p">()</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-19" name="__codelineno-18-19" href="#__codelineno-18-19"></a> <span class="n">queSize</span> <span class="p">=</span> <span class="mi">0</span>
|
||||||
|
<a id="__codelineno-18-20" name="__codelineno-18-20" href="#__codelineno-18-20"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-21" name="__codelineno-18-21" href="#__codelineno-18-21"></a>
|
||||||
|
<a id="__codelineno-18-22" name="__codelineno-18-22" href="#__codelineno-18-22"></a> <span class="cm">/* 获取双向队列的长度 */</span>
|
||||||
|
<a id="__codelineno-18-23" name="__codelineno-18-23" href="#__codelineno-18-23"></a> <span class="kd">func</span> <span class="nf">size</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-24" name="__codelineno-18-24" href="#__codelineno-18-24"></a> <span class="n">queSize</span>
|
||||||
|
<a id="__codelineno-18-25" name="__codelineno-18-25" href="#__codelineno-18-25"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-26" name="__codelineno-18-26" href="#__codelineno-18-26"></a>
|
||||||
|
<a id="__codelineno-18-27" name="__codelineno-18-27" href="#__codelineno-18-27"></a> <span class="cm">/* 判断双向队列是否为空 */</span>
|
||||||
|
<a id="__codelineno-18-28" name="__codelineno-18-28" href="#__codelineno-18-28"></a> <span class="kd">func</span> <span class="nf">isEmpty</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Bool</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-29" name="__codelineno-18-29" href="#__codelineno-18-29"></a> <span class="n">size</span><span class="p">()</span> <span class="p">==</span> <span class="mi">0</span>
|
||||||
|
<a id="__codelineno-18-30" name="__codelineno-18-30" href="#__codelineno-18-30"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-31" name="__codelineno-18-31" href="#__codelineno-18-31"></a>
|
||||||
|
<a id="__codelineno-18-32" name="__codelineno-18-32" href="#__codelineno-18-32"></a> <span class="cm">/* 入队操作 */</span>
|
||||||
|
<a id="__codelineno-18-33" name="__codelineno-18-33" href="#__codelineno-18-33"></a> <span class="kd">private</span> <span class="kd">func</span> <span class="nf">push</span><span class="p">(</span><span class="n">num</span><span class="p">:</span> <span class="nb">Int</span><span class="p">,</span> <span class="n">isFront</span><span class="p">:</span> <span class="nb">Bool</span><span class="p">)</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-34" name="__codelineno-18-34" href="#__codelineno-18-34"></a> <span class="kd">let</span> <span class="nv">node</span> <span class="p">=</span> <span class="n">ListNode</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="n">num</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-18-35" name="__codelineno-18-35" href="#__codelineno-18-35"></a> <span class="c1">// 若链表为空,则令 front, rear 都指向 node</span>
|
||||||
|
<a id="__codelineno-18-36" name="__codelineno-18-36" href="#__codelineno-18-36"></a> <span class="k">if</span> <span class="bp">isEmpty</span><span class="p">()</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-37" name="__codelineno-18-37" href="#__codelineno-18-37"></a> <span class="n">front</span> <span class="p">=</span> <span class="n">node</span>
|
||||||
|
<a id="__codelineno-18-38" name="__codelineno-18-38" href="#__codelineno-18-38"></a> <span class="n">rear</span> <span class="p">=</span> <span class="n">node</span>
|
||||||
|
<a id="__codelineno-18-39" name="__codelineno-18-39" href="#__codelineno-18-39"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-40" name="__codelineno-18-40" href="#__codelineno-18-40"></a> <span class="c1">// 队首入队操作</span>
|
||||||
|
<a id="__codelineno-18-41" name="__codelineno-18-41" href="#__codelineno-18-41"></a> <span class="k">else</span> <span class="k">if</span> <span class="n">isFront</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-42" name="__codelineno-18-42" href="#__codelineno-18-42"></a> <span class="c1">// 将 node 添加至链表头部</span>
|
||||||
|
<a id="__codelineno-18-43" name="__codelineno-18-43" href="#__codelineno-18-43"></a> <span class="n">front</span><span class="p">?.</span><span class="n">prev</span> <span class="p">=</span> <span class="n">node</span>
|
||||||
|
<a id="__codelineno-18-44" name="__codelineno-18-44" href="#__codelineno-18-44"></a> <span class="n">node</span><span class="p">.</span><span class="n">next</span> <span class="p">=</span> <span class="n">front</span>
|
||||||
|
<a id="__codelineno-18-45" name="__codelineno-18-45" href="#__codelineno-18-45"></a> <span class="n">front</span> <span class="p">=</span> <span class="n">node</span> <span class="c1">// 更新头结点</span>
|
||||||
|
<a id="__codelineno-18-46" name="__codelineno-18-46" href="#__codelineno-18-46"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-47" name="__codelineno-18-47" href="#__codelineno-18-47"></a> <span class="c1">// 队尾入队操作</span>
|
||||||
|
<a id="__codelineno-18-48" name="__codelineno-18-48" href="#__codelineno-18-48"></a> <span class="k">else</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-49" name="__codelineno-18-49" href="#__codelineno-18-49"></a> <span class="c1">// 将 node 添加至链表尾部</span>
|
||||||
|
<a id="__codelineno-18-50" name="__codelineno-18-50" href="#__codelineno-18-50"></a> <span class="n">rear</span><span class="p">?.</span><span class="n">next</span> <span class="p">=</span> <span class="n">node</span>
|
||||||
|
<a id="__codelineno-18-51" name="__codelineno-18-51" href="#__codelineno-18-51"></a> <span class="n">node</span><span class="p">.</span><span class="n">prev</span> <span class="p">=</span> <span class="n">rear</span>
|
||||||
|
<a id="__codelineno-18-52" name="__codelineno-18-52" href="#__codelineno-18-52"></a> <span class="n">rear</span> <span class="p">=</span> <span class="n">node</span> <span class="c1">// 更新尾结点</span>
|
||||||
|
<a id="__codelineno-18-53" name="__codelineno-18-53" href="#__codelineno-18-53"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-54" name="__codelineno-18-54" href="#__codelineno-18-54"></a> <span class="n">queSize</span> <span class="o">+=</span> <span class="mi">1</span> <span class="c1">// 更新队列长度</span>
|
||||||
|
<a id="__codelineno-18-55" name="__codelineno-18-55" href="#__codelineno-18-55"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-56" name="__codelineno-18-56" href="#__codelineno-18-56"></a>
|
||||||
|
<a id="__codelineno-18-57" name="__codelineno-18-57" href="#__codelineno-18-57"></a> <span class="cm">/* 队首入队 */</span>
|
||||||
|
<a id="__codelineno-18-58" name="__codelineno-18-58" href="#__codelineno-18-58"></a> <span class="kd">func</span> <span class="nf">pushFirst</span><span class="p">(</span><span class="n">num</span><span class="p">:</span> <span class="nb">Int</span><span class="p">)</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-59" name="__codelineno-18-59" href="#__codelineno-18-59"></a> <span class="n">push</span><span class="p">(</span><span class="n">num</span><span class="p">:</span> <span class="n">num</span><span class="p">,</span> <span class="n">isFront</span><span class="p">:</span> <span class="kc">true</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-18-60" name="__codelineno-18-60" href="#__codelineno-18-60"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-61" name="__codelineno-18-61" href="#__codelineno-18-61"></a>
|
||||||
|
<a id="__codelineno-18-62" name="__codelineno-18-62" href="#__codelineno-18-62"></a> <span class="cm">/* 队尾入队 */</span>
|
||||||
|
<a id="__codelineno-18-63" name="__codelineno-18-63" href="#__codelineno-18-63"></a> <span class="kd">func</span> <span class="nf">pushLast</span><span class="p">(</span><span class="n">num</span><span class="p">:</span> <span class="nb">Int</span><span class="p">)</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-64" name="__codelineno-18-64" href="#__codelineno-18-64"></a> <span class="n">push</span><span class="p">(</span><span class="n">num</span><span class="p">:</span> <span class="n">num</span><span class="p">,</span> <span class="n">isFront</span><span class="p">:</span> <span class="kc">false</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-18-65" name="__codelineno-18-65" href="#__codelineno-18-65"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-66" name="__codelineno-18-66" href="#__codelineno-18-66"></a>
|
||||||
|
<a id="__codelineno-18-67" name="__codelineno-18-67" href="#__codelineno-18-67"></a> <span class="cm">/* 出队操作 */</span>
|
||||||
|
<a id="__codelineno-18-68" name="__codelineno-18-68" href="#__codelineno-18-68"></a> <span class="kd">private</span> <span class="kd">func</span> <span class="nf">poll</span><span class="p">(</span><span class="n">isFront</span><span class="p">:</span> <span class="nb">Bool</span><span class="p">)</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-69" name="__codelineno-18-69" href="#__codelineno-18-69"></a> <span class="k">if</span> <span class="bp">isEmpty</span><span class="p">()</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-70" name="__codelineno-18-70" href="#__codelineno-18-70"></a> <span class="bp">fatalError</span><span class="p">(</span><span class="s">"双向队列为空"</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-18-71" name="__codelineno-18-71" href="#__codelineno-18-71"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-72" name="__codelineno-18-72" href="#__codelineno-18-72"></a> <span class="kd">let</span> <span class="nv">val</span><span class="p">:</span> <span class="nb">Int</span>
|
||||||
|
<a id="__codelineno-18-73" name="__codelineno-18-73" href="#__codelineno-18-73"></a> <span class="c1">// 队首出队操作</span>
|
||||||
|
<a id="__codelineno-18-74" name="__codelineno-18-74" href="#__codelineno-18-74"></a> <span class="k">if</span> <span class="n">isFront</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-75" name="__codelineno-18-75" href="#__codelineno-18-75"></a> <span class="n">val</span> <span class="p">=</span> <span class="n">front</span><span class="p">!.</span><span class="n">val</span> <span class="c1">// 暂存头结点值</span>
|
||||||
|
<a id="__codelineno-18-76" name="__codelineno-18-76" href="#__codelineno-18-76"></a> <span class="c1">// 删除头结点</span>
|
||||||
|
<a id="__codelineno-18-77" name="__codelineno-18-77" href="#__codelineno-18-77"></a> <span class="kd">let</span> <span class="nv">fNext</span> <span class="p">=</span> <span class="n">front</span><span class="p">?.</span><span class="n">next</span>
|
||||||
|
<a id="__codelineno-18-78" name="__codelineno-18-78" href="#__codelineno-18-78"></a> <span class="k">if</span> <span class="n">fNext</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-79" name="__codelineno-18-79" href="#__codelineno-18-79"></a> <span class="n">fNext</span><span class="p">?.</span><span class="n">prev</span> <span class="p">=</span> <span class="kc">nil</span>
|
||||||
|
<a id="__codelineno-18-80" name="__codelineno-18-80" href="#__codelineno-18-80"></a> <span class="n">front</span><span class="p">?.</span><span class="n">next</span> <span class="p">=</span> <span class="kc">nil</span>
|
||||||
|
<a id="__codelineno-18-81" name="__codelineno-18-81" href="#__codelineno-18-81"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-82" name="__codelineno-18-82" href="#__codelineno-18-82"></a> <span class="n">front</span> <span class="p">=</span> <span class="n">fNext</span> <span class="c1">// 更新头结点</span>
|
||||||
|
<a id="__codelineno-18-83" name="__codelineno-18-83" href="#__codelineno-18-83"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-84" name="__codelineno-18-84" href="#__codelineno-18-84"></a> <span class="c1">// 队尾出队操作</span>
|
||||||
|
<a id="__codelineno-18-85" name="__codelineno-18-85" href="#__codelineno-18-85"></a> <span class="k">else</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-86" name="__codelineno-18-86" href="#__codelineno-18-86"></a> <span class="n">val</span> <span class="p">=</span> <span class="n">rear</span><span class="p">!.</span><span class="n">val</span> <span class="c1">// 暂存尾结点值</span>
|
||||||
|
<a id="__codelineno-18-87" name="__codelineno-18-87" href="#__codelineno-18-87"></a> <span class="c1">// 删除尾结点</span>
|
||||||
|
<a id="__codelineno-18-88" name="__codelineno-18-88" href="#__codelineno-18-88"></a> <span class="kd">let</span> <span class="nv">rPrev</span> <span class="p">=</span> <span class="n">rear</span><span class="p">?.</span><span class="n">prev</span>
|
||||||
|
<a id="__codelineno-18-89" name="__codelineno-18-89" href="#__codelineno-18-89"></a> <span class="k">if</span> <span class="n">rPrev</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-90" name="__codelineno-18-90" href="#__codelineno-18-90"></a> <span class="n">rPrev</span><span class="p">?.</span><span class="n">next</span> <span class="p">=</span> <span class="kc">nil</span>
|
||||||
|
<a id="__codelineno-18-91" name="__codelineno-18-91" href="#__codelineno-18-91"></a> <span class="n">rear</span><span class="p">?.</span><span class="n">prev</span> <span class="p">=</span> <span class="kc">nil</span>
|
||||||
|
<a id="__codelineno-18-92" name="__codelineno-18-92" href="#__codelineno-18-92"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-93" name="__codelineno-18-93" href="#__codelineno-18-93"></a> <span class="n">rear</span> <span class="p">=</span> <span class="n">rPrev</span> <span class="c1">// 更新尾结点</span>
|
||||||
|
<a id="__codelineno-18-94" name="__codelineno-18-94" href="#__codelineno-18-94"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-95" name="__codelineno-18-95" href="#__codelineno-18-95"></a> <span class="n">queSize</span> <span class="o">-=</span> <span class="mi">1</span> <span class="c1">// 更新队列长度</span>
|
||||||
|
<a id="__codelineno-18-96" name="__codelineno-18-96" href="#__codelineno-18-96"></a> <span class="k">return</span> <span class="n">val</span>
|
||||||
|
<a id="__codelineno-18-97" name="__codelineno-18-97" href="#__codelineno-18-97"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-98" name="__codelineno-18-98" href="#__codelineno-18-98"></a>
|
||||||
|
<a id="__codelineno-18-99" name="__codelineno-18-99" href="#__codelineno-18-99"></a> <span class="cm">/* 队首出队 */</span>
|
||||||
|
<a id="__codelineno-18-100" name="__codelineno-18-100" href="#__codelineno-18-100"></a> <span class="kd">func</span> <span class="nf">pollFirst</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-101" name="__codelineno-18-101" href="#__codelineno-18-101"></a> <span class="n">poll</span><span class="p">(</span><span class="n">isFront</span><span class="p">:</span> <span class="kc">true</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-18-102" name="__codelineno-18-102" href="#__codelineno-18-102"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-103" name="__codelineno-18-103" href="#__codelineno-18-103"></a>
|
||||||
|
<a id="__codelineno-18-104" name="__codelineno-18-104" href="#__codelineno-18-104"></a> <span class="cm">/* 队尾出队 */</span>
|
||||||
|
<a id="__codelineno-18-105" name="__codelineno-18-105" href="#__codelineno-18-105"></a> <span class="kd">func</span> <span class="nf">pollLast</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-106" name="__codelineno-18-106" href="#__codelineno-18-106"></a> <span class="n">poll</span><span class="p">(</span><span class="n">isFront</span><span class="p">:</span> <span class="kc">false</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-18-107" name="__codelineno-18-107" href="#__codelineno-18-107"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-108" name="__codelineno-18-108" href="#__codelineno-18-108"></a>
|
||||||
|
<a id="__codelineno-18-109" name="__codelineno-18-109" href="#__codelineno-18-109"></a> <span class="cm">/* 访问队首元素 */</span>
|
||||||
|
<a id="__codelineno-18-110" name="__codelineno-18-110" href="#__codelineno-18-110"></a> <span class="kd">func</span> <span class="nf">peekFirst</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span><span class="p">?</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-111" name="__codelineno-18-111" href="#__codelineno-18-111"></a> <span class="bp">isEmpty</span><span class="p">()</span> <span class="p">?</span> <span class="kc">nil</span> <span class="p">:</span> <span class="n">front</span><span class="p">?.</span><span class="n">val</span>
|
||||||
|
<a id="__codelineno-18-112" name="__codelineno-18-112" href="#__codelineno-18-112"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-113" name="__codelineno-18-113" href="#__codelineno-18-113"></a>
|
||||||
|
<a id="__codelineno-18-114" name="__codelineno-18-114" href="#__codelineno-18-114"></a> <span class="cm">/* 访问队尾元素 */</span>
|
||||||
|
<a id="__codelineno-18-115" name="__codelineno-18-115" href="#__codelineno-18-115"></a> <span class="kd">func</span> <span class="nf">peekLast</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span><span class="p">?</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-116" name="__codelineno-18-116" href="#__codelineno-18-116"></a> <span class="bp">isEmpty</span><span class="p">()</span> <span class="p">?</span> <span class="kc">nil</span> <span class="p">:</span> <span class="n">rear</span><span class="p">?.</span><span class="n">val</span>
|
||||||
|
<a id="__codelineno-18-117" name="__codelineno-18-117" href="#__codelineno-18-117"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-118" name="__codelineno-18-118" href="#__codelineno-18-118"></a>
|
||||||
|
<a id="__codelineno-18-119" name="__codelineno-18-119" href="#__codelineno-18-119"></a> <span class="cm">/* 返回数组用于打印 */</span>
|
||||||
|
<a id="__codelineno-18-120" name="__codelineno-18-120" href="#__codelineno-18-120"></a> <span class="kd">func</span> <span class="nf">toArray</span><span class="p">()</span> <span class="p">-></span> <span class="p">[</span><span class="nb">Int</span><span class="p">]</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-121" name="__codelineno-18-121" href="#__codelineno-18-121"></a> <span class="kd">var</span> <span class="nv">node</span> <span class="p">=</span> <span class="n">front</span>
|
||||||
|
<a id="__codelineno-18-122" name="__codelineno-18-122" href="#__codelineno-18-122"></a> <span class="kd">var</span> <span class="nv">res</span> <span class="p">=</span> <span class="nb">Array</span><span class="p">(</span><span class="n">repeating</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="bp">count</span><span class="p">:</span> <span class="n">size</span><span class="p">())</span>
|
||||||
|
<a id="__codelineno-18-123" name="__codelineno-18-123" href="#__codelineno-18-123"></a> <span class="k">for</span> <span class="n">i</span> <span class="k">in</span> <span class="n">res</span><span class="p">.</span><span class="bp">indices</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-18-124" name="__codelineno-18-124" href="#__codelineno-18-124"></a> <span class="n">res</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="p">=</span> <span class="n">node</span><span class="p">!.</span><span class="n">val</span>
|
||||||
|
<a id="__codelineno-18-125" name="__codelineno-18-125" href="#__codelineno-18-125"></a> <span class="n">node</span> <span class="p">=</span> <span class="n">node</span><span class="p">?.</span><span class="n">next</span>
|
||||||
|
<a id="__codelineno-18-126" name="__codelineno-18-126" href="#__codelineno-18-126"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-127" name="__codelineno-18-127" href="#__codelineno-18-127"></a> <span class="k">return</span> <span class="n">res</span>
|
||||||
|
<a id="__codelineno-18-128" name="__codelineno-18-128" href="#__codelineno-18-128"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-18-129" name="__codelineno-18-129" href="#__codelineno-18-129"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -2515,7 +2643,113 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">array_deque.swift</span><pre><span></span><code><a id="__codelineno-28-1" name="__codelineno-28-1" href="#__codelineno-28-1"></a>
|
<div class="highlight"><span class="filename">array_deque.swift</span><pre><span></span><code><a id="__codelineno-28-1" name="__codelineno-28-1" href="#__codelineno-28-1"></a><span class="cm">/* 基于环形数组实现的双向队列 */</span>
|
||||||
|
<a id="__codelineno-28-2" name="__codelineno-28-2" href="#__codelineno-28-2"></a><span class="kd">class</span> <span class="nc">ArrayDeque</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-3" name="__codelineno-28-3" href="#__codelineno-28-3"></a> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">nums</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">]</span> <span class="c1">// 用于存储双向队列元素的数组</span>
|
||||||
|
<a id="__codelineno-28-4" name="__codelineno-28-4" href="#__codelineno-28-4"></a> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">front</span><span class="p">:</span> <span class="nb">Int</span> <span class="c1">// 队首指针,指向队首元素</span>
|
||||||
|
<a id="__codelineno-28-5" name="__codelineno-28-5" href="#__codelineno-28-5"></a> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">queSize</span><span class="p">:</span> <span class="nb">Int</span> <span class="c1">// 双向队列长度</span>
|
||||||
|
<a id="__codelineno-28-6" name="__codelineno-28-6" href="#__codelineno-28-6"></a>
|
||||||
|
<a id="__codelineno-28-7" name="__codelineno-28-7" href="#__codelineno-28-7"></a> <span class="cm">/* 构造方法 */</span>
|
||||||
|
<a id="__codelineno-28-8" name="__codelineno-28-8" href="#__codelineno-28-8"></a> <span class="kd">init</span><span class="p">(</span><span class="n">capacity</span><span class="p">:</span> <span class="nb">Int</span><span class="p">)</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-9" name="__codelineno-28-9" href="#__codelineno-28-9"></a> <span class="n">nums</span> <span class="p">=</span> <span class="nb">Array</span><span class="p">(</span><span class="n">repeating</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="bp">count</span><span class="p">:</span> <span class="n">capacity</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-28-10" name="__codelineno-28-10" href="#__codelineno-28-10"></a> <span class="n">front</span> <span class="p">=</span> <span class="mi">0</span>
|
||||||
|
<a id="__codelineno-28-11" name="__codelineno-28-11" href="#__codelineno-28-11"></a> <span class="n">queSize</span> <span class="p">=</span> <span class="mi">0</span>
|
||||||
|
<a id="__codelineno-28-12" name="__codelineno-28-12" href="#__codelineno-28-12"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-13" name="__codelineno-28-13" href="#__codelineno-28-13"></a>
|
||||||
|
<a id="__codelineno-28-14" name="__codelineno-28-14" href="#__codelineno-28-14"></a> <span class="cm">/* 获取双向队列的容量 */</span>
|
||||||
|
<a id="__codelineno-28-15" name="__codelineno-28-15" href="#__codelineno-28-15"></a> <span class="kd">func</span> <span class="nf">capacity</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-16" name="__codelineno-28-16" href="#__codelineno-28-16"></a> <span class="n">nums</span><span class="p">.</span><span class="bp">count</span>
|
||||||
|
<a id="__codelineno-28-17" name="__codelineno-28-17" href="#__codelineno-28-17"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-18" name="__codelineno-28-18" href="#__codelineno-28-18"></a>
|
||||||
|
<a id="__codelineno-28-19" name="__codelineno-28-19" href="#__codelineno-28-19"></a> <span class="cm">/* 获取双向队列的长度 */</span>
|
||||||
|
<a id="__codelineno-28-20" name="__codelineno-28-20" href="#__codelineno-28-20"></a> <span class="kd">func</span> <span class="nf">size</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-21" name="__codelineno-28-21" href="#__codelineno-28-21"></a> <span class="n">queSize</span>
|
||||||
|
<a id="__codelineno-28-22" name="__codelineno-28-22" href="#__codelineno-28-22"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-23" name="__codelineno-28-23" href="#__codelineno-28-23"></a>
|
||||||
|
<a id="__codelineno-28-24" name="__codelineno-28-24" href="#__codelineno-28-24"></a> <span class="cm">/* 判断双向队列是否为空 */</span>
|
||||||
|
<a id="__codelineno-28-25" name="__codelineno-28-25" href="#__codelineno-28-25"></a> <span class="kd">func</span> <span class="nf">isEmpty</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Bool</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-26" name="__codelineno-28-26" href="#__codelineno-28-26"></a> <span class="n">size</span><span class="p">()</span> <span class="p">==</span> <span class="mi">0</span>
|
||||||
|
<a id="__codelineno-28-27" name="__codelineno-28-27" href="#__codelineno-28-27"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-28" name="__codelineno-28-28" href="#__codelineno-28-28"></a>
|
||||||
|
<a id="__codelineno-28-29" name="__codelineno-28-29" href="#__codelineno-28-29"></a> <span class="cm">/* 计算环形数组索引 */</span>
|
||||||
|
<a id="__codelineno-28-30" name="__codelineno-28-30" href="#__codelineno-28-30"></a> <span class="kd">private</span> <span class="kd">func</span> <span class="nf">index</span><span class="p">(</span><span class="n">i</span><span class="p">:</span> <span class="nb">Int</span><span class="p">)</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-31" name="__codelineno-28-31" href="#__codelineno-28-31"></a> <span class="c1">// 通过取余操作实现数组首尾相连</span>
|
||||||
|
<a id="__codelineno-28-32" name="__codelineno-28-32" href="#__codelineno-28-32"></a> <span class="c1">// 当 i 越过数组尾部后,回到头部</span>
|
||||||
|
<a id="__codelineno-28-33" name="__codelineno-28-33" href="#__codelineno-28-33"></a> <span class="c1">// 当 i 越过数组头部后,回到尾部</span>
|
||||||
|
<a id="__codelineno-28-34" name="__codelineno-28-34" href="#__codelineno-28-34"></a> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="n">capacity</span><span class="p">())</span> <span class="o">%</span> <span class="n">capacity</span><span class="p">()</span>
|
||||||
|
<a id="__codelineno-28-35" name="__codelineno-28-35" href="#__codelineno-28-35"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-36" name="__codelineno-28-36" href="#__codelineno-28-36"></a>
|
||||||
|
<a id="__codelineno-28-37" name="__codelineno-28-37" href="#__codelineno-28-37"></a> <span class="cm">/* 队首入队 */</span>
|
||||||
|
<a id="__codelineno-28-38" name="__codelineno-28-38" href="#__codelineno-28-38"></a> <span class="kd">func</span> <span class="nf">pushFirst</span><span class="p">(</span><span class="n">num</span><span class="p">:</span> <span class="nb">Int</span><span class="p">)</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-39" name="__codelineno-28-39" href="#__codelineno-28-39"></a> <span class="k">if</span> <span class="n">size</span><span class="p">()</span> <span class="p">==</span> <span class="n">capacity</span><span class="p">()</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-40" name="__codelineno-28-40" href="#__codelineno-28-40"></a> <span class="bp">print</span><span class="p">(</span><span class="s">"双向队列已满"</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-28-41" name="__codelineno-28-41" href="#__codelineno-28-41"></a> <span class="k">return</span>
|
||||||
|
<a id="__codelineno-28-42" name="__codelineno-28-42" href="#__codelineno-28-42"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-43" name="__codelineno-28-43" href="#__codelineno-28-43"></a> <span class="c1">// 队首指针向左移动一位</span>
|
||||||
|
<a id="__codelineno-28-44" name="__codelineno-28-44" href="#__codelineno-28-44"></a> <span class="c1">// 通过取余操作,实现 front 越过数组头部后回到尾部</span>
|
||||||
|
<a id="__codelineno-28-45" name="__codelineno-28-45" href="#__codelineno-28-45"></a> <span class="n">front</span> <span class="p">=</span> <span class="n">index</span><span class="p">(</span><span class="n">i</span><span class="p">:</span> <span class="n">front</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-28-46" name="__codelineno-28-46" href="#__codelineno-28-46"></a> <span class="c1">// 将 num 添加至队首</span>
|
||||||
|
<a id="__codelineno-28-47" name="__codelineno-28-47" href="#__codelineno-28-47"></a> <span class="n">nums</span><span class="p">[</span><span class="n">front</span><span class="p">]</span> <span class="p">=</span> <span class="n">num</span>
|
||||||
|
<a id="__codelineno-28-48" name="__codelineno-28-48" href="#__codelineno-28-48"></a> <span class="n">queSize</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||||
|
<a id="__codelineno-28-49" name="__codelineno-28-49" href="#__codelineno-28-49"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-50" name="__codelineno-28-50" href="#__codelineno-28-50"></a>
|
||||||
|
<a id="__codelineno-28-51" name="__codelineno-28-51" href="#__codelineno-28-51"></a> <span class="cm">/* 队尾入队 */</span>
|
||||||
|
<a id="__codelineno-28-52" name="__codelineno-28-52" href="#__codelineno-28-52"></a> <span class="kd">func</span> <span class="nf">pushLast</span><span class="p">(</span><span class="n">num</span><span class="p">:</span> <span class="nb">Int</span><span class="p">)</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-53" name="__codelineno-28-53" href="#__codelineno-28-53"></a> <span class="k">if</span> <span class="n">size</span><span class="p">()</span> <span class="p">==</span> <span class="n">capacity</span><span class="p">()</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-54" name="__codelineno-28-54" href="#__codelineno-28-54"></a> <span class="bp">print</span><span class="p">(</span><span class="s">"双向队列已满"</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-28-55" name="__codelineno-28-55" href="#__codelineno-28-55"></a> <span class="k">return</span>
|
||||||
|
<a id="__codelineno-28-56" name="__codelineno-28-56" href="#__codelineno-28-56"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-57" name="__codelineno-28-57" href="#__codelineno-28-57"></a> <span class="c1">// 计算尾指针,指向队尾索引 + 1</span>
|
||||||
|
<a id="__codelineno-28-58" name="__codelineno-28-58" href="#__codelineno-28-58"></a> <span class="kd">let</span> <span class="nv">rear</span> <span class="p">=</span> <span class="n">index</span><span class="p">(</span><span class="n">i</span><span class="p">:</span> <span class="n">front</span> <span class="o">+</span> <span class="n">size</span><span class="p">())</span>
|
||||||
|
<a id="__codelineno-28-59" name="__codelineno-28-59" href="#__codelineno-28-59"></a> <span class="c1">// 将 num 添加至队尾</span>
|
||||||
|
<a id="__codelineno-28-60" name="__codelineno-28-60" href="#__codelineno-28-60"></a> <span class="n">nums</span><span class="p">[</span><span class="n">rear</span><span class="p">]</span> <span class="p">=</span> <span class="n">num</span>
|
||||||
|
<a id="__codelineno-28-61" name="__codelineno-28-61" href="#__codelineno-28-61"></a> <span class="n">queSize</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||||
|
<a id="__codelineno-28-62" name="__codelineno-28-62" href="#__codelineno-28-62"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-63" name="__codelineno-28-63" href="#__codelineno-28-63"></a>
|
||||||
|
<a id="__codelineno-28-64" name="__codelineno-28-64" href="#__codelineno-28-64"></a> <span class="cm">/* 队首出队 */</span>
|
||||||
|
<a id="__codelineno-28-65" name="__codelineno-28-65" href="#__codelineno-28-65"></a> <span class="kd">func</span> <span class="nf">pollFirst</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-66" name="__codelineno-28-66" href="#__codelineno-28-66"></a> <span class="kd">let</span> <span class="nv">num</span> <span class="p">=</span> <span class="n">peekFirst</span><span class="p">()</span>
|
||||||
|
<a id="__codelineno-28-67" name="__codelineno-28-67" href="#__codelineno-28-67"></a> <span class="c1">// 队首指针向后移动一位</span>
|
||||||
|
<a id="__codelineno-28-68" name="__codelineno-28-68" href="#__codelineno-28-68"></a> <span class="n">front</span> <span class="p">=</span> <span class="n">index</span><span class="p">(</span><span class="n">i</span><span class="p">:</span> <span class="n">front</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-28-69" name="__codelineno-28-69" href="#__codelineno-28-69"></a> <span class="n">queSize</span> <span class="o">-=</span> <span class="mi">1</span>
|
||||||
|
<a id="__codelineno-28-70" name="__codelineno-28-70" href="#__codelineno-28-70"></a> <span class="k">return</span> <span class="n">num</span>
|
||||||
|
<a id="__codelineno-28-71" name="__codelineno-28-71" href="#__codelineno-28-71"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-72" name="__codelineno-28-72" href="#__codelineno-28-72"></a>
|
||||||
|
<a id="__codelineno-28-73" name="__codelineno-28-73" href="#__codelineno-28-73"></a> <span class="cm">/* 队尾出队 */</span>
|
||||||
|
<a id="__codelineno-28-74" name="__codelineno-28-74" href="#__codelineno-28-74"></a> <span class="kd">func</span> <span class="nf">pollLast</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-75" name="__codelineno-28-75" href="#__codelineno-28-75"></a> <span class="kd">let</span> <span class="nv">num</span> <span class="p">=</span> <span class="n">peekLast</span><span class="p">()</span>
|
||||||
|
<a id="__codelineno-28-76" name="__codelineno-28-76" href="#__codelineno-28-76"></a> <span class="n">queSize</span> <span class="o">-=</span> <span class="mi">1</span>
|
||||||
|
<a id="__codelineno-28-77" name="__codelineno-28-77" href="#__codelineno-28-77"></a> <span class="k">return</span> <span class="n">num</span>
|
||||||
|
<a id="__codelineno-28-78" name="__codelineno-28-78" href="#__codelineno-28-78"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-79" name="__codelineno-28-79" href="#__codelineno-28-79"></a>
|
||||||
|
<a id="__codelineno-28-80" name="__codelineno-28-80" href="#__codelineno-28-80"></a> <span class="cm">/* 访问队首元素 */</span>
|
||||||
|
<a id="__codelineno-28-81" name="__codelineno-28-81" href="#__codelineno-28-81"></a> <span class="kd">func</span> <span class="nf">peekFirst</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-82" name="__codelineno-28-82" href="#__codelineno-28-82"></a> <span class="k">if</span> <span class="bp">isEmpty</span><span class="p">()</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-83" name="__codelineno-28-83" href="#__codelineno-28-83"></a> <span class="bp">fatalError</span><span class="p">(</span><span class="s">"双向队列为空"</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-28-84" name="__codelineno-28-84" href="#__codelineno-28-84"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-85" name="__codelineno-28-85" href="#__codelineno-28-85"></a> <span class="k">return</span> <span class="n">nums</span><span class="p">[</span><span class="n">front</span><span class="p">]</span>
|
||||||
|
<a id="__codelineno-28-86" name="__codelineno-28-86" href="#__codelineno-28-86"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-87" name="__codelineno-28-87" href="#__codelineno-28-87"></a>
|
||||||
|
<a id="__codelineno-28-88" name="__codelineno-28-88" href="#__codelineno-28-88"></a> <span class="cm">/* 访问队尾元素 */</span>
|
||||||
|
<a id="__codelineno-28-89" name="__codelineno-28-89" href="#__codelineno-28-89"></a> <span class="kd">func</span> <span class="nf">peekLast</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-90" name="__codelineno-28-90" href="#__codelineno-28-90"></a> <span class="k">if</span> <span class="bp">isEmpty</span><span class="p">()</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-91" name="__codelineno-28-91" href="#__codelineno-28-91"></a> <span class="bp">fatalError</span><span class="p">(</span><span class="s">"双向队列为空"</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-28-92" name="__codelineno-28-92" href="#__codelineno-28-92"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-93" name="__codelineno-28-93" href="#__codelineno-28-93"></a> <span class="c1">// 计算尾元素索引</span>
|
||||||
|
<a id="__codelineno-28-94" name="__codelineno-28-94" href="#__codelineno-28-94"></a> <span class="kd">let</span> <span class="nv">last</span> <span class="p">=</span> <span class="n">index</span><span class="p">(</span><span class="n">i</span><span class="p">:</span> <span class="n">front</span> <span class="o">+</span> <span class="n">size</span><span class="p">()</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-28-95" name="__codelineno-28-95" href="#__codelineno-28-95"></a> <span class="k">return</span> <span class="n">nums</span><span class="p">[</span><span class="bp">last</span><span class="p">]</span>
|
||||||
|
<a id="__codelineno-28-96" name="__codelineno-28-96" href="#__codelineno-28-96"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-97" name="__codelineno-28-97" href="#__codelineno-28-97"></a>
|
||||||
|
<a id="__codelineno-28-98" name="__codelineno-28-98" href="#__codelineno-28-98"></a> <span class="cm">/* 返回数组用于打印 */</span>
|
||||||
|
<a id="__codelineno-28-99" name="__codelineno-28-99" href="#__codelineno-28-99"></a> <span class="kd">func</span> <span class="nf">toArray</span><span class="p">()</span> <span class="p">-></span> <span class="p">[</span><span class="nb">Int</span><span class="p">]</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-100" name="__codelineno-28-100" href="#__codelineno-28-100"></a> <span class="c1">// 仅转换有效长度范围内的列表元素</span>
|
||||||
|
<a id="__codelineno-28-101" name="__codelineno-28-101" href="#__codelineno-28-101"></a> <span class="kd">var</span> <span class="nv">res</span> <span class="p">=</span> <span class="nb">Array</span><span class="p">(</span><span class="n">repeating</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="bp">count</span><span class="p">:</span> <span class="n">size</span><span class="p">())</span>
|
||||||
|
<a id="__codelineno-28-102" name="__codelineno-28-102" href="#__codelineno-28-102"></a> <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">)</span> <span class="k">in</span> <span class="n">sequence</span><span class="p">(</span><span class="bp">first</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">front</span><span class="p">),</span> <span class="n">next</span><span class="p">:</span> <span class="p">{</span> <span class="nv">$0</span> <span class="o"><</span> <span class="kc">self</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="o">-</span> <span class="mi">1</span> <span class="p">?</span> <span class="p">(</span><span class="nv">$0</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nv">$1</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="p">:</span> <span class="kc">nil</span> <span class="p">})</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-28-103" name="__codelineno-28-103" href="#__codelineno-28-103"></a> <span class="n">res</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="p">=</span> <span class="n">nums</span><span class="p">[</span><span class="n">index</span><span class="p">(</span><span class="n">i</span><span class="p">:</span> <span class="n">j</span><span class="p">)]</span>
|
||||||
|
<a id="__codelineno-28-104" name="__codelineno-28-104" href="#__codelineno-28-104"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-105" name="__codelineno-28-105" href="#__codelineno-28-105"></a> <span class="k">return</span> <span class="n">res</span>
|
||||||
|
<a id="__codelineno-28-106" name="__codelineno-28-106" href="#__codelineno-28-106"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-28-107" name="__codelineno-28-107" href="#__codelineno-28-107"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
|
File diff suppressed because one or more lines are too long
BIN
sitemap.xml.gz
BIN
sitemap.xml.gz
Binary file not shown.
Loading…
Reference in a new issue