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

解决办法:哈希表

知识点:

  • 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. github删除某个库repository

    1.登陆gihub网站,在该选中需要删除的repository,点击进去 2.删除repository 点击进去以后进入新的页面,拉到页面尾部,如图 然后弹出确认框,再输入需要删除的repositor ...

  2. Dalvik VM 和JVM的比较

    避免出现版权问题android重写JVM 两者在编译后的文件格式区别: JVM: .java->.class->.jar DALVIK VM:.java->.class->.d ...

  3. django网站地图sitemap

    网站地图是根据网站的结构.框架.内容,生成的导航网页,是一个网站所有链接的容器.很多网站的连接层次比较深,蜘蛛很难抓取到,网站地图可以方便搜索引擎或者网络蜘蛛抓取网站页面,了解网站的架构,为网络蜘蛛指 ...

  4. js_计时器之setInterval

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 《剑指offer》第五十四题(二叉搜索树的第k个结点)

    // 面试题54:二叉搜索树的第k个结点 // 题目:给定一棵二叉搜索树,请找出其中的第k大的结点. #include <iostream> #include "BinaryTr ...

  6. public,protected,private

    1.public:public表明该数据成员.成员函数是对所有用户开放的,所有用户都可以直接进行调用 2.private:private表示私有,私有的意思就是除了class自己之外,任何人都不可以直 ...

  7. QT5 解决QSqlDatabase: QMYSQL driver not loaded 问题

    QT版本 Qt 5.12.0 MySQL版本 8.0.13 转到MySQL的安装目录 G:\mysql-8.0.13-winx64\mysql-8.0.13-winx64\lib 将安装目录下的两个文 ...

  8. 使用http://start.spring.io/ 生成工程

    今天学习spring-cloud,无意中发现一个spring提供的构建工程的页面,想记录下,发现有个博客写的很好就直接抄过来了.  原文链接: https://blog.csdn.net/u01050 ...

  9. VNC安装配置

    1,安装VNC yum install tigervnc-server -y2,设定密码: vncpasswd root 3,配置服务文件,下面是配置了6个窗口. cp /lib/systemd/sy ...

  10. Golang socket

    1.本例子实现了一个简单的TCP echo.客户端发送Hello,服务端回应World. 参考:<Socket编程> 2.服务端代码 package main import ( " ...