LeetCode #1 TwoSum
Description:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
題意:給定一個數組,和一個定值,找出數組元素的兩個下標,這兩個元素之和要等於給的定值
思路:
首先我們想到的是暴力法,兩個 for 循環求解,時間復雜度為O(n^2)
//Runtime: 106 ms //First thought: BF class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { int i,j; vector<int> ret {0, 1}; for (i = 0; i< nums.size(); ++i) { ret[0] = i; for(j = i+1; j < nums.size(); ++j){ if (nums[i] + nums[j] == target) { ret[1] = j; return ret; } } } } };
但是,我發現時間消耗太多了,所以借鑒當時“換硬幣”、“爬樓梯”問題的優化方法,由於num[i]、num[j]、target都是定值,所以在條件判斷裏不需要每次都進行加運算
//Runtime: 79 ms //Because nums[i], nums[j], target are fixed values, we do not need to do additions every time class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { int i,j; vector<int> ret {0, 1}; for (i = 0; i< nums.size(); ++i) { ret[0] = i; int tar = target - nums[i]; for(j = i+1; j < nums.size(); ++j){ if (nums[j] == tar) { ret[1] = j; return ret; } } } } };
AC後,看了 Discuss 裏頭的方法以及婁神的博客,我采用了更復雜的數據結構 散列表(HashTable)以降低時間復雜度,我的想法是這樣: 如果想只掃描一遍數組就得出結果,那麽肯定就要有一部字典,邊掃描邊存儲值,在這裏存儲的不是數組當前的值,而是“目標值 - 當前值”,我們稱之為對象值。
也就是說,字典裏存儲的是每個數據所希望的”另一半“的大小。所以,字典的 Key 是對象值,字典的 Value 是數組索引。然後我們再往後掃描,如果掃描到的值的另一半出現在了字典裏,那麽說明當前值是”上一半“所需要的”下一半“,此時將它們的索引存儲在 ret[0]、ret[1]中並返回;如果沒有字典裏沒有出現它的另一半,那麽把對象值和當前索引繼續存儲字典中。
//Runtime: 6 ms #include<unordered_map> using std::unordered_map; class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int,int> um; vector<int> res(2); int i; int n = nums.size(); for (i = 0; i < n; ++i) { if (um.find(target - nums[i]) != um.end()) { res[0] = um[target - nums[i]]; res[1] = i; return res; } else { um[nums[i]] = i; } } um.clear(); } };
LeetCode #1 TwoSum