一. 题目
1. Two Sum
Total Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy
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.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.
二. 题意
- 给定一个数组和一个目标值
- 找出数组中两个成员,两者之和为目标值
- 假设一定存在一个解
三. 分析
- 算法核心:
- 三种方法:
- 暴力搜索: O(n^2): 超时
- 哈希表: O(n)
- 先快排, 后二分查找: O(nlogn) + O(nlogn)
- 先快排, 后使用双指针分别指向数组头和尾,同时双向遍历数组: O(nlogn) + O(n)
- 三种方法:
- 实现细节:
- 算法逻辑相对简单
- 实现细节相对容易
四. 题解
- 哈希表
- 实现
1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& nums, int target) { 4 vector<int> res; 5 unordered_map<int, int> m; 6 7 for (int i = 0; i < nums.size(); i++) m[nums[i]] = i; 8 9 for (int i = 0; i < nums.size(); i++) { 10 if (m.count(target - nums[i]) && m[target - nums[i]] != i) { 11 res.push_back(i); 12 res.push_back(m[target - nums[i]]); 13 return res; 14 } 15 } 16 17 return res; 18 } 19 };
- 结果
- 快排-二分查找
- 实现
1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& B, int target) { 4 vector<int> res; 5 vector<pair<int, int>> A; 6 7 for (int i = 0; i < B.size(); i++) { 8 A.push_back(make_pair(B[i], i)); 9 } 10 11 my_qsort(A, 0, A.size() - 1); 12 13 for (int i = 0; i <= A.size(); i++) { 14 int left = i + 1, right = A.size() - 1; 15 while (left <= right) { 16 int mid = left + (right - left) / 2; 17 if (A[mid].first == target - A[i].first) { 18 res.push_back(A[i].second); 19 res.push_back(A[mid].second); 20 return res; 21 } 22 if (A[mid].first < target - A[i].first) left = mid + 1; 23 else right = mid - 1; 24 } 25 } 26 27 return res; 28 } 29 private: 30 void my_qsort(vector<pair<int, int>>& A, int l, int r) { 31 if (l > r) return; 32 33 pair<int, int> key = A[l]; 34 int nl= l, nr = r; 35 while (l < r) { 36 pair<int, int> tmp; 37 while (A[r].first >= key.first && l < r) r--; 38 while (A[l].first <= key.first && l < r) l++; 39 40 41 tmp = A[l]; 42 A[l] = A[r]; 43 A[r] = tmp; 44 } 45 A[nl] = A[l]; 46 A[l] = key; 47 48 my_qsort(A, nl, l - 1); 49 my_qsort(A, l + 1, nr); 50 } 51 };
-
- 结果
- 快排-双指针
- 实现
1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& B, int target) { 4 vector<int> res; 5 vector<pair<int, int>> A; 6 7 for (int i = 0; i < B.size(); i++) { 8 A.push_back(make_pair(B[i], i)); 9 } 10 11 int left = 0, right = A.size() - 1; 12 my_qsort(A, 0, A.size() - 1); 13 14 while (left < right) { 15 if (A[left].first + A[right].first < target) left++; 16 else if (A[left].first + A[right].first > target) right--; 17 else {res.push_back(A[left].second), res.push_back(A[right].second); return res;}; 18 } 19 20 return res; 21 } 22 private: 23 void my_qsort(vector<pair<int, int>>& A, int l, int r) { 24 if (l > r) return; 25 26 pair<int, int> key = A[l]; 27 int nl= l, nr = r; 28 while (l < r) { 29 pair<int, int> tmp; 30 while (A[r].first >= key.first && l < r) r--; 31 while (A[l].first <= key.first && l < r) l++; 32 33 34 tmp = A[l]; 35 A[l] = A[r]; 36 A[r] = tmp; 37 } 38 A[nl] = A[l]; 39 A[l] = key; 40 41 my_qsort(A, nl, l - 1); 42 my_qsort(A, l + 1, nr); 43 } 44 };
-
- 结果
时间: 2024-10-25 20:35:38