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

解决办法:哈希表

知识点:

  • 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. Python-ConfigParser获取配置项名称大小写问题

    C:\Python27\Lib\ConfigParser.py: def optionxform(self, optionstr): return optionstr.lower() 会将配置文件中的 ...

  2. 关于jQ的Ajax操作

    jQ的Ajax操作 什么是AJAX AJAX = 异步的javascript和XML(Asynchronous Javascript and XML) 它不是一门编程语言,而是利用JavaScript ...

  3. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

  4. [从零开始搭网站七]CentOS上安装Mysql

    点击下面连接查看从零开始搭网站全系列 从零开始搭网站 通过前面6章,我们买好了服务器,配置了服务器连接,服务器上配置了JDK和Tomcat,准备了域名(这个我没教,自己去阿里/百度/腾讯买,买东西我相 ...

  5. IIS发布静态页面配置

    第一步:按照正常网站发布添加网站: 第二步:修改该网站的默认文档: 第三步:添加默认文档,把静态页的名称添加进去: 第四步:重启网站,浏览:

  6. Oracle Single-Row Functions(单行函数)——NULL-Related Functions

    参考资料:http://docs.oracle.com/database/122/SQLRF/Functions.htm#SQLRF006 Single-row functions return a ...

  7. [转][smart3d]Smart3D之手动配置 S3C 索引加载全部的OSGB瓦片数据

    转自:https://blog.csdn.net/u013719339/article/details/77840728/ 一.须知: S3C是Smart3D内部格式,实质上是一个分块模型的索引,可以 ...

  8. Linux若干源码编译

    Spark源码编译: dev/目录下执行make-distribution.sh./dev/make-distribution.sh --name 2.6.0-cdh5.7.0 --tgz Pyarn ...

  9. Django框架中,使用celery实现异步

    作用:在使用框架时,在视图函数中实现异步构成: 任务task:一段耗时并与响应结果无关的代码,如发短信 工人worker:新进程,用于执行任务代码 代理人broker:调用任务时,将任务添加到队列中, ...

  10. Java访问Redis

    Redis的数据类型总共有如下几种 1.String(字符串) 2.List(列表),字符串列表,有序 3.Hash(哈希),可以存储类似于数据库的表结构 4.Set(集合),无序,不可重复 5.ZS ...