题目介绍

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. setjmp

    #include <setjmp.h> #include <stdio.h> jmp_buf j; void raise_exception(void) { printf(&q ...

  2. mysql 查询整个数据库所有表的行数

    >use information_schema; >select sum(table_rows) from tables where TABLE_SCHEMA = "test&q ...

  3. 初次接触webpack

    1.学习地址 中文文档 https://www.webpackjs.com/concepts/ webpack-dev-server配置说明 https://www.webpackjs.com/con ...

  4. Docker安装mysql5.6

    1.docker hub 上查找mysql镜像 2.在docker hub上(阿里云加速器)拉取mysql镜像到本地标签为5.6 3.使用mysql5.6创建容器(也叫运行镜像) 4.交互运行,进入m ...

  5. 走进JavaWeb技术世界1:Web后端与J2EE的由来

    转自:微信公众号 码农翻身 这个问题来自于QQ网友,一句两句说不清楚,索性写个文章. 我刚开始做Web开发的时候,根本没有前端,后端之说. 原因很简单,那个时候服务器端的代码就是一切:接受浏览器的请求 ...

  6. IDEA中log4j.properties配置文件详解

    配置实例 ### 配置根 ### log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,D ...

  7. CDH构建大数据平台-配置集群的Kerberos认证安全

     CDH构建大数据平台-配置集群的Kerberos认证安全 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 当平台用户使用量少的时候我们可能不会在一集群安全功能的缺失,因为用户少,团 ...

  8. python select模块

    Python select 一.前言 Python的select()方法直接调用操作系统的IO接口,它监控sockets,open files, and pipes(所有带fileno()方法的文件句 ...

  9. ubuntu tensorflow cpu faster-rcnn 测试自己训练的模型

    (flappbird) luo@luo-All-Series:~/MyFile/tf-faster-rcnn_box$ (flappbird) luo@luo-All-Series:~/MyFile/ ...

  10. Insomni’hack CTF-l33t-hoster复现分析

    题目地址: https://github.com/eboda/insomnihack/tree/master/l33t_hoster 源码如下: <?php if (isset($_GET[&q ...