Skip to content

Commit 048a81f

Browse files
committed
Deploying to gh-pages from @ 04a491b 🚀
1 parent 2113995 commit 048a81f

File tree

6 files changed

+220
-6
lines changed

6 files changed

+220
-6
lines changed

day3/lc203.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,10 @@ <h2 id="解题思路"><a class="header" href="#解题思路">解题思路</a></h
195195
impl Solution {
196196
pub fn remove_elements(head: Option&lt;Box&lt;ListNode&gt;&gt;, val: i32) -&gt; Option&lt;Box&lt;ListNode&gt;&gt; {
197197
let mut head = Some(Box::new(ListNode { val: 0, next: head }));
198-
let mut a = &amp;mut head;
198+
let mut a: &amp;mut Option&lt;Box&lt;ListNode&gt;&gt; = &amp;mut head;
199199
while a.as_deref_mut().unwrap().next.is_some() {
200+
// ^ Option&lt;&amp;mut ListNode&gt;
201+
//
200202
let v = a.as_deref_mut().unwrap().next.as_deref().unwrap().val;
201203
if v == val {
202204
let mut b = a.as_deref_mut().unwrap().next.take();
@@ -211,7 +213,7 @@ <h2 id="解题思路"><a class="header" href="#解题思路">解题思路</a></h
211213
}
212214
}
213215
<span class="boring">}</span></code></pre></pre>
214-
<p>属实有点恶心了</p>
216+
<p>属实有点恶心了,看着太复杂了,这就是不用take的后果</p>
215217
<h2 id="学习感想"><a class="header" href="#学习感想">学习感想</a></h2>
216218
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
217219
</span>
@@ -296,6 +298,8 @@ <h2 id="学习感想"><a class="header" href="#学习感想">学习感想</a></h
296298
}
297299
}
298300
<span class="boring">}</span></code></pre></pre>
301+
<p>链表的ownership还是非常容易理清楚的</p>
302+
<p>一个Option不是owner没法直接unwrap,但是as_mut了之后可以随意unwrap,这也是容器穿透</p>
299303

300304
</main>
301305

day3/lc206.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,41 @@ <h2 id="解题思路"><a class="header" href="#解题思路">解题思路</a></h
210210
<span class="boring">}</span></code></pre></pre>
211211
<h2 id="学习感想"><a class="header" href="#学习感想">学习感想</a></h2>
212212
<p>所以我这个算是什么方式</p>
213+
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
214+
</span>
215+
216+
<span class="boring">fn main() {
217+
</span>struct Solution {}
218+
219+
// Definition for singly-linked list.
220+
#[derive(PartialEq, Eq, Clone, Debug)]
221+
pub struct ListNode {
222+
pub val: i32,
223+
pub next: Option&lt;Box&lt;ListNode&gt;&gt;
224+
}
225+
226+
impl ListNode {
227+
#[inline]
228+
fn new(val: i32) -&gt; Self {
229+
ListNode {
230+
next: None,
231+
val
232+
}
233+
}
234+
}
235+
236+
impl Solution {
237+
pub fn reverse_list(mut head: Option&lt;Box&lt;ListNode&gt;&gt;) -&gt; Option&lt;Box&lt;ListNode&gt;&gt; {
238+
let mut res: Option&lt;Box&lt;ListNode&gt;&gt; = None;
239+
while let Some(mut x) = head {
240+
head = x.next.take();
241+
x.next = res;
242+
res = Some(x);
243+
}
244+
res
245+
}
246+
}
247+
<span class="boring">}</span></code></pre></pre>
213248

214249
</main>
215250

day3/lc707.html

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,74 @@ <h2 id="解题思路"><a class="header" href="#解题思路">解题思路</a></h
274274
<span class="boring">}</span></code></pre></pre>
275275
<h2 id="学习感想"><a class="header" href="#学习感想">学习感想</a></h2>
276276
<p>也没啥好说的,rust只要写出来,基本是对的,没有用take的形式,而是全部去除了option用ref</p>
277+
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
278+
</span><span class="boring">fn main() {
279+
</span>struct MyLinkedList {
280+
head: Option&lt;Box&lt;ListNode&gt;&gt;,
281+
cnt: i32,
282+
}
283+
284+
struct ListNode {
285+
val: i32,
286+
next: Option&lt;Box&lt;ListNode&gt;&gt;,
287+
}
288+
289+
/**
290+
* `&amp;self` means the method takes an immutable reference.
291+
* If you need a mutable reference, change it to `&amp;mut self` instead.
292+
*/
293+
impl MyLinkedList {
294+
295+
fn new() -&gt; Self {
296+
Self {
297+
head: None,
298+
cnt: 0i32,
299+
}
300+
}
301+
302+
fn get(&amp;self, mut index: i32) -&gt; i32 {
303+
if index &gt;= self.cnt { return -1i32 }
304+
let mut ptr: &amp; Box&lt;ListNode&gt; = self.head.as_ref().unwrap();
305+
while index &gt; 0i32 {
306+
ptr = ptr.next.as_ref().unwrap();
307+
index -= 1i32;
308+
}
309+
ptr.val
310+
}
311+
312+
fn add_at_head(&amp;mut self, val: i32) {
313+
self.add_at_index(0i32, val);
314+
}
315+
316+
fn add_at_tail(&amp;mut self, val: i32) {
317+
self.add_at_index(self.cnt, val);
318+
}
319+
320+
fn add_at_index(&amp;mut self, mut index: i32, val: i32) {
321+
if index &gt; self.cnt { return }
322+
let mut ptr: &amp;mut Option&lt;Box&lt;ListNode&gt;&gt; = &amp;mut self.head;
323+
while index &gt; 0i32 {
324+
ptr = &amp;mut ptr.as_mut().unwrap().next;
325+
index -= 1i32;
326+
}
327+
self.cnt += 1i32;
328+
*ptr = Some(Box::new(ListNode { val: val, next: ptr.take() }))
329+
}
330+
331+
fn delete_at_index(&amp;mut self, mut index: i32) {
332+
if index &gt;= self.cnt { return }
333+
let mut ptr: &amp;mut Option&lt;Box&lt;ListNode&gt;&gt; = &amp;mut self.head;
334+
while index &gt; 0i32 {
335+
ptr = &amp;mut ptr.as_mut().unwrap().next;
336+
index -= 1i32;
337+
}
338+
let nxt: Option&lt;Box&lt;ListNode&gt;&gt; = ptr.take().unwrap().next.take();
339+
*ptr = nxt;
340+
self.cnt -= 1i32;
341+
}
342+
}
343+
344+
<span class="boring">}</span></code></pre></pre>
277345

278346
</main>
279347

print.html

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,10 @@ <h2 id="解题思路-5"><a class="header" href="#解题思路-5">解题思路</a
808808
impl Solution {
809809
pub fn remove_elements(head: Option&lt;Box&lt;ListNode&gt;&gt;, val: i32) -&gt; Option&lt;Box&lt;ListNode&gt;&gt; {
810810
let mut head = Some(Box::new(ListNode { val: 0, next: head }));
811-
let mut a = &amp;mut head;
811+
let mut a: &amp;mut Option&lt;Box&lt;ListNode&gt;&gt; = &amp;mut head;
812812
while a.as_deref_mut().unwrap().next.is_some() {
813+
// ^ Option&lt;&amp;mut ListNode&gt;
814+
//
813815
let v = a.as_deref_mut().unwrap().next.as_deref().unwrap().val;
814816
if v == val {
815817
let mut b = a.as_deref_mut().unwrap().next.take();
@@ -824,7 +826,7 @@ <h2 id="解题思路-5"><a class="header" href="#解题思路-5">解题思路</a
824826
}
825827
}
826828
<span class="boring">}</span></code></pre></pre>
827-
<p>属实有点恶心了</p>
829+
<p>属实有点恶心了,看着太复杂了,这就是不用take的后果</p>
828830
<h2 id="学习感想-5"><a class="header" href="#学习感想-5">学习感想</a></h2>
829831
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
830832
</span>
@@ -909,6 +911,8 @@ <h2 id="学习感想-5"><a class="header" href="#学习感想-5">学习感想</a
909911
}
910912
}
911913
<span class="boring">}</span></code></pre></pre>
914+
<p>链表的ownership还是非常容易理清楚的</p>
915+
<p>一个Option不是owner没法直接unwrap,但是as_mut了之后可以随意unwrap,这也是容器穿透</p>
912916
<div style="break-before: page; page-break-before: always;"></div><h1 id="707-设计链表"><a class="header" href="#707-设计链表">707. 设计链表</a></h1>
913917
<h2 id="题目描述-6"><a class="header" href="#题目描述-6">题目描述</a></h2>
914918
<p>你可以选择使用单链表或者双链表,设计并实现自己的链表。</p>
@@ -1017,6 +1021,74 @@ <h2 id="解题思路-6"><a class="header" href="#解题思路-6">解题思路</a
10171021
<span class="boring">}</span></code></pre></pre>
10181022
<h2 id="学习感想-6"><a class="header" href="#学习感想-6">学习感想</a></h2>
10191023
<p>也没啥好说的,rust只要写出来,基本是对的,没有用take的形式,而是全部去除了option用ref</p>
1024+
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
1025+
</span><span class="boring">fn main() {
1026+
</span>struct MyLinkedList {
1027+
head: Option&lt;Box&lt;ListNode&gt;&gt;,
1028+
cnt: i32,
1029+
}
1030+
1031+
struct ListNode {
1032+
val: i32,
1033+
next: Option&lt;Box&lt;ListNode&gt;&gt;,
1034+
}
1035+
1036+
/**
1037+
* `&amp;self` means the method takes an immutable reference.
1038+
* If you need a mutable reference, change it to `&amp;mut self` instead.
1039+
*/
1040+
impl MyLinkedList {
1041+
1042+
fn new() -&gt; Self {
1043+
Self {
1044+
head: None,
1045+
cnt: 0i32,
1046+
}
1047+
}
1048+
1049+
fn get(&amp;self, mut index: i32) -&gt; i32 {
1050+
if index &gt;= self.cnt { return -1i32 }
1051+
let mut ptr: &amp; Box&lt;ListNode&gt; = self.head.as_ref().unwrap();
1052+
while index &gt; 0i32 {
1053+
ptr = ptr.next.as_ref().unwrap();
1054+
index -= 1i32;
1055+
}
1056+
ptr.val
1057+
}
1058+
1059+
fn add_at_head(&amp;mut self, val: i32) {
1060+
self.add_at_index(0i32, val);
1061+
}
1062+
1063+
fn add_at_tail(&amp;mut self, val: i32) {
1064+
self.add_at_index(self.cnt, val);
1065+
}
1066+
1067+
fn add_at_index(&amp;mut self, mut index: i32, val: i32) {
1068+
if index &gt; self.cnt { return }
1069+
let mut ptr: &amp;mut Option&lt;Box&lt;ListNode&gt;&gt; = &amp;mut self.head;
1070+
while index &gt; 0i32 {
1071+
ptr = &amp;mut ptr.as_mut().unwrap().next;
1072+
index -= 1i32;
1073+
}
1074+
self.cnt += 1i32;
1075+
*ptr = Some(Box::new(ListNode { val: val, next: ptr.take() }))
1076+
}
1077+
1078+
fn delete_at_index(&amp;mut self, mut index: i32) {
1079+
if index &gt;= self.cnt { return }
1080+
let mut ptr: &amp;mut Option&lt;Box&lt;ListNode&gt;&gt; = &amp;mut self.head;
1081+
while index &gt; 0i32 {
1082+
ptr = &amp;mut ptr.as_mut().unwrap().next;
1083+
index -= 1i32;
1084+
}
1085+
let nxt: Option&lt;Box&lt;ListNode&gt;&gt; = ptr.take().unwrap().next.take();
1086+
*ptr = nxt;
1087+
self.cnt -= 1i32;
1088+
}
1089+
}
1090+
1091+
<span class="boring">}</span></code></pre></pre>
10201092
<div style="break-before: page; page-break-before: always;"></div><h1 id="206-反转链表"><a class="header" href="#206-反转链表">206. 反转链表</a></h1>
10211093
<h2 id="题目描述-7"><a class="header" href="#题目描述-7">题目描述</a></h2>
10221094
<p>给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。</p>
@@ -1061,6 +1133,41 @@ <h2 id="解题思路-7"><a class="header" href="#解题思路-7">解题思路</a
10611133
<span class="boring">}</span></code></pre></pre>
10621134
<h2 id="学习感想-7"><a class="header" href="#学习感想-7">学习感想</a></h2>
10631135
<p>所以我这个算是什么方式</p>
1136+
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
1137+
</span>
1138+
1139+
<span class="boring">fn main() {
1140+
</span>struct Solution {}
1141+
1142+
// Definition for singly-linked list.
1143+
#[derive(PartialEq, Eq, Clone, Debug)]
1144+
pub struct ListNode {
1145+
pub val: i32,
1146+
pub next: Option&lt;Box&lt;ListNode&gt;&gt;
1147+
}
1148+
1149+
impl ListNode {
1150+
#[inline]
1151+
fn new(val: i32) -&gt; Self {
1152+
ListNode {
1153+
next: None,
1154+
val
1155+
}
1156+
}
1157+
}
1158+
1159+
impl Solution {
1160+
pub fn reverse_list(mut head: Option&lt;Box&lt;ListNode&gt;&gt;) -&gt; Option&lt;Box&lt;ListNode&gt;&gt; {
1161+
let mut res: Option&lt;Box&lt;ListNode&gt;&gt; = None;
1162+
while let Some(mut x) = head {
1163+
head = x.next.take();
1164+
x.next = res;
1165+
res = Some(x);
1166+
}
1167+
res
1168+
}
1169+
}
1170+
<span class="boring">}</span></code></pre></pre>
10641171
<div style="break-before: page; page-break-before: always;"></div><h1 id="第二章-链表part02"><a class="header" href="#第二章-链表part02">第二章 链表part02</a></h1>
10651172
<p>● day 1 任务以及具体安排:https://docs.qq.com/doc/DUG9UR2ZUc3BjRUdY
10661173
● day 2 任务以及具体安排:https://docs.qq.com/doc/DUGRwWXNOVEpyaVpG

searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

searchindex.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)