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. 函数调用方法之__cdecl与_stdcall

    在debug VS c工程文件时,碰到cannot convert from 'int (__cdecl *)(char *)' to 'xxx'这个问题,发现问题在于typedef函数指针类型时,将 ...

  2. 设置cell高度的两种方法(label高度的可变引起cell高度可变的情况)

    第一种:(iOS8以后可用) 在Xib或stroyboard中(代码也可以) 利用AutoLayout设置好label的约束(比如可以设置四个边都距离屏幕50等方式,必须四个边都要固定好). 在代码部 ...

  3. ROS(一)Topic 通信

    ROS系统起源于2007年斯坦福大学人工智能实验室的项目与机器人技术公司Willow Garage的个人机器人项目(Personal Robots Program)之间的合作,2008年之后就由Wil ...

  4. Docker介绍及安装

    Docker介绍 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制, ...

  5. 使用Unity创建依赖注入

        这篇文章翻译自<Dependency Injection With Unity>第三章.文中提到的类似"前几节"的内容您不必在意,相信您可以看懂的. P.S:如 ...

  6. jmeter的脚本增强之参数化

    jmeter作为一款开源的测试工具,功能广泛,深受测试同胞们的喜爱,这次来讲讲关于如何参数化及其方式.那为什么要进行一个参数化呢,如做压测时,要有大量的数据来模拟用户的真实场景,像登录页面操作,系统是 ...

  7. Android 简介

    一 Android起源 android: 机器人 android是google公司开发的基于Linux2.6的免费开源操作系统 2005 Google收购 Android Inc. 开始 Dalvik ...

  8. tpo-09 C2 Return a sociology book

    check out 在library里有借书的意思 第 1 段 1.Listen to a conversation between a student and a librarian employe ...

  9. java 实现redis缓存

    由于项目加载时请求数据量过大,造成页面加载很慢.采用redis作缓存,使二次访问时页面,直接取redis缓存. 1.redis连接参数 2.连接redis,设置库 3.配置文件开启缓存 4.mappe ...

  10. 打包一个Docker镜像,让你的好友加载开启一个容器,并且每隔一秒输出hello,world到指定的文件中

    一.两个脚本代码 Dockerfile FROM bash COPY . /usr/herui/ WORKDIR /usr/herui/ CMD [ "sh", "hel ...