貢獻者: Huaxin
Remove Element 模擬面試錄影(英)
Flatten Binary Tree to Linked List 模擬面試錄影(漢)
Middle of the Linked List 模擬面試錄影(漢)
🧔:interviewer 👶:interviewee
🧔: We have a problem. Given an integer array nums and an integer val. Please attempt to remove all occurences of val in nums and return the number of elements in nums which are not equal to val. The number which are not equal to val is k. Additionally, you need to consider that the first elemtents in nums are not equal to val.Don't concern the remaining elements.
👶: I will receive an integer array and an integer val. Then return the count of k elements that are not equal to to val in the array. Also, the first k elements in the array are not equal to val, right?
🧔:That's right. note that all yout operations will be performed on the integer array you get.
👶:
Okay, let me give you an example, assuming our input is below
input: [5,1,5,3], val 為 5
output: [1,3,] , k 為 2
The two last elements in the integer array are not considered, and the order of my 1 and 3 can be rearranged. Is my understanding correct?
🧔:
Yes, it doesn't affect the final accuracy.
👶:
Here , I will use an integer to keep track of the current position in the array where the value is not val. This integer is used for inserting new elements that are not val and also for returning the final count of elements that are not val. Next, I will use a for loop to iterate through all elements in the integer array one by one. In this way, the time complecity is O(n), and the space complecity is O(1).
🧔:OKay, Start now.
👶:
we declare an integer k ,and then use a for loop to iterate through each element. If the current element is not equal to val. we insert it starting from the first element of nums, and increment k to prepare for the next inserion position.
After the traversal, return the next available position for inserting elements that are not equal to "val."
👶:
Suppose we have an array [1,5,1,6] and val is 1. At i=0, we check if nums[0] is not equal to 1. If it's not, we skip it. The current nums is [1,5,1,6].
At i=1, we check if nums[1] is not equal to 1. If it is,we insert nums[1] at position k=0, and then increment k. The current nums is [5,5,1,6]
At i=2, the nums is [5,5,1,6]
At i=3, the nums is [5,6,1,6]
Finally, we return k with a value of 2.
🧔:interviewer 👶:interviewee
🧔:給你單獨一個 linked list 的 head,請你返回該 linked list 的 middle node。
假設碰到兩個 middle node 麻煩請返回第二個 middle node。
👶:我會拿到一個 linked list 的 head ,然後嘗試去找出 linked list 的 middle node 對嗎?
🧔:是的沒錯,注意若存在兩個 middle node 記得返回第二個。
👶:例如有一個 linked list 為 5->8->9->10,此時中間存在兩個 middle ,最終我必須返回 node 9。
🧔:對。
👶:我想用兩個 pointer,分別名為 middle 和 final。以下為我的想法
一開始讓 middle = head,final = head
使用 while loop 並每次判斷 final 和 final->next 是否都存在
最後返回 middle pointer
關於時間複雜度為 O(N),因為遍尋時間為線性,也就是 linked list 的長度。
空間複雜度為 O(1),因為只用了兩個指標,為 constant。
🧔:好
👶:
👶:這裡我舉兩個不同長度 linked list 做說明,分別為 2->3->4,2->3->4->5
第一次 middle 走到 node 3,final 走到 node 4
第二次 判斷 while(final && final->next)
時,因 final->next
不成立跳出 while Loop, 返回 middle node
第一次 middle 走到 node 3,final 走到 node 4
第二次 判斷 while(final && final->next)
時成立,因為 final->next
存在,final->next
指向 null,因此 middle 走到 node 4,final 走到 null
第三次 跳出 while loop, 返回 middle node
🧔:如果嘗試把 while loop 的判斷式改成 while(final)
會發生什麼事
👶:我舉個例子好了,假設我們 linked list 為 2->5->7->9->10
第一次 middle 走到 node 5,final 走到 node 7
第二次 middle 走到 node 7,final 走到 node 10
第三次 middle 走到 node 9,final pointer 走第一步時並不會出錯,但在走第二步時便會碰到 access violation 的問題。
🧔:好
🧔:給你一個二元樹的 root,請你試著去攤平成一個 linked list,該 linked list 應該使用相同的 TreeNode class,其中右子樹會指向 list 的下一個節點並且左子樹為 null。
👶:我會得到一個二元樹的 root,然後我要將該 tree 攤平成一個 linked list,其中右子樹會指向 list 的下一個節點並且左子樹為 null,對嗎?
🧔:是的。
👶:假設我的 input 如下
然後最後 output 如下
🧔:是的
👶:
🧔:那該方法的時間複雜度和時間複雜度是?
👶:該方法的時間複雜度取決於節點數量,所以為 O(n),空間複雜度為O(1)
👶:
🧔:我看完後發現,你有個地方會重複記憶體分配和釋放,你的想法是 ?
👶:我觀察了我的程式碼並且比對您剛剛提到的想法,在 while loop 中會重複為 p 分配記憶體,造成額外的計算成本,可以將 p 在 while loop 前提前宣告,來改善此問題。
🧔:是的,因為當你 root 的 left 要處理的次數增加時,你的運算成本便會增加
👶:這裡我們以下方例子做示範
step 1
嘗試將 root 的左子樹的最右節點,node 4,指向 root 的右子節點,node 5
step 2
再將 root 的 right 指向 node 2,則 root 的 left 指向 null,然後 root 往右子節點移動
step 3
以下重複上面所述動作
step 4
step 5
針對interviewer的檢討:
針對interviewee的檢討:
優點
REACTO 做得不錯
在講解的時候表達滿清楚的
可以改進的地方
interviewer和interviewee之間的切換可以適當的留白