1. 题目

1.1 英文题目

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

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

You can return the answer in any order.

1.2 中文题目

给定一个整数数组nums,返回数组中“和是某个给定值target”的两个数的下标。假设对于每次输入有且只有一个解。

1.3输入输出

输入 输出
nums = [2,7,11,15], target = 9 [0,1]
nums = [3,2,4], target = 6 [1,2]
nums = [3,3], target = 6 [0,1]

2. 实验平台

IDE:VS2019

IDE版本:16.10.1

语言:c++

3. 代码

3.1 功能程序

#pragma once
#include<vector>
#include<map>
using namespace std; //主功能
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target)
{
map<int, int> hashMap; // 声明哈希表,存储已经遍历过的nums,其中nums的元素为key,元素的索引为value
vector<int> ans; // 存储结果
for (int i = 0; i < nums.size(); i++) // 遍历nums
{
int temp = target - nums[i]; // 定义target与nums[i]的差值
if (hashMap.count(temp)) // 向前搜索是否有与差值相同的元素,若有
{
ans = { hashMap[temp], i }; // 给出结果
}
hashMap[nums[i]] = i; // 将遍历过的nums[i]加入到哈希表hashMap中
}
return ans; // 返回结果
}
};

3.2 测试程序

#include "Solution.h"
#include <vector>
#include<iostream>
using namespace std; // 主程序
void main()
{
vector<int> nums = { 1,1,2,7,4,2,5 };
int target = 4; // 定义输入
Solution solution; // 实例化Solution
vector<int> result = solution.twoSum(nums, target); // 主算法
cout << "[" << result[0] << "," << result[1] << "]" << endl; // 输出结果
}

4. 代码思路

构建哈希表,每次循环搜索时,都从搜索当前位置到前面几个元素进行查找

5. 注意事项

哈希表会对相同的key值的value进行覆盖处理,这一点需要加以注意。

Leetcode No.1 Two Sum(c++哈希表实现)的更多相关文章

  1. 《LeetBook》LeetCode题解(1) : Two Sum[E]——哈希Map的应用

    001.Two Sum[E] Two SumE 题目 思路 1双重循环 2 排序 3 Hashmap 1.题目 Given an array of integers, return indices o ...

  2. 数据结构中的哈希表(java实现)利用哈希表实现学生信息的存储

    哈希表 解释 哈希表是一种根据关键码去寻找值的数据映射结构,该结构通过把关键码映射的位置去寻找存放值的地方 内存结构分析图 1.定义一个类为结点,存储的信息 2.定义链表的相关操作 3.定义一个数组存 ...

  3. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  4. leetcode 练习1 two sum

    leetcode 练习1  two sum whowhoha@outlook.com 问题描述 Given an array of integers, return indices of the tw ...

  5. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  6. 【一天一道LeetCode】#113. Path Sum II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  8. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  9. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

随机推荐

  1. 10.11 arping:发送arp请求

    arping命令 是用于发送arp请求到一个相邻主机的工具,arping使用arp数据包检查局域网内所有设备的硬件地址.  arping 命令的参数选项及说明 -c <次数>       ...

  2. linux下 find命令使用

     按名称查找  find . -name filename [root@vps repo]# ls README.md vps.sh[root@vps repo]# find . -iname vps ...

  3. 解决1字节的UTF-8序列的字节1无效问题

    学习路上碰到了这个异常 解决方法如下: 1.手动将< ? xml version="1.0" encoding="UTF-8"?>中的UTF-8更改 ...

  4. MongoDB学习笔记:Python 操作MongoDB

    MongoDB学习笔记:Python 操作MongoDB   Pymongo 安装 安装pymongopip install pymongoPyMongo是驱动程序,使python程序能够使用Mong ...

  5. DLPack构建跨框架的深度学习编译器

    DLPack构建跨框架的深度学习编译器 Tensorflow,PyTorch和ApacheMxNet等深度学习框架提供了一个功能强大的工具包,可用于快速进行原型设计和部署深度学习模型.易用性通常是以碎 ...

  6. 【SQLite】教程05-SQLite创建数据库、附加、分离数据库

    创建数据库 .quit命令 退出sqlite 提示符 .quit .dump 命令 使用 SQLite .dump 点命令来导出完整的数据库在一个文本文件中,如下所示: sqlite3 Test.db ...

  7. 「题解」黑暗塔 wizard

    本文将同步发布于: 洛谷博客: csdn: 博客园: 简书. 题目 题意简述 给定 \(y\),求 \(\varphi(x)=y\) 中 \(x\) 的个数和最大值. \(1\leq y\leq 10 ...

  8. 使用mockjs模拟分页请求

    首先安装mockjs npm install mockjs --save-dev 创建mock.js //mock.js const Mock = require("mockjs" ...

  9. 题解 P1850 [NOIP2016 提高组] 换教室

    做完这道题才略微感觉自己懂了一点关于概率与期望的知识QAQ... 一:关于概率与期望的定义 转载节选于blog 1.什么是数学期望? 数学期望亦称期望.期望值等.在概率论和统计学中,一个离散型随机变量 ...

  10. csp-s模拟测试52-53

    留坑.... 改完题再说吧. 留坑....最近考得什么鬼??模拟53T1 u(差分) 一道差分题????然而考场没有想到如何维护斜率上的差分,事后经miemeng和cyf的生(xuan)动(xue)讲 ...