leetcode(E)two sum

来源:互联网 发布:linux文件复制命令 编辑:程序博客网 时间:2024/06/05 21:14
  1. Two Sum QuestionEditorial Solution My Submissions
    Total Accepted: 344929
    Total Submissions: 1217901
    Difficulty: Easy
    Contributors: Admin
    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.

Subscribe to see which companies asked this question

Show Tags
Show Similar Problems

代码:

class Solution {public:    vector<int> twoSum(vector<int>& nums, int target) {        multimap<int,int> m;vector<int> v;        for(int i=0;i<nums.size();++i){            if(m.count(target-nums[i])&&m[target-nums[i]]!=i){                v.push_back(i);                v.push_back(m[target-nums[i]]);                return v;            }        }    }};

末尾挨个给v push_back其实没有比要,直接return (i, m[target-nums[i])

0 0