【Leecode】两数之和
学习使用标准模板库(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】两数之和的更多相关文章
- leecode刷题(8)-- 两数之和
leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输 ...
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- LeeCode:两数之和【1】
LeeCode:两数之和[1] 题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2 ...
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- 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 ...
- 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 ...
- [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 ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
随机推荐
- GSM:嗅探语音流量
GSM: Sniffing voice traffic I wrap up the GSM series with a walkthrough on how to decrypt voice traf ...
- nova98 假区域链 骗人项目(vexx.pro的前身)
首先,我是受害者. nova98前期是vexx.pro,前期推广送比特龙, 送3个,然后推广一个新人可以再拿到1.5个. 然后呢,现在就又推出一个新网站,nova98,把之前推广的人领到币全部清零,而 ...
- 普天同庆,微博开通,从今以后,努力用功! 狗屎一样的顺口溜!Q狗屎!!狗屎。。。。。 测试。。测试。。。没刷过微博。屯里来的。看看啥效果
普天同庆,微博开通,从今以后,努力用功! 狗屎一样的顺口溜!Q狗屎!!狗屎..... 测试..测试...没刷过微博.屯里来的.看看啥效果
- python学习第四次笔记
python学习第四次记录 列表list 列表可以存储不同数据类型,而且可以存储大量数据,python的限制是 536870912 个元素,64位python的限制是 1152921504606846 ...
- CSS常用属性-xy
一.文本Text CSS text-align 属性 文本对齐方式 CSS text-decoration 属性 text-decoration 属性规定添加到文本的修饰 CSS line-heigh ...
- Delphi7 在Windows 7上无法打开Help选项下帮助文档
发现无法打开Delphi7的帮助文档,百度了一下,问题已经解决. Delphi的帮助文档是*.hlp格式的,而Windows7已经不再带有WinHlp32程序,所以无法再打开此类格式. 此时你需要下载 ...
- 使用LSTM-RNN建立股票预测模型
硕士毕业之前曾经对基于LSTM循环神经网络的股价预测方法进行过小小的研究,趁着最近工作不忙,把其中的一部分内容写下来做以记录. 此次股票价格预测模型仅根据股票的历史数据来建立,不考虑消息面对个股的影响 ...
- MySQLdb模块(数据库)
安装 pip install mysqlclient 连接数据库 db = MySQLdb.connect(host="IP",port=端口,user="账号" ...
- Struts2配置。
** Web.xml配置** <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=&q ...
- iOS CATransition 动画的简单使用
下面是实现的代码 //选择动画 - (IBAction)selectAnimationTypeButton:(id)sender { UIButton *button = sender; animat ...