貢獻者:月退 - Leg
:cat::interviewer
:dog::interviewee
:cat::Hi, Leg! It's great to see you here. I have a few questions for you. We can discuss solutions together. If you have any questions later on, feel free to interrupt me and ask.
:dog::Great, I understand.
:cat::In our daily lives, we often encounter the challenge of setting goals, and we hope that the goals we find meet our expectations. This is similar to when we search for employees, and when you search for a job, we both confirm whether or not it meets our expectations.
:dog::Exactly, we are both going through a process of confirmation.
:cat::Based on this premise, I hope you can answer the following question: Assuming I have aList
with many non-repeating numbers, I want to find two numbers whose sum equals the inputtarget
number. Additionally, return the positions of these two numbers.
:dog::(Repeat)So, our input will consist of a sequence of numbers inList
format and a numbertarget
. We hope to find two numbers from theList
that, when added together, equaltarget
. Finally, we need to return the positions of these two numbers within theList
. Is that correct?
:cat::Yes, your understanding is correct.
:dog::(Example)So, if the inputList
is [4,1,3] andtarget=4
, then the expected output should be [1,2] instead of [0]. This is because [0] contains only one number. Is that right?
:cat::Yes, your assumption is correct.
:dog::(Approach)I would like to start by using adictionary
inpython
to record the position of each number, where thekey
is the number in theList
and thevalue
is its position. Next, I'll iterate through the numbers in theList
and calculate the difference betweentarget
and the number in theList
. Then, I'll check if the difference is in thedictionary
. If not, I will continue searching. If it is, I will return the current position and the position of the subtracted number.
:cat::It sounds quite reasonable. So, please go ahead and code the idea you just mentioned.
:dog::(Code)
首先,我need a dict whose name is nums2id,and the length of nums,so for … the key of nums2id is the number in nums,the value of nums2id is the position of this key number
i want to calculate the difference between target and each element in nums
for …
we need to ckeck that the position of difference and i are different
:dog::We can use the example I just provided to verify.
nums=[4,1,3]
,target=4
, sonums2id[4]=0
,nums2id[1]=1
,nums2id[3]=2
. Then we traverse throughnums
, wheni=0
,complement=0
. Since 0 is not innums2id
, we proceed toi=1
. At this point,complement=3
, and 3 is innums2id
. Moreover, the positions of 1 and 3 are different, so we return[1,2]
, representing the positions of numbers 1 and 3, respectively.
:cat::Alright, then I'd like to ask you to analyze the time complexity and space complexity of this function.
:dog::Because we only need to iterate throughnums
once, the time complexity is O(n). Additionally, since there is no extra memory allocation, the space complexity is O(1).
:cat::Then there's no further questions. Let's conclude here for today.Thank you for coming by.
:dog::Thank you, and I hope to see you again.
:cat::你好月退,今天是由我來問你一些問題,如果對我的描述有疑問都可以直接打斷我,沒關係。
:dog::好的,我瞭解了。
:cat::那你在來面試前應該知道我們公司是做語言相關的對吧?那在語言中有個特別的概念叫做迴文,也就是你正著唸或是倒過來唸,結果都是一樣的,如果我們今天想要知道這個句子或是符不符合迴文的現象,你會想要怎麼做呢?
:dog::(Repeat) 好的,那我想再做一次確認,看我的理解有沒有錯誤。也就是說,現在我要檢驗一個句子,它從第一個字唸到最後一個字,和從最後一個字唸回第一個字,是否相同這樣對嗎?
:cat::你的理解沒錯。
:dog::(Example)所以如果以英文來舉例的話就是像是,level或是deed,如果我以數字來舉例的話,就會像是1221或是212這樣對嗎?
:cat::是的,你的舉例都是合理的。你可以用數字的方式來解釋你的想法或是解法。
:dog::那我想確認一下,輸入數字包含小於零的數字嗎?
:cat::包含小於零的數字。
:dog::(Approach)好的,那第一個我想到的是,可以使用數學的計算。首先,如果數字小於零,那他一定不會是迴文,所以直接return False
就可以了,如果大於等於零的話,就不斷地將數字除以10並取餘數的方法來取得輸入數字的個位數字、十位數字、百位數字…,取得個位數字時將它存起來,取得十位數字的時候,將個位數字乘以10加上十位數字並存起來,以此類推…,就可以得到反過來的數字了,最後只要驗證得到的數字和輸入數字是否相同就可以了。
:cat::聽起來蠻合理的,但有點複雜,你可以把你剛剛說的方法實作出來嗎?
:dog::(Code)好的沒問題
:dog::那這邊講幾個例子,假如
x
設為121,那就先將x
紀錄為old_x
,一開始tmp
設為0,因為121大於0所以進入迴圈,接著可以利用%
這個工具取得除以10的餘數,也就是個位數的1,所以此時tmp
是1,接著將121除以10並取整數商得到12,再次進入迴圈,重複剛剛的動作,可以得到tmp
是12,這是由剛剛10*tmp
等於10加上12%10
等2得到的,最後x
更新為1,一樣進入迴圈,重複剛剛的動作就可以得到tmp=121
,最後就會回傳True
。
:cat::看起來沒有什麼問題,那我想問你,你覺得你的這份code可能會有什麼淺在的問題呢?
:dog::嗯,我覺得可能會相對比較複雜一點,因為過程中都是數學計算,對於其他人來說可能會比較沒那麼容易一眼就看懂。
:cat::這確實是可能的問題,那你有辦法提供更簡潔易懂的其他解法嗎?
:dog::我想到的是,可以將數字轉換成字串,直接去比較第一個字和最後一個字,第二個字和倒數第二字,以此類推下去,如果都相同就是迴文,如果有一個不同就不是迴文。
:cat::這樣確實有比較好理解,那請你實做看看。
:dog::
:dog::這樣就可以達到比較的效果了,也相對於第一個解法更直覺一點。不過如果要達到剛剛講的效果,其實
python
中對字串的切片功能也可以達到將字串反過來。
:cat::那請你實做一下這個功能如何操作呢?
:dog::
:cat::這樣程式碼看起來是更加簡潔了。好,那我今天就大概到這裡為止,你有什麼問題想問的嗎?
:dog::沒有了。
:cat::好的,那期待能再看到你。掰掰
:dog::謝謝,掰掰。
:cat::嗨,又見面了,這次是最後的面試了。你會很緊張嗎?
:dog::有一點緊張又有一點期待。
:cat::好,那之前有一次是要你找出,兩個數字加起來等於目標的題目,你還記得嗎?
:dog::記得。
:cat::那這次希望請你做看看把兩個數字加起來。但不是簡單的單純用加法加起來。會給你兩個數字,但這兩個數字是用 linked list 來表示的,像是325,會表示成node5指向node2指向node3,接著你需要把這兩個數字加起來,並且再用一樣概念的 linked list 表示。
:dog::(Repeat)好的,所以我會拿到兩個數字順序是反的的 linked list ,最後要回傳這兩個數字相加後的數字的 linked list 。這樣對嗎?
:cat::是的沒錯。
:dog::那我可以先把數字325用[5,2,3]來表示嗎?
:cat::可以。
:dog::(Example)好的,那我先舉個例子來確認我的理解有沒有錯,假設輸入[5,2,3]、[6,1,7]分別代表325和716,最後我要輸出[1,4,0,1],因為325+716=1041
,這樣對嗎?
:cat::沒錯。
:dog::(Approach)好的,那假設輸入[5,2,3],我等等會把325稱為「原始數字」這樣來表示。首先,需要先定義一下這個代表數字的 linked list ,它會有val
用來代表數值,next
用來代表指向誰。接著我們的輸入就是符合這個定義的 linked list 。得到兩個輸入後,我會想先把它們改寫成int
的格式,因為這樣相加起來會變得很容易。那要把他們變成int
的格式就需要分別跑過兩個輸入的所有node,先利用字串的加法會是直接把兩個字串接起來,我們可以把後面node的val
一直加在字串的前面,這樣就可以得到代表原始數字的字串格式,接著再利用python
內建函示把它轉成int
格式就好。之後將轉換好的兩個原始數字相加,接下來就是要把答案轉換成 linked list ,因為 linked list 需要把答案反過來接起來,因此可以利用之前使用過的取餘數的方式來取得最後數字一位,接著再依序接起來就可以了。
:cat::聽起來沒什麼問題,那就直接起你實做看看吧。
:dog::(Code)
:dog::那我們用[5,2]、[6]來檢驗看看,
l1=[5,2]
,第一次進到while
後,num1="5"
,第二次num1="25"
,因為"2"+"5"
,類推可以得到num2="6"
,接著num=31
,接著我們先建立一個假的 head ,利用取餘數的方式取得rem=1
,並建立一個新的node,tmp.val=1
,再將它和res
接起來,此時我們會得到0指向1,接著更新num=3
,取得rem=3
,並將新的node,tmp.val=3
,再和res
接起來,就會得到0指向1指向3的 linked list,最後只要回傳1指向3的部分就好,所以我們回傳head.next
。
:cat::好的,那我想問你,為什麼需要res
和head
這兩個 linked list呢?
:dog::哦,因為我們在接node的時候,res
指向的位置會不斷地往後更新,所以最後他會停在我剛剛舉例的3的位置,如果直接回傳res
會是錯的,但因為一開始有讓head
指向0的位置,所以只要回傳head.next
就可以得到1的位置,就會是正確的答案
:cat::好的,那沒什麼問題了,今天就先這樣,辛苦了
:dog::不會,你也辛苦了,謝謝。
:warning: 英文影片的連結有誤