leetcode 1 两数之和 hashmap
主要是hashmap。还有边插入边查找,提高效率和降低空间复杂度。
之前一直用map,结果发现还有hashmap,效率更高。
注意名称空间为
using namespace __gnu_cxx;
问题在于hash_map目前并没有纳入C++ 标准模板库中,在跨平台使用时就可能会出现问题,
但几乎每个版本的STL都提供了相应的实现。
头文件<hash_map>
另外map插入数据时有几种方法。我习惯用make_pair
常用
- map<int, string> mapStudent;
- mapStudent.insert(pair<int, string>(1, "student_one"));
- map<int, string> mapStudent;
- mapStudent.insert(map<int, string>::value_type (1, "student_one"));
- map<int, string> mapStudent;
- mapStudent.insert(make_pair(1, "student_one"));
- mapStudent.insert(make_pair(2, "student_two"));
#include<hash_map>
#include<vector> using namespace __gnu_cxx; class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
/* vector<int> b;
for(int i=0;i<nums.size();i++)
mmap.insert(make_pair(nums[i],i));
for(int i=0;i<nums.size();i++)
{
if((mmap.count(target-nums[i]))&&(mmap[target-nums[i]]!=i))
{
b.push_back(mmap[target-nums[i]]);
b.push_back(i);
break;
}
}*/
vector<int> b;
hash_map<int,int> mmap;
for(int i=0;i<nums.size();i++)
{
mmap.insert(make_pair(nums[i],i));
if((mmap.count(target-nums[i]))&&(mmap[target-nums[i]]!=i))
{
b.push_back(mmap[target-nums[i]]);
b.push_back(i);
break;
}
} return b;
};
};
leetcode 1 两数之和 hashmap的更多相关文章
- 前端与算法 leetcode 1. 两数之和
目录 # 前端与算法 leetcode 1. 两数之和 题目描述 概要 提示 解析 解法一:暴力法 解法二:HashMap法 算法 传入[1, 2], [11, 1, 2, 3, 2]的运行结果 执行 ...
- LeetCode:两数之和、三数之和、四数之和
LeetCode:两数之和.三数之和.四数之和 多数之和问题,利用哈希集合减少时间复杂度以及多指针收缩窗口的巧妙解法 No.1 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在 ...
- leetCode:twoSum 两数之和 【JAVA实现】
LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...
- Leetcode 001. 两数之和(扩展)
1.题目要求 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 2.解法一:暴力法(for*for,O(n*n)) ...
- LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)
653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...
- Leetcode 1. 两数之和 (Python版)
有粉丝说我一个学算法的不去做Leetcode是不是浪费,于是今天闲来没事想尝试一下Leetcode,结果果断翻车,第一题没看懂,一直当我看到所有答案的开头都一样的时候,我意识到了我是个铁憨憨,人家是让 ...
- 每天一道面试题LeetCode 01 -- 两数之和
Two Sum 两数之和 Given an array of integers, find two numbers such that they add up to a specific target ...
- Leetcode 167. 两数之和 II - 输入有序数组 By Python
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...
- LeetCode 167. 两数之和 II - 输入有序数组
题目: 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的 ...
随机推荐
- 阿里云VOD(二)
一.准备工作 1.设置不转码 测试之前设置默认"不转码",以节省开发成本 2.找到子账户的AccessKey ID 3.给子账户添加授权 AliyunVODFullAccess 4 ...
- Py编程方法,尾递归优化,map函数,filter函数,reduce函数
函数式编程 1.面向过程 把大的问题分解成流程,按照流程来编写过程 2.面向函数 面向函数编程=编程语言定义的函数+数学意义上的函数先弄出数学意义上的方程式,再用编程方法编写这个数学方程式注意面向函数 ...
- SQL Server 日志收缩方法
在日常运维中,有时会遇到"The transaction log for database 'xxxx' is full due to 'ACTIVE_TRANSACTION'." ...
- ORB-SLAM2-tracking线程
tracking线程 Tracking线程的主要工作是从图像中提取ORB特征,根据上一帧进行姿态估计或者进行通过全局重定位初始化位姿,然后跟踪已经重建的局部地图,优化位姿,再根据一些规则确定新的关键帧 ...
- ant design vue 地区选择(级联)
city.js const options = [ { value:'北京市', label:'北京市', children:[ { value:'北京市', label:'北京市', childre ...
- MySQL主从配置This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL '' first.
MySQL主从配置This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD ...
- (Sqlserver)sql求连续问题
题目一:create table etltable( name varchar(20) , seq int, money int); create table etltarget ( name var ...
- springBoot controller入参LocalDateTime
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss") @DateTimeForma ...
- LOJ10013曲线
题目描述 明明做作业的时候遇到了n 个二次函数s_i(x)=ax^2+bx+c ,他突发奇想设计了一个新的函数 f(x)=max{s_i(x)},i=1,2,...,n. 明明现在想求这个函数在 [ ...
- EasyExcel导出小结:动态标题、标题格式、相同值合并
1. 实列相关依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel& ...