【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,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
随机推荐
- makefile笔记3 - makefile规则
target ... : prerequisites ... command ... ... 规则包含两个部分,一个是依赖关系,一个是生成目标的方法. 在 Makefile 中,规则的顺序是很重要的, ...
- Navicat连接mysql出现1045错误
使用Navicat连接mysql出现1045,可能的原因为忘记密码,下面方法可以帮助重置密码. 1,以管理员权限运行cmd程序: 2,cd C:\Program Files (x86)\MySQL\M ...
- 安装mysql5.5遇到的狗屁问题,最后还是细心一下就好
首先巩固下自己已经遗忘了一年的Mysql数据库和navicat可视化数据库,安装数据库没有具体要求直接点下一步就好,我第一次安装提示服务器名无效,后来发现了原因,mysql服务压根没有启动,也就是更直 ...
- less &进行选择判断css的样式
先说&在less写 .parent{ .child{} &.and{} }在css就是 .parent.child{}//父子关系 .parent.and{}//并关系 用到这个方法是 ...
- snap7和plc的IP设置问题
设备ip必须在同一个网段,才能ping到 相关ip的链接https://blog.csdn.net/bytxl/article/details/41897287 在调试plc与树莓派的过程中,犯了一个 ...
- Sonar Java 规则插件开发 (基于阿里开发手册)
引言 最近在做Sonar静态代码扫描管理,以此顺手接了Sonar的插件开发,基于阿里开发手册进行开发,在整体开发过程中,其中还是遇到不少坑位,也以此给大家做相应借鉴官网Demo演示插件开发地址:htt ...
- 如何删除node_modules
1.$ npm install -g rimraf 执行此命令 2.$ rimraf node_modules
- Java 并发开发:Lock 框架详解
摘要: 我们已经知道,synchronized 是Java的关键字,是Java的内置特性,在JVM层面实现了对临界资源的同步互斥访问,但 synchronized 粒度有些大,在处理实际问题时存在诸多 ...
- CrawlSpider模板
crawlSpider 创建CrawlSpider模板 scrapy genspider -t crawl <爬虫名字> <域名> 模板代码示例: # -*- coding: ...
- promise在angular中的基本使用
promise在angular中的基本使用 <!DOCTYPE html> <html ng-app="myApp"> <head> <m ...