码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-12-15 21:35:27      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:vector   etc   http   nbsp   tor   post   style   题目   class   

题目: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;
    }
};

 

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

标签:vector   etc   http   nbsp   tor   post   style   题目   class   

原文地址:http://www.cnblogs.com/zhangjiuding/p/8044736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!