题目介绍

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

参考答案

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> hp;
int n = nums.size();
vector<int> res; for(int i=;i<n;i++){ int rest = target - nums[i];
if(hp.find(rest) != hp.end()){
res.push_back(hp[rest]);
res.push_back(i);
return res;
}
hp[nums[i]] = i; // map[key] = value -> map[num] = index;
}
return res;
}
};

补充说明

循环分成三部分:

1.  剩下的 = 目标 - 现在的

2. 检查剩下的是否在map里,在的话,就说明是数组里面的数字,可以返回index了。

3. 将该数字存入map中

有一些要特别注意的是:

map[ key ] = value -> map[num] = index ,以数字为key, 以存入的内容为index。所以,搜索的时候,以key(即数字)为依据,而 index 为我们要的结果。

LC 1. Two Sum的更多相关文章

  1. LC 918. Maximum Sum Circular Subarray

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  2. LC 494. Target Sum

    问题描述 You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 ...

  3. LC 813. Largest Sum of Averages

    We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the su ...

  4. LC 377. Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. LC 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. LC 677. Map Sum Pairs

    Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...

  7. [LC] 1099. Two Sum Less Than K

    Given an array A of integers and integer K, return the maximum S such that there exists i < j wit ...

  8. [LC] 167. Two Sum II - Input array is sorted

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  9. [LC] 437. Path Sum III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  10. [LC] 113. Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

随机推荐

  1. web软件测试基础系统测试简化理论

    系统测试点主要如下 1.系统测试基础-2.测试对象与测试级别-3.系统测试类型-4.系统测试方法-5.系统测试之软件测试质量. 1.系统测试:是尽可能彻底地检查出程序中的错误,提高软件系统的可靠性. ...

  2. Selenium执行cdp命令,driver.execute_cdp_cmd用法

    Chrome自带的开发者工具DevTools功能非常强大.有时候我们在使用Selenium操作浏览器时需要通过调用一下DevTools的方法来完成一些设置,如模拟移动设备,弱网模拟等等. Seleni ...

  3. BZOJ 4732 UOJ #268 [清华集训2016]数据交互 (树链剖分、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=4732 (UOJ) http://uoj.ac/problem/268 题解 ...

  4. 编写第一个Servlet程序

    在开始这一节之前呢,我们还需要把Tomcat配置到Eclipse中,配置的方式很简单,打开Eclipse,Window,Preferences,进入到这个页面 将Tomcat的安装目录配置到Eclip ...

  5. 如何配置WAMP环境(主要是Apache与PHP)

    1.配置Apache 1.编辑Apache配置文件---http.conf(位于安装目录下的conf子目录内): 2.修改Document Root 和 Directory选项,这是修改Apache的 ...

  6. linux下如何使rtc设备注册为指定的设备文件/dev/rtc1?

    答: 通过设备树中的aliases节点来指定即可; 如某rtc设备的节点名为rtc@68,那么想让系统为该设备生成指定的设备文件/dev/rtc1,那么就在设备树的根节点中增加aliases节点,示例 ...

  7. oracle传输表空间

    https://blog.csdn.net/ch7543658/article/details/39271135/ Oracle expdp/impdp常用性能优化方法 1.查看操作系统endiann ...

  8. ServletConfig对象的使用

    作用: * 获取Servlet的配置信息 * 获取ServletContext对象 *主要是用于加载servlet的初始化参数.在一个web应用可以存在多个ServletConfig对象(一个Serv ...

  9. XM概述

    概述: Extensible Markup Language: 可扩展的标记语言 特点: 语法很严格 标签自定义 作用: * 存储数据 * 做配置文件 * 用于进行数据传输 文档声明: 标示这个文档是 ...

  10. jemter 90%line的解释

    假如: 有10个数: 1.2.3.4.5.6.7.8.9.10    按由大到小将其排列. 求它的第90%百分位,也就是第9个数刚好是9 ,那么他的90%Line 就是9 . 另一组数: 2.2.1. ...