Leetcode No.1 Two Sum(c++哈希表实现)
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++哈希表实现)的更多相关文章
- 《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 ...
- 数据结构中的哈希表(java实现)利用哈希表实现学生信息的存储
哈希表 解释 哈希表是一种根据关键码去寻找值的数据映射结构,该结构通过把关键码映射的位置去寻找存放值的地方 内存结构分析图 1.定义一个类为结点,存储的信息 2.定义链表的相关操作 3.定义一个数组存 ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- leetcode 练习1 two sum
leetcode 练习1 two sum whowhoha@outlook.com 问题描述 Given an array of integers, return indices of the tw ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【一天一道LeetCode】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
随机推荐
- 修改mysql中数据库存储主路径
一.首先把mysql的服务先停掉. 二.更改MySQL配置文件My.ini中的数据库存储主路径 打开文件夹C:\ProgramData\MySQL\MySQL Server 5.7中的my.ini文件 ...
- Django(43)restful接口规范
restful接口规范 什么是接口规范?接口规范就是为了采用不同的后台语言,也能使用同样的接口获取到同样的数据.如何写接口:接口规范是规范化书写接口的,写接口要写url.响应数据 注:如果将 ...
- week-01
week-01 1. 计算机组成 从底层开始: 硬件: CPU.内存.硬盘.网卡.主板.显卡.风扇.电源.鼠标键盘 等: 系统: Linux.Windows.Mac 等: 软件: QQ.微信.吃鸡.农 ...
- GO语言面向对象05---接口的多态
package main import "fmt" type Fighter interface { Attack() (bloodloss int) Defend() } /*骑 ...
- Angel图算法
Angel图算法 [2.0]CommonFriends 计算两个好友的共同好友数,某种程度上可以刻画两个节点之间的紧密程度. 输入 输入数据路径:输入文件所在路径,无权网络数据, 数据格式为两列 sr ...
- TensorRT 数据格式说明
TensorRT数据格式说明 NVIDIA TensorRT支持不同的数据格式.需要考虑两个方面:数据类型和布局. 数据类型格式 数据类型是每个单独值的表示.它的大小决定了数值的范围和表示的精度:分 ...
- springcloud-config配置异常Cannot clone or checkout repository 和 Authentication is required but no CredentialsProvider has been registered解决过程
Cannot clone or checkout repository, 出现这个异常,通过检查是因为自己本地没有配置 ssh,所以配置了, https://blog.csdn.net/zy_2818 ...
- 【NX二次开发】Block UI 绘图区
属性说明 常规 类型 描述 BlockID String 控件ID Enable Logical 是否可操作 Group Logical ...
- 在H5页面播放m3u8音频文件
需要使用hls插件 首先安装依赖npm install hls.js --save <audio ref="audio"></audio> import H ...
- 面试官:MySQL的可重复读级别能解决幻读问题吗?
引言 之前在深入了解数据库理论的时候,了解到事务的不同隔离级别可能存在的问题.为了更好的理解所以在MySQL数据库中测试复现这些问题.关于脏读和不可重复读在相应的隔离级别下都很容易的复现了. 但是对于 ...