001. Two Sum

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了001. Two Sum相关的知识,希望对你有一定的参考价值。

 1 class Solution {
 2 public:
 3     vector<int> twoSum(vector<int>& nums, int target) {
 4         map<int, int> m;
 5         vector<int> res;
 6         for (size_t i = 0; i < nums.size(); ++i) m[nums[i]] = i;
 7         for (size_t i = 0; i < nums.size(); ++i) {
 8             int gap = target - nums[i];
 9             if (m.find(gap) != m.end() && m[gap] != i) {
10                 res.push_back(i);
11                 res.push_back(m[gap]);
12                 break;
13             }
14         }
15         return res;
16     }
17 }; 

 

以上是关于001. Two Sum的主要内容,如果未能解决你的问题,请参考以下文章

No.001:Two Sum

001-Two Sum

Leetcode001. Two Sum

LeetCode刷题记录001Two Sum

LeetCode 001 Two Sum - Java

001. Two Sum