题目介绍

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:

  1. Given nums = [2, 7, 11, 15], target = 9,
  2.  
  3. Because nums[0] + nums[1] = 2 + 7 = 9,
  4. return [0, 1].

参考答案

  1. class Solution {
  2. public:
  3. vector<int> twoSum(vector<int>& nums, int target) {
  4. unordered_map<int,int> hp;
  5. int n = nums.size();
  6. vector<int> res;
  7.  
  8. for(int i=;i<n;i++){
  9.  
  10. int rest = target - nums[i];
  11. if(hp.find(rest) != hp.end()){
  12. res.push_back(hp[rest]);
  13. res.push_back(i);
  14. return res;
  15. }
  16. hp[nums[i]] = i; // map[key] = value -> map[num] = index;
  17. }
  18. return res;
  19. }
  20. };

补充说明

循环分成三部分:

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. Ubuntu18.04安装和配置Django,并实现简单示例

    一.前言(系统,django介绍,window.mac.linux简单区别) Django是python开发过程最重要的web框架.因为在看的Django教学视频是在mac下安装的,我自己用的是Lin ...

  2. redis之不重启,切换RDB备份到AOF备份

    确保redis版本在2.2以上 [root@pyyuc /data 22:23:30]#redis-server -v Redis server v=4.0.10 sha=00000000:0 mal ...

  3. 带下划线的 HTTP Header无法获取到可能是因为nginx

    背景:新版本修改了个功能是在老版本的基础上做的,同一个接口,需要兼容老版本,因此让前台在header中封装了 version版本号,client_type 客户端类型,根据这两个字段判断接口要走的逻辑 ...

  4. Vue开发工具VS Code与调试

    vscode安装 进入vscode官网(https://code.visualstudio.com/Download)vscode插件安装进入vscode官网插件商店(https://marketpl ...

  5. BUUCTF-writeup

    Reverse RSA 使用openssl模块 rsa -pubin -text -modulus -in pub.key得到n值,在 factordb.com上分解大素数得到p,q值,脚本生成pri ...

  6. 虚拟化技术实现 — QEMU-KVM

    目录 文章目录 目录 前文列表 KVM QEMU QEMU-KVM QEMU-KVM 调用 KVM 内核模块启动虚拟机的流程概要 前文列表 <虚拟化技术实现 - 虚拟化技术发展编年史> K ...

  7. @Value()读取配置文件属性,读出值为null的问题

    一.问题描述 自定义一个Filter如下: @Component public class JwtFilter extends GenericFilterBean{ @Value("${jw ...

  8. thinkphp5 更改入口文件在主目录

    默认thinkPHP入口文件在public/index.php,而在虚拟主机部署时,不能设置访问路径,因此需要将入口文件放置在主目录上. 一.主目录下新建index.php 复制以下内容 // 定义应 ...

  9. MySQL网页端在线查询工具

    现在许多应用都移到云服务器上面了,数据库的远程维护.监控成为一大问题,通过TreeSoft数据库管理系统,可以方便的使用浏览器,通过网页操作的方式管理MySQL,Oracle,DB2,PostgreS ...

  10. 分析UIS-RNN源代码的代码规范和风格

    结合工程实践选题相关的一套源代码,根据其编程语言或项目特点,分析其在源代码目录结构.文件名/类名/函数名/变量名等命名.接口定义规范和单元测试组织形式等方面的做法和特点: 列举哪些做法符合代码规范和风 ...