暴力法可解决,速度很慢。

解决办法:哈希表

知识点:

  • map的构造
  • 遍历map使用迭代器,判断条件
  • 插入  pair<int,int>
  • 寻找key是否存在
    class Solution {
    public:
    vector<int> twoSum(vector<int>& nums, int target) {
    map<int,int> m;
    map<int,int>::iterator itr;
    vector<int> ans;
    for(int i=; i<nums.size(); i++)
    { if(i == )
    {
    m.insert(pair<int,int>(nums.at(i),i));
    continue;
    } itr = m.find(target-nums.at(i));
    if(itr != m.end())
    {
    ans.push_back(itr->second);
    ans.push_back(i);
    return ans;
    }
    m.insert(pair<int,int>(nums.at(i),i));
    }
    }
    };

1.两数之和(Two Sum) C++的更多相关文章

  1. 领扣-1/167 两数之和 Two Sum MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. [Swift]LeetCode1 .两数之和 | Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  3. 两数之和 Two Sum

    给定一个整数数列,找出其中和为特定值的那两个数. 你可以假设每个输入都只会有一种答案,同样的元素不能被重用. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 n ...

  4. c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538

    第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a> ...

  5. LeetCode 1:两数之和 Two Sum

    题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中 ...

  6. LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)

    653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...

  7. LeetCode_#1_两数之和 Two Sum_C++题解

    1. 两数之和 Two Sum 题目描述 Given an array of integers, return indices of the two numbers such that they ad ...

  8. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  9. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

随机推荐

  1. C#题目及答案(1)

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成 ...

  2. 【Ruby】【变量】

    知识点[Ruby 中$开头的全局变量.内部变量.隐藏变量介绍] Ruby 中充满了一系列的隐藏变量,我们可以从这些预定义的全局变量中获取一些有意思的信息. 全局进程变量 $$ 表示当前运行的 ruby ...

  3. 虚拟现实外包公司—北京动点飞扬软件承接VR/AR软件、游戏外包

    欢迎通过以下方式联系北京动点飞扬软件外包业务咨询QQ:372900288 (全天在线)   咨询邮箱:SLteam@vip.qq.com   同时我们还承接 HTML5外包. kinect外包.Uni ...

  4. Redis的EXPIRE过期机制介绍

    概述在实际开发过程中经常会遇到一些有时效性数据,比如限时优惠活动,缓存或者验证码之类的.过了一段时间就需要删除这些数据.在关系型数据库中一般都要增加一个字段记录数据的到期时间,然后周期性地检查过期数据 ...

  5. c++ demo

    #include "stdafx.h" #include <boost\version.hpp> #include <boost\config.hpp> # ...

  6. Qt5_选择文件对话框

    1. 类似如下代码: QString fileName = QFileDialog::getOpenFileName( this, tr("Open File"), NULL,// ...

  7. 华硕N55SF 折腾记

    家里给老妈用的N55SL是2011年买的.这么多年了,最近一身毛病,最近来回折腾很多次.吐槽记录如下 1不定时花屏.闪屏,然后偶尔硬盘掉盘 (找不到硬盘,开机BIOS里).主要是使用一段时间之后发热, ...

  8. Eclipse Jee环境配置

    最近下载了新的Eclipse Jee Neon版本,记录一下如何进行开发环境的配置. 1.下载必要的开发环境文件 ①下载Java SE Development Kit (简称JDK) ②下载Tomca ...

  9. js判断安卓客户端或者是ios客户端,是否是微信浏览器

      代码: function xaizai() {  var u = navigator.userAgent, app = navigator.appVersion;  var isAndroid = ...

  10. windows安装使用docker

    doker就是一个容器,如果想要在windows安装还必须要用另外一个工具docker-toolbox.下载地址:https://mirrors.aliyun.com/docker-toolbox/w ...