貢獻者: 靈魂程式手-Soul Coder
模擬面試錄影: 漢語
模擬面試錄影: 英語interviewer🤖:
interviewee🐯:
🤖: 好,我是今天的面試官麻煩你打開我剛剛發給你的文本
🐯: 好,我已經打開了。
🤖: 這邊呢,會有一些問題需要你去回答。然後,我們的問題呢是這邊有兩個linked list會當做輸入 l1、l2, 那會回傳一個新的linked list。那這個新的linked list呢,它左邊的位元是比較低,右邊的位元是比較高,所以相加的時候要注意像中間這個它加起來等於10的時候,它進位是進位到下一個ListNode裡面去所以它這邊會變8,3+7+1 = 8 。那這裡有一些constructor你可以使用,接下來就麻煩你完成這個method
🐯: 那我們要做的呢是把兩個linked list給相加後回傳結果,那假設我的l1 = 9 9 9 那l2 = 1 那我的回傳是0 0 0 1。想請問我這樣理解有錯嗎。
🤖: 嘿,你這樣理解是對的。
🐯: 那接下來我們就開始邊實作邊講我們的想法,那我們這樣會遇到長度不同的問題,那我們可以分成三個部分。就是l1跟l2有長度的時候,像l1 跟 l2有相同的長度是1嘛,那我們就可以用一個回圈去處理,那後面這個l1 剩餘的我們就可以用另一個回圈去處理,相反l2比較長的話我們也需要多一個,所以我們總共需要三個while回圈。這三個回圈分辨代表l1,就是檢查l1 & l2是不是nullptr除了這個意外我們還需要檢查l1再來還會有一個l2,那我們就具備這三個回圈去處理上面的這三個問題。再來呢我們需要建置一個輸出的linked list一開始先給他設成一個無效的0那這設成null的話也可以。但是設成null的話一開始就要去判斷他是不是null如果是null才給他建置一個null如果不是則繼續往next接,那我們不那麼做,因為那樣做會在回圈裡面消耗一定的性能。好,那我們這邊就直接改成now,now就指向那個head。那這邊now的作用就是在回圈裡面指向最後一個node節點。那,我們就開始實作這個l1跟l2有相同長度的時候,那..我們這個sum他就會等於l1的value加上l2的value,那這邊呢需要考慮另一個問題。像他會導致一個進位所以我們需要有另一個變數去存這個進位,這邊叫它"addDelay",那這個"addDelay"我們可以把它宣告在回圈的外部,這邊先設成0,那…相加完這個"addDelay"此時就會變成1(口誤手打是0)嘛已經加總了,此時我們還需要再檢查它還有沒有進位到下一個位,就是下一個相加的位置上。如果他大於等於10的話那他就會再進位到下一個位置上,所以我們這邊需要判斷這個那"addDelay"的話他就會變回1。sum的話我們就把他減一個10,因為進位了嘛我們這邊是十進制所以是-10,那這邊不用除的原因是因為它最大只會是兩個9 + 1最大只會到18(口誤實際19)。用出發會導致性能開銷更大,但是沒有這個必要。好…那我們就可以把結果加到我們的輸出的最末端上。好那加上去了之後我們就要把我們最末端的…我們就要把我們當前的這個now的node移到就是他的指針要移到最後一個節點上。所以它就會等於它自己的下一個,那當然l1 跟 l2 因為已經做完加法所以他們也要移到下一個位元上。好…那這一部分就把長度相同的給做完。那長度不相同的跟長度相同的差不多它們會有一些區別。區別就區別在這個l1(嘴巴打結)只有l1然後這個rrr,然後這個l2是沒有的因為他已經到了nullptr(重複了兩次第二次更正讀音)。那已經到nullptr之後就沒有這個東西了。所以再接下來這個l2也一樣就留下這個l2,這個l1也不需要因為它也已經到nullptr。好,那…我們這邊就已經把加法做完了。那我們就已經到最後一個位元這邊,那還有一個沒有處理的就是這一個,最後一位是進位的話我們還沒有處理,所以我們去給他做一個判斷。如果這邊"addDelay"不是0的話,就代表說他最後在這邊還是需要進一位的。那我們這邊加一個判斷就可以處理掉它了。那處理掉它之後我們就要開始准備回傳,回傳的話我們回傳head,但是這邊head我們之前說過它前面有一個無用的ListNode所以我們這邊需要把這個ListNode給拔掉,那首先我們需要把free掉那個空間防止內存洩露,所以now先指向head,那head它就指向它的下一個真正有用的節點上。就是真正的開始位置再來把這個now給刪掉,那這題的解法就這樣完成了,謝謝。
🤖: 好,謝謝你的回答。
🤖: 那,這邊還有一題需要你來完成。那就是會給你一個n整數n。然後你需要准備一個0到n+1的array去完成一下動作。那這個動作呢其實是幫你計算這個數組裡面的值,那你要回傳的不是數組,而是數組裡面最大的一個值。那這邊就要麻煩你完成這個method。
Return the maximum integer in the array nums.
🐯: 好,那…我們要回傳這個最大值,那假設我算完之後我的array裡面是{1,4,5,3,1}(口誤手打是{2,4,5,3,1}),那我的回傳值是不是應該是5。想請問這樣對嗎?
🤖: 嘿,對就是那樣。
🐯: 好,那… 我把他註解掉。好,那…抱歉。(修復排版中…)
🐯: 那接下來我們就直接先建一個array。然後是n+1,那我們就還需要考慮到他等於0或者1的時候會導致的問題所以我們這邊如果它小於2,那我們就直接回傳它的值就可以了,那如果他大於2的話我們的dp[0]
就要設成0。然後dp[1]
就要設成1。那我們除了這個以外我們還需要一個變數叫做"maxValue",那這個"maxValue"我們一開始可以把它設為最小值(INT_MAX)。那接下來我們就可以開始我們的回圈然後去把每一個結果都算出來。i < n, i<=n,然後i++(胡言亂語中)。那這邊他有兩個轉移方程,那第一個的話就是他的條件當i等於偶數的時候,那另一個的話就是i等於非偶數的時候,那i等於偶數的時候我們可以得知它是等於~就是dp[i]
會等於dp整數的i除2。好,那…當他是奇數的時候我們可以看到他的dp[i]
會等於兩個數字相加一個是dp i除2另一個是dp i除2加1。那它就會形成這個樣子,那我們可以知道可以計算出來之後那也要就是把這個"maxValue"也更新。因為我們要的不是整個數組回傳,我們要的只是它的最大值而已。那maxValue = max(maxValue, dp[i][j])
那就可以確保說我們拿到的一定是最大的數值,那假設說它小於2沒進入回圈,那它其實在上面就會回傳回去了。所以我們也不需要考慮這個問題。那這個問題就這樣算是解出來了。最後只需要把它回傳,回傳"maxValue",那這題就這樣解出來了。那…謝謝。
🤖:那謝謝你,我們今天的面試就到這邊。
🤖: Hi, I'm the interviewer today. Here is the problem you need to solve it. This problem is the verify parentheses string is legal or not. So, you need to considere this three kind of parentheses, you no need to considere another parentheses. So, you need to completely this function. Just go through please.
🐯: Ok, now we need to verify a parentheses string is legal or not. For example, like the… s is equal to branket parentheses and we will return false. For like this, we also need return false. Althought is close and open parentheses is same but the order is not legal and another one is only the open parentheses. But it doesn't close, so it also return false. The legal one is only for the order correct and the parentheses open and close is same. So, it will be return true. Is this correct for my understanding?
🤖: Yes, it's correct.
🐯: Ok, I will go through to complete the method. Ok~~~ , this… method can complete by two approach one is soft coding another one is hard coding. But I will go through with soft coding. If using the hard coding, we will use the if else or switch(在亂念). So, I will just go through with map using soft coding. Here we I use unorder_map because using map the BigO is logn and unorder_map the BigO is 1. So for the performance I will go through with unordered_map. And then the key and value is(are) characters. Besides that, we also need a… stack, we need a stack store the store the parentheses we check or we convert and then we should initialize the map…(typing…). Ok, we set up the map. Here we can use auto, ops sorry character is better, i iterator s. And then we can check the i is in mp or not. So I try find if it's not equal~ mp.end() it's mean that we got it in mp. So when it's in mp, we try to convert and push it to the stack, (repeating script one more time…), else here is mean that it's not the open parentheses it's the close parentheses here. (開始胡說八道了~)So, we will try to check the stack, the top of the stack is equal the i or not. If it's not equal then i will just return false. Like this one the stack of the top is the close parentheses of this and then we touch another parentheses but it's not same with the top of stack so it will just return false. So here, it here until here it's mean that is matched so we just pop it. Okay, oh here need another one condition because probably the stack is empty and then it~~ the i will be the, the i be the cross parentheses so we need to check the stack is empty or not it's stack, if the stack is empty then it just return false it means that the stack is empty but it touches a close parentheses. Okay here we gonna return true before we return true we need to check the stacks empty or not if it's not empty then it's return false because when the parentheses is open at the last and then it doesn't close the parentheses and then when we return true then the program will be false. So for the correctly output so here need to check again. Ok that's my solution. Thank you very much.