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 ...
随机推荐
- 10.1 ifconfig:配置或显示网络接口信息
ifconfig命令 用于配置网卡IP地址等网络参数或显示当前网络的接口状态,其类似于Windows下的ipconfig命令,这两个命令很容易混淆,读者需要区分一下.此外,ifconfig命令在配置网 ...
- shell字符截取
shell字符截取 截取文本中以vm开头的单词 grep -o vm.* text | cut -d' ' -f1 截取活动主机
- docker中ubuntu源更新慢加速 换为国内源 Debian10源
本来以为是Ubuntu打包的镜像,换了阿里源老是报错100公钥不可用,结果发现是Debian的操作系统,换位Debian的操作系统打包的,换位Debian的源即可 #源如果使用错误也会报错,没有Deb ...
- 做个地道的c++程序猿:copy and swap惯用法
如果你对外语感兴趣,那肯定听过"idiom"这个词.牛津词典对于它的解释叫惯用语,再精简一些可以叫"成语".想要掌握一门语言,其中的"成语" ...
- Sqlflow 之隐私政策(Privacy plolicy)介绍
在大数据技术流行的今天,SQLFlow 可以通过分析各种数据库对象的定义给开发和管理者带来很大的助力.能够让您在大数据时代应对自如,如虎添翼. 在之前的文章中我们已经详细介绍过SQLFlow是什么.能 ...
- harbor搭建及使用
harbor搭建及使用 1 系统及软件版本 1.1 系统版本 # uname -a Linux localhost.localdomain 3.10.0-862.el7.x86_64 #1 SMP ...
- 安装 error: Microsoft Visual C++ 14.0 is required 解决方案
最近写的项目需要用到Python的第三方库,比如:mmh3, pyshark等等,而直接pip install ... 会报错:error: Microsoft Visual C++ 14.0 is ...
- 最小高度树Java版本(力扣)
最小高度树 给定一个有序整数数组,元素各不相同且按升序排列,编写一个算法,创建一棵高度最小的二叉搜索树. 示例:给定有序数组: [-10,-3,0,5,9],一个可能的答案是:[0,-3,9,-10, ...
- springmvc——自定义类型转换器
一.什么是springmvc类型转换器? 在我们的ssm框架中,前端传递过来的参数都是字符串,在controller层接收参数的时候springmvc能够帮我们将大部分字符串类型的参数自动转换为我们指 ...
- 【dog与lxy】8.25题解-necklace
necklace 题目描述 可怜的dog最终还是难逃厄运,被迫于lxy签下城下之约.这时候lxy开始刁难dog. Lxy首先向dog炫耀起了自己的财富,他拿出了一段很长的项链.这个项链由n个珠子按顺序 ...