LeetCode 1. Two Sum (c++ stl map)

题目:https://leetcode.com/problems/two-sum/description/

 

stl map代码:

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

 

posted @ 2017-12-15 20:31  ninding  阅读(123)  评论(0编辑  收藏  举报