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

解决办法:哈希表

知识点:

  • 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. 什么是mvc?

    MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型  (Model).视图(View)和控制器(Controller).      ...

  2. Python Scrapy安装

    直接安装scrapy 各种报错,后来各种百度终于解决了,如下是亲身的经历. pip install scrapy 这样直接会报错. 第一步: 先安装wheel pip install wheel 第二 ...

  3. Qt基础学习(3)-----滑动条之QSlider

    //mydialog.h #ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog> class QLineEdit; class Q ...

  4. 如何查找CpG Islands, CpG shores等 --转载

    转载自: http://blog.sciencenet.cn/blog-306699-1033567.html 在homo species物种,随着研究的深入,注释信息不断丰富 针对CpG Islan ...

  5. 【六】php 错误异常处理

    错误异常处理 概念:代码在try代码块被调用执行,catch代码块捕获异常 异常需要手动抛出 throw new Exception (‘message’,code) throw将出发异常处理机制 在 ...

  6. ZZNU 正约数之和 2094

    #include<iostream> #include<cstring> #include<queue> #include<cstdio> #inclu ...

  7. STL_string.ZC

    1.转成 小写/大写 #include <algorithm>using namespace std; // 转成小写transform(_strAttrNameG.begin(), _s ...

  8. windows与linux换行符

    我一次linux上写的脚本,利用vim进行脚本编写,然后下载下来在nodepad++上面打开,在nodepad上面新建了一个文件将原来文件内容复制过去,保存后下载复制文件在linux上面进行运行,发现 ...

  9. 力扣(LeetCode)965. 单值二叉树

    如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树. 只有给定的树是单值二叉树时,才返回 true:否则返回 false. 思路 递归 java版 /** * Definition for ...

  10. leecode第六十一题(旋转链表)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...