1_Two Sum --LeetCode
原题如下:

思路:将nums放到一个map<int,int>中,其中,键是nums中元素,值对应其下标。然后遍历nums,取nums中一个值nums[i],接着用target减去它,最后再map中找差值map[num[i]]。如果发现差值,则返回i,map[num[i]]。
代码如下:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> vec;
map<int,int> im;
//将nums装进map容器中,键为vector元素,值为数组下标
for(int i = ;i<nums.size();i++)
{
im[nums[i]]=i;
}
map<int,int>::iterator it;
//遍历vector作差,然后再map中寻找差值,如果命中,则记录下标
for(int i = ;i<nums.size();i++)
{
int sub = target - nums[i];
it = im.find(sub);
if(it != im.end() && it->second != i)
{
vec.push_back(i);
vec.push_back(it->second);
break;
}
}
return vec;
}
1_Two Sum --LeetCode的更多相关文章
- Path Sum [LeetCode]
Problem Description: http://oj.leetcode.com/problems/path-sum/ Pretty easy. /** * Definition for bin ...
- 1_Two Sum
1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...
- 39. Combination Sum - LeetCode
Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...
- Minimum Path Sum [LeetCode]
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Nested List Weight Sum -- LeetCode 339
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- Combination Sum [LeetCode]
Problem Description: http://oj.leetcode.com/problems/combination-sum/ Basic idea: It seems complicat ...
- two Sum ---- LeetCode 001
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- Minimum Size Subarray Sum -- leetcode
题目描写叙述: Given an array of n positive integers and a positive integer s, find the minimal length of a ...
- Minimum Size Subarray Sum —— LeetCode
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
随机推荐
- Struts2是什么?
Struts2是什么: Struts2是整合了struts1和webwork的技术优点的使用广泛的MVC框架: Struts2的特点: 1.基于MVC框架,结构清晰,便于开发人员掌控开发流程: 2.使 ...
- HTTP就是这么简单
为什么要学HTTP? 我们绝大多数的Web应用都是基于HTTP来进行开发的.我们对Web的操作都是通过HTTP协议来进行传输数据的. 简单来说,HTTP协议就是客户端和服务器交互的一种通迅的格式. H ...
- Linux history命令
history命令主要用于显示历史命令, 重新执行历史命令. Linux系统当你在shell(控制台)中输入并执行命令时,shell会自动把你的命令记录到历史列表中,一般保存在用户目录下的.bash_ ...
- 使Tomcat指向指定的JDK目录
1,修改bin文件夹下面的catalina.bat文件,把如下内容 rem ----- Execute The Requested Command -------------------------- ...
- js中如何处理大量有规律的变量
var a1=document.getElementById('a1'); var a1=document.getElementById('a2'); var a1=document.getEleme ...
- k短路模板 POJ2449
采用A*算法的k短路模板 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- POJ 1755 Triathlon [半平面交 线性规划]
Triathlon Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6912 Accepted: 1790 Descrip ...
- 前端系列之CSS基础知识概述
1.什么是DIV (1).div就是html一个普通标签,进行区域划分.特性:独自占一行.独自不能实现复杂效果.必须结合css样式进行渲染. (2).div通常其是块级元素 (3).div是定义文档中 ...
- 阿里云学习之IOT物联网套件(配置篇)
文档时间:2018.-1-24 首注:此文章是参照以下文章的整合与补充: https://bbs.aliyun.com/read/309106.html?amp;displayMode=1&p ...
- CentOS安装EPEL Remi
EPEL,Remi 因为Centos官方源很多软件都没有,编译安装又比较麻烦,可以安装EPEL源Remi源解决此问题. CentOS 5.x : wget http://dl.fedoraprojec ...