Leetcode 1. Two Sum(hash+stl)
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> tm;
int a = , b = ;
int len = nums.size(); multimap<int,int> hash; for(int i = ; i < len; i++){
hash.insert(pair<int,int>(nums[i],i));
}
for(int i = ; i < len; i++){
multimap<int,int>::iterator s;
s=hash.find(target-nums[i]);
if(s== hash.end()){
continue;
}
if(i != (*s).second){
a = i;
b = (*s).second;
break;
}
}
tm.push_back(a);
tm.push_back(b);
return tm;
}
};
Leetcode 1. Two Sum(hash+stl)的更多相关文章
- LeetCode 1. Two Sum (c++ stl map)
题目:https://leetcode.com/problems/two-sum/description/ stl map代码: class Solution { public: vector< ...
- LeetCode 437. Path Sum III (STL map前缀和)
找遍所有路径,特判以根为起点的串即可. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
随机推荐
- 机器学习实战-Logistics回归
Logistics回归:实战,有两个特征X0,X1.100个样本,进行Logistics回归 1.导入数据 def load_data_set(): """ 加载数据集 ...
- 机器学习实战-K-近邻算法(kNN)
k-近邻算法(kNN)---它的工作原理是:存在一个样本数据集合,也称做训练样本集,并且样本集中每个数据都存在标签,即我们知道样本集中每个数据与所属分类的对应关系.输入没有标签的新数据后,将新数据的每 ...
- 社工 - By源码托管平台|云盘 - GitHub - 汇总
搜索规则 ()默认搜索是从master分支搜索代码 ()只有小于384k的代码才是可以搜索到的 ()搜索语句不能有特殊字符如. , : ; / \ ` ' " = * ! ? # $ &am ...
- Centos 安装Jenkins的坎坷
安装Jenkins(简单复制即可) wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.rep ...
- 为企业服务器配置RAID0、raid1、 raid10、raid5、raid6、等常见RAID
RAID卡操作手册先从开机启动时如何进入管理界面开始介绍: 1)当机器开启后,显示器出现阵列卡检测信息时,会提示用户是否要进入管理界面对阵列卡进行操作,此时按下Ctrl + H 即可,如下图 2)按下 ...
- c++ 取整:四舍五入 向上取整 向下取整
对于数据的取整是经常需要考虑的 现在总结如下 #include<iostream> #include<cmath> using namespace std; int main( ...
- 终于有人把 Docker 讲清楚了,万字详解!
一.简介 1.了解Docker的前生LXC LXC为Linux Container的简写.可以提供轻量级的虚拟化,以便隔离进程和资源,而且不需要提供指令解释机制以及全虚拟化的其他复杂性.相当于C++中 ...
- Linux环境安装mongodb
介绍 上篇介绍了Linux环境下安装Node.js的步骤,紧接着来安装mongodb.另外,推荐我的另一篇 Windows下图文详解Mongodb安装及配置,先在Windows下熟悉下mongodb, ...
- [LeetCode] 135. 分发糖果
题目链接 : https://leetcode-cn.com/problems/candy/ 题目描述: 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分 ...
- UESTC-1059 秋实大哥与小朋友(离散化+线段树)
秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...