Leetcode 理解题目意思与专有名词-Leetcode 解题心得
Version: 2023101201
作者: Billour Ou 欧育溙
这是我最近进行真人Leetcode 一对一面试考试心得,写成文章分享给有需要的人。
我解理到的重点:英文语意跟中文语意顺序是完全相反的。 如果你是刚开始刷Leetcode 造成你身心上很大压力的朋友,看不懂leetcode题目是正常的。
我最近发现,在进行Leetcode 考试时会遇到以下三个问题:
举例题目:
965. Univalued Binary Tree
A binary tree is uni-valued if every node in the tree has the same value.
Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.
Example 1:
Input: root = [1,1,1,1,1,null,1]
Output: true
Example 2:
Input: root = [2,2,2,5,2]
Output: false
Constraints:
The number of nodes in the tree is in the range [1, 100].
0 <= Node.val < 100
第一阶段:
理解 Leetcode 题目的意思。动词很重要,因为接下来我们要用动词进行英文句子的拆分并且理解其意思。Univalued Binary Tree
A binary tree is uni-valued if every node in the tree has the same value.
Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.
因为中文的语意理解跟英文不同,所以,我用分析的方式让读者能更能理解题目。
A binary tree is uni-valued if every node in the tree has the same value.
主词 : A binary tree
动词: is
说明: uni-valued if every node in the tree has the same value.
说明细节: 中文意思(在树的节点node 有相同数值vale 就是 uni-valued) if every node in the tree has the same value.
在读上述这一段文字时,要先看「说明」然后读英文句子时要从后面倒着看,才会理解成中文的语意。
所以,读上述英文就会变成先看if every node in the tree has the same value.
Tree has the same value. 当有相同的数值vale
if every node in the tree 在二元树内的节点 node
has 动词
---转换成中文----
在二元树内的节点 node,任二个节点 node,当有相同的数值vale,就是 uni-valued binary tree。
所以,英文语意跟中文语意顺序是完全相反的。
Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.
主词 : Given the root of a binary tree, 逗点, 的前面都是主词
动词: return
说明: if the given tree is uni-valued, or false otherwise.
说明细节: 如果这个二元树是 uni-valued 就回传 true ,如果不是 uni-valued就回传 false
true if the given tree is uni-valued 如果这个二元树是 uni-valued 就回传 true ,
or false otherwise. 如果不是 uni-valued就回传 false
备注: 我曾经把 Leetcode 的题目拿给七位英文老师看,七位英文老师全部都看不懂题目的意思,最后,我把题目拿给数学系的朋友看,数学系的朋友马上可以理解题目的意思。所以,如果你是刚开始刷Leetcode 造成你身心上很大压力的朋友,看不懂题目是正常的,看完这段留言或许会好过一点。
第二阶段:
2. 资料结构的专有名词。
专有名词很重要,因为如果在考试当下忘记专有名词的定义,基本上这题就无法解答出来,所以,在考Leetcode 面试时,要先把所有资料结构的基本定义都复习一次。
以965题这个题目为例子专有名词有以下:
Binary Tree :每个节点最多只有两个分支(即不存在分支度大于2的节点)的树结构[1]。通常分支被称作「左子树」或「右子树」。二元树的分支具有左右次序,不能随意颠倒。
Uni-valued: A binary tree is uni-valued if every node in the tree has the same value.
第三阶段:
3. 找到相对应的资料结构,且,写出程式语言。
以这一965题就是使用二元树的前序遍历 (preorder): 中 -> 左 -> 右 ,且使用深度优先(DFS)。
前序遍历preorder:
vals.append(node.val)
dfs(node.left)
dfs(node.right)
以下是python 解答:
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def isUnivalTree(self, root: Optional[TreeNode]) -> bool: vals = [] def dfs(node): if node : vals.append(node.val) dfs(node.left) dfs(node.right) dfs(root) return len(set(vals)) ==1
有时候,解题真的要有解题的题感,因为在1对1的程式考试时,人都会紧张,面试者也会有很大的心理压力,所以,3. 找到相对应的资料结构,且,写出程式语言。跟当时你的心理状态有很大的关係,如果平时有把所有的资料结构都记下来,但在考试当天太紧张的话,也会有差错的时候。所以,就算没有要换工作,平时就多参加 Leetcode 的面试,增加自已的考试临场考试的反应能力。
2023/10/12 目前我刷题的题目数量为411题数,1,669 (一年的提交次数) submissions in the last year。
平均一题Leetcode 我会用 C language, C++ , Python 三种语言都写一次。所以我是一年的提交次数会高达1,669次。