学习使用标准模板库(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. URL和URL比较

    浅谈URI和URL URI(Uniform Resource Identifier)字面上的意思是,统一资源标示符 URL(Uniform Resource Locator),统一资源定位符 光从字面 ...

  2. 解决Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain value for com.geek.dao.ContentDao.Integer

    mybatis报错:Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain val ...

  3. 360技术笔试编程题 - 无意间看到这么个东西,闲来无事用JS写了一下

    题目描述 为考验各自的数学能力,小B和小A经常在一起玩各种数值游戏,这一次他们又有了一种新玩法.每人从指定的数值范围中各自选择一个整数,记小A选择的数值为a,小B选择的数值为b.他们用一个均匀分布的随 ...

  4. 使用__slots__ __str__ __iter__

    __slots__ 为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class实例能添加的属性. __str__  用这个命令定义方法,可以返 ...

  5. JAVA005-基本数据类型变量的存储

    1.变量按类型分: 数据类型分为基本数据类型和引用数据类型 引用数据类型包括数组和类等 类定义的变量又叫对象 2.按照作用范围分为: 类级别.对象实例级.方法级.块级 方法级:局部变量 3.字符型的字 ...

  6. HTML下标签之应用

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  7. JAVA第十次作业

    JAVA第十次作业 (一)学习总结 1.用思维导图对java多线程的学习内容进行总结. 参考资料: XMind. 2.下面是一个单线程实现的龟兔赛跑游戏. public class TortoiseH ...

  8. SQL Server中的连接查询(内连接、外连接、交叉连接)

    在数据库查询中,经常会用到两个有关联的表进行查询,需要把两个表中的数据按照某些条件查出来,这时就可以使用连接查询 连接查询分为三种:内连接.外连接和交叉连接 1. 内连接 内连接inner join ...

  9. vue 数字随机滚动(数字递增)

    html: <span v-for="i in numArr">{{i}}</span>   data: numArr: [], methods: perN ...

  10. 51Nod - 1433 0和5 找规律

    小K手中有n张牌,每张牌上有一个一位数的数,这个字数不是0就是5.小K从这些牌在抽出任意张(不能抽0张),排成一行这样就组成了一个数.使得这个数尽可能大,而且可以被90整除. 注意: 1.这个数没有前 ...