iT邦幫忙

0

Leetcode 理解題目意思與專有名詞-Leetcode 解題心得

  • 分享至 

  • xImage
  •  

Leetcode 理解題目意思與專有名詞-Leetcode 解題心得

Version: 2023101201
作者: Billour Ou 歐育溙

這是我最近進行真人Leetcode 一對一面試考試心得,寫成文章分享給有需要的人。
我解理到的重點:英文語意跟中文語意順序是完全相反的。 如果你是剛開始刷Leetcode 造成你身心上很大壓力的朋友,看不懂leetcode題目是正常的。
我最近發現,在進行Leetcode 考試時會遇到以下三個問題:

  1. 理解 Leetcode 題目的意思。
  2. 資料結構的專有名詞。
  3. 找到相對應的資料結構,且,寫出程式語言。

舉例題目:
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:

https://ithelp.ithome.com.tw/upload/images/20231012/20126723X26BLATOIF.png

Input: root = [1,1,1,1,1,null,1]
Output: true

Example 2:

https://ithelp.ithome.com.tw/upload/images/20231012/20126723BYQo5j5vza.png

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

第一階段:

  1. 理解 Leetcode 題目的意思。
    動詞很重要,因為接下來我們要用動詞進行英文句子的拆分並且理解其意思。
  2. 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 = right

class 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

https://ithelp.ithome.com.tw/upload/images/20231012/20126723cUWs6ZFxUu.jpg

有時候,解題真的要有解題的題感,因為在1對1的程式考試時,人都會緊張,面試者也會有很大的心理壓力,所以,3. 找到相對應的資料結構,且,寫出程式語言。跟當時你的心理狀態有很大的關係,如果平時有把所有的資料結構都記下來,但在考試當天太緊張的話,也會有差錯的時候。所以,就算沒有要換工作,平時就多參加 Leetcode 的面試,增加自已的考試臨場考試的反應能力。

2023/10/12 目前我刷題的題目數量為411題數,1,669 (一年的提交次數) submissions in the last year。
平均一題Leetcode 我會用 C language, C++ , Python 三種語言都寫一次。所以我是一年的提交次數會高達1,669次。


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言