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 ...
随机推荐
- Python+Selenium自动化-获取页面信息
Python+Selenium自动化-获取页面信息 1.获取页面title title:获取当前页面的标题显示的字段 from selenium import webdriver import t ...
- 3DPytorch-API NVIDIA Kaolin
3DPytorch-API NVIDIA Kaolin NVIDIA Kaolin library provides a PyTorch API for working with a variety ...
- 在cuDNN中简化Tensor Ops
在cuDNN中简化Tensor Ops 在Tesla V100 GPU中引入神经网络模型以来,神经网络模型已迅速利用NVIDIA Tensor Cores进行深度学习.例如,基于Tensor Core ...
- windows10下JDK9的环境配置
JDK版本:jdk-9.0.4_windows-x64_bin.exe windows版本:windows10 专业版 64位 需要在系统变量中新建如下3个变量: JAVA_HOME=jdk安装路径J ...
- H5简介(转)
H5究竟是什么? "HTML5(WEB前端)技术由HTML(结构).CSS(样式).JavaScript(行为)组成.HTML5是WEB的未来,HTML5不仅在PC端,更是在移动端上也有广泛 ...
- 二、创建测试JSP页面
root@web1 ~]# rm -rf /usr/local/tomcat/webapps/ROOT/* 1.创建测试网页 [root@web1 ROOT]# vim /usr/local/tomc ...
- Git 高级用法,喜欢就拿去用
如果你觉得 git 很迷惑人,那么这份小抄正是为你准备的! 请注意我有意跳过了 git commit.git pull/push 之类的基本命令,这份小抄的主题是 git 的一些「高级」用法. 导航 ...
- MindSpore自定义模型损失函数
技术背景 损失函数是机器学习中直接决定训练结果好坏的一个模块,该函数用于定义计算出来的结果或者是神经网络给出的推测结论与正确结果的偏差程度,偏差的越多,就表明对应的参数越差.而损失函数的另一个重要性在 ...
- 【题解】Luogu p3478 [POI2008]STA-Station 动态规划
题目描述 给出一个$N$个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 输入输出格式 输入格式 第一行一个数$n$,表示树上共有$n$个点接下来$n-1$行,表示$n-1$条边;每行 ...
- 不管卷不卷,面试还是得问问你G1原理!
所有的垃圾回收器的目的都是朝着减少STW的目的而前进,G1(Garbage First)回收器的出现颠覆了之前版本CMS.Parallel等垃圾回收器的分代收集方式,从2004年Sun发布第一篇关于G ...