2024/12/1 学习日记

avatar 1100879
大迫瓜
791
0
学习日记

今天的学习围绕两个战场展开:CIT 593考试准备和LeetCode练兵,还额外深入了C语言的动态内存管理。
在战斗中总结方法,于难题中练兵,是今天的主旋律。

CIT 593考试准备与动态内存学习
除了考试备战,我还重点学习了C语言的动态内存管理,特别是堆内存(heap memory)与栈内存(stack memory)的使用与区别:

1.堆内存:
; 使用 malloc() 和 free() 手动管理内存。
; 灵活但需要小心避免内存泄漏。

2.栈内存:
; 自动分配和释放,局部变量存储于此。
; 空间有限,适合短期存储需求。

教授的考试建议:
教授特别强调了两点常见错误:

1.问答题答得太长:问答题只需一两句直击核心,写长文会浪费时间。
2.不该花太多时间在加分题:加分题耗时长且得分少,应该把主要精力放在必答题上。

复习策略:

; 先快速浏览讲义,确保关键概念清晰。
; 复习作业特别是做错的题目,确保在考试中避免重复错误。
; 最后通过模拟考试检验实力,营造真实考试环境,训练时间分配与解题速度。

学习动态内存的同时,我还深入理解了C语言中如何管理指针与数组,特别是在高效利用内存时避免陷入未初始化或双重释放的陷阱。

LeetCode Practice

1. Doubly Linked List with HashMap: A Perfect Duo
This question focused on implementing an LRU Cache using a combination of HashMap and doubly linked list.

; The HashMap acts as the forward attacker with O(1) lookups.
; The doubly linked list handles order maintenance, ensuring efficient updates for the most and least recently used items.

The challenge was initializing the doubly linked list. Adding a head and tail dummy node simplified operations. This allowed quick additions at the head and deletions from the tail. With modular helper functions like addToHead() and removeNode(), the implementation became clean and efficient.

2. Regex Matching: Dynamic Programming in Action

The second problem was a hard-level regex matching task involving * and . operators:

; *: Matches zero or more occurrences of the preceding character.
; .: Matches any single character.

I solved it with a 2D dynamic programming table, where:

; dp[i][j] determined if the first i characters of the string matched the first j characters of the pattern.

; Key strategies:
1. * as zero matches: Ignore the preceding character entirely (dp[i][j] = dp[i][j-2]).
2. * as one or more matches: Extend the match backward (dp[i][j] = dp[i-1][j]).

Though edge cases like empty strings or consecutive * were tricky, a methodical approach to defining base cases and transitions led to success.

3. Word Ladder: Turning Strings into a Graph

This problem asked for the shortest path between two words, where a word could transform into another if they differed by exactly one letter. For example:

; APPLE and APPLI are connected (1 letter difference).
; APPLE and APPPP are not connected (2 letters difference).

Key insights:

1.Graph Representation: I used wildcard patterns (e.g., A*PLE, AP*LE) to group similar words in a HashMap, allowing O(1) neighbor lookups.
2.Breadth-First Search (BFS): BFS was perfect for finding the shortest path, exploring neighbors layer by layer. The first match to the target word guarantees the shortest path.

Once the graph was clear, the problem transitioned from daunting to straightforward.

总结

今天的学习是对细节的深入打磨与核心策略的总结。

; 在CIT 593中,复习动态内存的管理让我更加理解堆与栈的差异与应用场景。考试复习则让我明确了抓重点、避陷阱的策略。
; 在LeetCode中,三道题目让我体会到数据结构与算法的深层妙用:从链表与HashMap的组合,到动态规划的逻辑清晰,再到图论中的路径计算。

今天,是胜利的一天。
0条回复