題目
Given an array of integers(nums), return indices of the two numbers such that they add up to a specific target(target).
You may assume that each input would have exactly one solution, and you may not use the same element twice. (Link)
思路
兩數相加= target,若兩數相同,則兩數都為target的一半;若兩數不同,則一個會大於target的一半,另一個會小於target的一半。所以第一個想法是先以(target/2)作為分界,將nums分成大於他及小於他的兩個array,然後從其中一個array的每個項目開始,看看是否能在另一個array中找到一個數字能相加後等於target。
Pseudocode
1 | a = target/2 |
Code
1 | def two_sum(nums, target) |
分析
此演算法的運作時間:
nums.count(a)為O(n)- 兩次
nums.select皆為O(n) each迴圈裡包著include?,是為O(n2)
整體Time complexity 為 O(n2)
參考他人作法
1 | def two_sum(nums, target) |
分析
利用Hash來儲存已經找尋過的num及他的index,如此一來就不需要寫兩個迴圈,只需往後找target-num是否存在Hash裡,就能知道答案了。由於each迴圈為O(n),而Hashtable為O(1),所以整體的Time complexity只有O(n)。