学习使用标准模板库(STL)中的map,hash_map。涉及数据结构知识:哈希表,红黑树。

map的使用方法

https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html

hash_map的原理和使用方法

https://blog.csdn.net/ddkxddkx/article/details/6555754

两遍哈希表

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> index();
int num = nums.size();
map<int, int> nums_map;
for (int i = ; i < num; i++)
{
nums_map[nums[i]] = i;
} map<int, int>::iterator iter; // 查看是否存在相同“键”而覆盖的情况
for (iter = nums_map.begin(); iter != nums_map.end(); iter++)
{
cout << iter->first <<" " << iter->second<<endl;
} num = nums_map.size();
for(int j=; j<num; j++){
int complement = target - nums[j];
if (nums_map.count(complement) && nums_map.find(complement)->second != j){
index = {j, nums_map.find(complement)->second};
return index;
}
} return index;
}
};

一遍哈希表

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> index();
int num = nums.size();
map<int, int> nums_map;
int complement; for (int i = ; i<num; i++)
{
// nums_map[nums[i]] = i; 若放在这里,当出现nums = [3,3], tareget = 6时会出现报错
complement = target - nums[i];
if (nums_map.count(complement) && nums_map.find(complement)->second != i)
{
index ={i, nums_map.find(complement)->second};
return index;
}
nums_map[nums[i]] = i; } map<int, int>::iterator iter; // 查看是否存在相同“键”而覆盖的情况
for (iter = nums_map.begin(); iter != nums_map.end(); iter++)
{
cout << iter->first <<" " << iter->second<<endl;
} return index;
}
};

【Leecode】两数之和的更多相关文章

  1. leecode刷题(8)-- 两数之和

    leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输 ...

  2. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  3. LeeCode:两数之和【1】

    LeeCode:两数之和[1] 题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2 ...

  4. 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X

    题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...

  5. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  6. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  7. LeetCode 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 ...

  8. [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  9. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  10. Leetcode(一)两数之和

    1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...

随机推荐

  1. Big Txt File(一)

    对于当今的数据集来说,动不动就上G的大小,市面的软件大多不支持,所以需要自己写一个. 常见的txt文本行形式存储的时候也不过是行数多些而已,可以考虑只观测部分行的方式,基于这个思路可以搞一个大数据的浏 ...

  2. JavaScript里面的arguments到底是个啥?

    类数组对象:arguments 总所周知,js是一门相当灵活的语言.当我们在js中在调用一个函数的时候,我们经常会给这个函数传递一些参数,js把传入到这个函数的全部参数存储在一个叫做arguments ...

  3. dos2章

    讲FOR之前呢,咋先告诉各位新手朋友,如果你有什么命令不懂,直接在CMD下面输入: name /? 这样的格式来看系统给出的帮助文件,比如for /? 就会把FOR命令的帮助全部显示出来!当然许多菜鸟 ...

  4. 如何快速判断一个key是否存在在亿级数据中(bloomFilters)

    面试题 现在有一个非常庞大的数据(亿级),假设全是 int 类型.现在我给你一个数,你需要告诉我它是否存在其中(尽量高效) 分析 采用bloomFilters进行实现(时间&空间尽可能的有效) ...

  5. 在Raspbian Stretch系统上设置Home Assistant开机启动

    较新的Linux发行版趋向于用systemd管理守护进程,如果您不确定系统是否正在使用systemd,您可以使用以下命令进行检查: -o comm= 如果上述命令返回字符串systemd,说明系统正在 ...

  6. element 多个文件上传多次http请求解决方法

    第一步 action="#" 第二步 :auto-upload = "false" 第三步 给元素绑定click事件触发提交方法,注意,把其他没有用的文件都给去 ...

  7. DataGridView设置行高

    .Net中DataGridView控件如何设置行高 在DataGridView控件中,默认的行高很大,而标题头的行高却很小,感觉很不匀称. 标题头的行高比较好设置需要修改两个属性1修改ColumnHe ...

  8. 关于TCP和MQTT之间的转换(转载)

    现在物联网流行的就是MQTT 其实MQTT就是在TCP的基础上建立了一套协议 可以看这个,本来我自己想用Wireshark监听一下,不过百度一搜索一大把,我就不测试了 https://blog.csd ...

  9. React中使用echarts

    1.安装相关的依赖: cnpm i react-for-echarts -S cnpm i echarts -S 2.使用方法: 页面引入: import ReactEcharts from 'ech ...

  10. linux下磁盘管理(du、df)命令使用

    DF :disk free 磁盘可用量 DU: disk usage 磁盘使用 df:列出文件系统的整体磁盘使用量: df参数: -a:列出所有的文件系统,包括系统特有的/proc等文件系统 -k:以 ...