首页 > 代码库 > 001. Two Sum

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