学习使用标准模板库(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. 使用WebStorm报错 Namespace 'v-bind' is not bound

    一:报错描述:                Namespace 'v-bind' is not bound.Namespace 'v-on' is not bound 等 二:问题说明:      ...

  2. 后台List里的数据传到前台表格和下拉列表为什么不显示

    传到前台表格和下拉列表我是用<c:forEach做的,百度了很久,仔仔细细对我的代码没有问题,那么到底是为什么不显示呢?! 找了很久啊,最后发现是我没有引入jstl的标签库!居然是因为这个…… ...

  3. python爬虫初级--获取指定页面上的菜单名称以及链接,然后导出

    ''' Created on 2017年4月5日 @author: Admin ''' import requests from bs4 import BeautifulSoup as bsp # 网 ...

  4. mac eclipse maven -solved

    最近开始用mac,在开始之初体验到了mac系统的丝滑流畅,但也感受到重新开始学习一个平台的坡度. 最近学习maven,创建项目时总是报错,网上查阅到的资料很少.最后在settings.xml中添加了阿 ...

  5. 忽略SIGPIPE信号

    #include <stdlib.h> #include <sys/signal.h> void SetupSignal() { struct sigaction sa; // ...

  6. java排序 冒泡?+插入排序

    冒泡.public class insortSort { public static void main(String[] args) { int[] arr = {12, 3, 4, 55, 36, ...

  7. element-ui隐藏组件scrollbar的使用

    话不多说,直接上图 总结:el-scrollbar组件设置高度100%包裹住需要滚动的dom结构即可. 再例如: 至于配置props,参见源码https://github.com/ElemeFE/el ...

  8. 输入框VS软键盘

    最近在做一个h5的时候遇到的问题 我们都知道当页面上的有输入框被选中了,这个时候就回调出键盘用户可以输入.但是安卓手机在弹出键盘时页面的输入框也会被覆盖住: 以下为暂时的解决办法:(以下方法同时解决了 ...

  9. 设计简单的VB程序

    1.模拟对话程序 [程序源码] Option Explicit Private Sub Command1_Click() Text2.Text = "" Text1.Text = ...

  10. node.js一行一行的获取txt文件内容

    node.js一行一行获取text文件代码: const readline = require('readline');//Readline是Node.js里实现标准输入输出的封装好的模块,通过这个模 ...