leetcode之twosum
1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& nums, int target) { 4 vector<int>result; 5 int i,j,k; 6 map<int,int>h; 7 for(i=0;i<nums.size();i++) { 8 if(!h.count(nums[i])) 9 h[nums[i]]=i+1; 10 k=h[target-nums[i]]; 11 if(k>0&&k!=i+1) { 12 result.push_back(k-1); 13 result.push_back(i); 14 break; 15 } 16 } 17 return result; 18 19 } 20 };