1.首先想到的方法就是两个for循环全部遍历,代码如下,可通过,但效率太低

 class Solution
{
public:
vector<int> twoSum(vector<int> nums, int target)
{
vector<int> res;
for (int i = ; i < nums.size(); i++)
{
for (int j = i + ; j < nums.size(); j++)
{
if (nums[j] == target - nums[i])
{
res.push_back(i);
res.push_back(j);
}
}
}
return res;
}
};

2.使用unordered_map,遍历vector中每个元素,并在hash表中通过find()查找目标元素,若找到则写入结果,否则将当前元素加入到hash表中。(每次调用find()函数是为了判断当前元素与其前面的元素之和是否为target值)。

 class Solution
{
public:
vector<int> twoSum(vector<int> nums, int target)
{
unordered_map<int,int> hash;
vector<int> res;
for (int i = ; i < nums.size(); i++)
{
int numTofind = target - nums[i]; if(hash.find(numTofind) != hash.end())
{
res.push_back(hash[numTofind]);
res.push_back(i);
}
else
{
hash[nums[i]] = i;
}
}
return res;
} };

[Leetcode] 1.Two Sum(unordered_map)的更多相关文章

  1. leetcode 1 Two Sum(查找)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  2. 「LeetCode」0001-Two Sum(Ruby)

    题意与分析 题意直接给出来了:给定一个数,返回数组中和为该数(下为\(x\))的两个数的下标. 这里有一个显然的\(O(n)\)的实现:建立一个hash表,每次读入数(记作\(p\))的时候查询has ...

  3. LeetCode 1 Two Sum(二分法)

    题目来源:https://leetcode.com/problems/two-sum/ Given an array of integers, find two numbers such that t ...

  4. LeetCode 1. Two Sum (JavaScript)

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  5. leetcode 之 two sum (easy)c++

    1.数组的长度 length() .容器vector长度  size() .容器vector vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库. ...

  6. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  7. Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum)

    Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum) 深度优先搜索的解题详细介绍,点击 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在 ...

  8. Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance)

    Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance) 深度优先搜索的解题详细介绍,点击 给定一个保存员工信息的数据结构,它包含了员工唯一的id ...

  9. Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)

    Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...

随机推荐

  1. 如何在VMware Fusion中导入windows下的虚拟机

    最近换了新款的mbp,因为偷懒,便将之前在windows台式机上的虚拟机搬了过来. 特此记录下搬运过程,方便以后查看. 一 操作过程 安装激活VMware 常规操作,无需赘言 拷贝windows下虚拟 ...

  2. C#中如何使用JS脚本

    C#中如何使用JS脚本 目前在做的组态软件中就使用到了js脚本,这部分js脚本是供用户编写的,用户可以通过我们提供的脚本以及js自身的逻辑,用户就可以随心所欲的控制设备的运行.有比较了几款在C#中执行 ...

  3. Educational Codeforces Round 46 (Rated for Div. 2) D. Yet Another Problem On a Subsequence

    这个题是dp, dp[i]代表以i开始的符合要求的字符串数 j是我们列举出的i之后一个字符串的开始地址,这里的C是组合数 dp[i] += C(j - i - 1, A[i]] )* dp[j]; # ...

  4. 北京Uber优步司机奖励政策(1月11日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. Python3 之选课系统

    项目介绍:项目名称:(oldboy选课系统)项目功能: 分为 学员视角, 老师视角 , 管理员视角 学员视角{ (注册 登录 个人中心 选课 学习 上课) 登录 就是登录 注册: 填写 资料 信息 完 ...

  6. getSteam

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  7. jQuery wordexport导出 word

    同事给我说了简单的导出word的插件,亲测了下,做个随笔. 这个导出插件是jQuery自带的的插件,通过调用wordexport.js来实现导出功能. 1.引入的js <script type= ...

  8. jmeter属性设置

    使用${__setProperty(newuserid,1,)}函数对属性进行设置 2.使用${__P(userid)}函数在其他线程组中引用该属性 备注: 1.JMeter属性在测试脚本的任何地方都 ...

  9. mysql新手进阶02

    云想衣裳花想容,春风拂槛露华浓. 若非群玉山头见,会向瑶台月下逢. 现在有一教学管理系统,具体的关系模式如下: Student (no, name, sex, birthday, class) Tea ...

  10. Oracle作业练习题

    第一问 //登陆scott用户 //解锁 alter user scott account unlock; //给用户申请密码 alter user scott identified by tiger ...