Leetcode日记

1.two sum

问题追踪

STL vector用法介绍

这里写图片描述

这里写图片描述

error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

error This file requires compiler and library support for the \

  • 查看g++编译器的版本

这里写图片描述

  • 4.8以上版本支持C++ 11标准
  • 配置文件写完后,source 命令可以立即刷新命令,而不用重新开机
  • 配置文件选择为~/.bashrc,然后在该文件下添加如下的命令

这里写图片描述

这里写图片描述

这里写图片描述

unordered_map - C++ Reference(英)

unordered map - Microsoft(中)

unsordered_map的具体用法


#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>         //C++ 11 standard

using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int> &numbers, int target) {
        unordered_map<int, int> umap;
        // initialize the variable with 0 to prevent none result

        vector<int> output(2);    // vector 2 data ,default data

        for (int i = 0; i < numbers.size(); i++)
        {
            // Avoid the situations like 4 = 8 - 4
            if (umap.find(numbers[i]) != umap.end())
            {
                output[0] = umap[numbers[i]] + 1;
                output[1] = i + 1;
            }
            else
            {
                umap.insert(make_pair(target - numbers[i], i));  
            }
        }
        return output;
    }
};

int main()
{
    Solution s1;
    vector<int> numbers = {1,2,4,4,5};
    int target = 9;

    vector<int> output = s1.twoSum(numbers, target);
    cout << output[0] << " " << output[1] << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值