跟讀練習: Two Sum - Leetcode 1 - HashMap - Python - 通過影片學習英語口語
選擇練習模式
每種模式都會開啟全屏練習室。
110 句
如果句子過短或過長,請點選 Edit 進行調整。
1
Let's solve leak code 1 to sum, the most popular leak code question.
2
So we're given an input array and some target, in this case 9.
3
And we want to find the two values in this input array that sum to 9.
4
So in this case, it's 2 and 7.
5
Now we want to return the indices of these two values.
6
So the index of 2 is 0, the index of 7 is 1.
7
So we return 0 and 1.
8
We're guaranteed that there's exactly one solution, so we don't have to worry about not finding a solution, and we don't have to worry about multiple solutions.
9
Now, the most intuitive way to solve this problem is basically just check every combination of two values
10
and see if they can sum up to our target.
11
So we start at two, we check every combination we can make that includes two.
12
So we scan through the remainder of the array, one, five, three, and check if any of those numbers added to two sums to our target four.
13
In this case, none of them do.
14
So next we can repeat the process.
15
Let's check every combination including one that sums up to target four.
16
So we scan through every element that comes after it, five and three, and we find that one added with three sums up to our target four.
17
Notice that we didn't have to check the values
18
that came before one because we already checked the combination two and one when we were up over here.
19
Remember when we checked every combination with 2.
20
So we didn't have to repeat that work down here.
21
We only had to check the numbers that came after 1.
22
So the runtime of this algorithm isn't super efficient.
23
This is basically brute force.
24
We're going through the entire array of length n and we're going to do that worst case n times for each number.
25
This means that overall worst case time complexity will be O of n squared.
26
So can we do better?
27
Now the thing to notice is that for each number,
28
for example, 1, the value we're looking for is the difference between the target and this value 1.
29
So we're looking for 4 minus 1, which is equal to 3.
30
So that means this is the only value we can add to 1 that'll equal the target.
31
So we don't have to check every number, we just want to know if 3 exists.
32
Now the easiest way we can do this, the most efficient, is by making a hash map of every value in our input array
33
so we can instantly check if the value 3 exists.
34
Now let's try the same problem except let's use a hash map this time.
35
Now in our hash map, we're going to be mapping each value to the index of each value.
36
So the index of 2 is 0, the index of 1 is 1, the index of 5 is 2, the index of 3 is 3.
37
So in our hash map, we're going to be mapping the value to the index.
38
now we could add every value in this array into the hash map before we start iterating through it
39
but there's actually an easier way to do it
40
if we added the entire array into the hash map initially
41
then we would get to the value 2 first right we
42
would want to check does the difference between target 4 minus this value 2
43
which is equal to 2 exists in our hash map and we would find that 2 does exist in our hash map, but we're not allowed to reuse the same one, right?
44
Because they're both at the same index.
45
We can't use the same value twice, so we would have to compare the index of our current
46
two with the index of the two that's in our hash map.
47
There's actually an easier way to do this though, and it's a little clever, and let me show you how to do it that way.
48
So doing it this clever way, initially we say our hash map is empty.
49
So we get to the value two first of all, right?
50
And we want to look for the difference 4 minus 2 in our hash map.
51
Our hash map is empty, so we don't find 2.
52
So then, after we've visited this element, then we can add it to our hash map.
53
So now that I'm done visiting it, I'm going to move to the second element 1.
54
And before I do that, I'm going to add this value 2 to our hash map, and the index of this value is going to be 0.
55
Now I'm at 1.
56
I'm looking for 4 minus 1, which is 3.
57
I see 3 isn't in our hash map, but it actually is in our array.
58
So what's the problem?
59
Well, for now, we're going to say we don't find a 3.
60
So we add 1 to our hash map.
61
The index of this 1 is 1.
62
And now we move to the next element, 5.
63
we check does 4 minus 5 uh it's 4 minus 5 exist in our hash map that's negative 1
64
so no it does not then we add this 5 to our hash map
65
and it's index which is 2
66
and we move to the last value in the array 3
67
we check does 4 minus 3 exist in our hash map now that's 1
68
so we see it does exist right over here.
69
The value exists and its index is one.
70
So now we found our two values that sum to the target and we want to return their indexes,
71
their indices, which are going to be one and three.
72
So with this algorithm, we don't have to initialize our hash map.
73
It can be initially empty and then we can just iterate through this array in one pass.
74
Now the reason the algorithm can work in that way with just one pass is this.
75
So let's say we had a giant array, right?
76
We know for sure that there are two elements in this array that sum to our target, right?
77
We don't know where they are.
78
They're at some arbitrary location.
79
When we visit the first one of those elements, our hash map is only gonna be this portion of the array.
80
It's only gonna have the values that came before the first value.
81
So we're going to notice that the second value
82
that can sum to the target is not going to be in our hash map yet.
83
But once we get to the second value, our hash map is going to be this portion.
84
So every value that comes before this, right?
85
So we're going to be guaranteed that once we visit the second element that sums up to the target, we're going to be guaranteed that the first one is already in our hash map.
86
So we're guaranteed to find the solution.
87
Now, since we only have to iterate through the array once, and we're adding each value to our hash map, which is a constant time operation,
88
and we're checking if a value exists in our hash map, which is also a constant time operation, the time complexity is going to be big O of n.
89
We are using extra memory, right?
90
That hash map isn't free, so the memory complexity is also going to be O of n
91
because we could potentially add every value to the hash map.
92
So now let's code the solution.
93
So remember we need a hash map, right?
94
I'm going to call this previous map because it's basically every element that comes before the current element.
95
Every previous element is going to be stored in this map.
96
We're going to be mapping the value to the index of that value.
97
So now let's iterate through every value in this array.
98
We need the index as well as the actual number.
99
So let's do it like this in Python.
100
Before we add this to our map, let's check if the difference, which is equal to target minus n.
101
Now let's check if this difference is already in the hash map.
102
if it is, then we can return the solution, which is going to be a pair of the indices.
103
So I can get the first index like this, and the second index is just i.
104
Now if we don't find the solution, then we have to update our hash map.
105
So for this value n, I'm going to say the index is i, and then we're going to continue.
106
Since we're guaranteed that a solution exists, we don't have to return anything out here, right?
107
But I'll just put a return for no reason.
108
Now let's see if it works. And it works perfectly.
109
So with this kind of neat little trick with just doing it in one pass, you can reduce the amount of code you have to write
110
and not have to worry about like edge cases and comparisons and things like that.
同頻道
FOR YOU
推薦影片
為什麼通過這個影片練習口語?
在學習英語的過程中,練習口語至關重要。觀看這段關於“兩數之和”的影片,不僅能幫助你理解程式設計邏輯,還能給你提供實踐口語的機會。通過模仿影片中的講者,你可以提高自己的發音、語調和表達能力,讓自己在實際對話中更加流利自然。使用 shadow speech 技巧進行練習時,你會發現自己能夠更好地掌握英語的語感,提升自己的口語能力,尤其是在 雅思口語練習 方面。
語法與表達分析
在這個影片中,講者使用了一些核心表達方式,這些表達在日常交流中也非常常見:
- 我們想要找到…… - 這種句式用於闡明目標或需求,比如“我們想要找到兩個加起來為9的數值”。
- 我們保證…… - 用於強調某種情況的確定性,比如“我們保證只有一個解決方案”。
- 實際上…… - 用於轉折和引入更深入的討論,比如“實際上,最直觀的方法是檢查每個組合”。
- 注意…… - 這是一個指引聽眾注意關鍵資訊的表達方式,幫助提升溝通的效率。
利用 看YouTube學英語 這種方式,可以讓你在實際語境中學習這些表達,同時增強記憶深刻性。
常見發音陷阱
在這段影片中,有幾個單詞和短語可能會對學習者造成發音上的挑戰:
- array(陣列) - 學習者常常在母音發音上出現偏差,可能會誤讀為“埃瑞”而非“啊瑞”。
- sum(和) - 注意這個單詞的短母音發音,很多學習者可能會發成“薩姆”。
- target(目標) - “塔戈特”的讀音常常被混淆,確保每個音節都準確無誤。
通過 shadowspeak 的方法反覆練習這些單詞,能夠幫助學習者掌握更標準的發音,提升他們的英語口語能力。
影片中的語法
說話人最常用的結構,並附上影片中的原話:
| 結構 | 影片中的用法 |
|---|---|
| 定語從句 who / which + 從句 — 補充說明人或事物 | index which is · indices, which are · map, which is |
| 被動語態 be + 過去分詞 — 強調發生了什麼,而不是誰做的 | be guaranteed · be stored |
什麼是跟讀法?
跟讀法 (Shadowing) 是一種有科學依據的語言學習技巧,最初開發用於專業口譯員的培訓,並由多語言者Alexander Arguelles博士普及。這個方法簡單而強大:您在聽英語母語原聲的同時立即大聲重複——就像是一個延遲1-2秒緊跟說話者的影子。與被動聽力或語法練習不同,跟讀法強迫您的大腦和口腔肌肉同時處理並模模擬實的講話模式。研究表明它能顯著提高發音準確性,語調,節奏,連讀,聽力理解和口語流利度——使其成為雅思口語備考和真實英語交流最有效的方法之一。














