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 ... 
随机推荐
- js给页面添加滚动事件并判断滚动方向
			<script> var scrollFunc = function (e) { var direct = 0; e = e || window.event; if (e.wheelDel ... 
- awk数组结合+=统计题
			awk增加统计列值为增加列数或进行运行结果统计,使用符号 + =.增加的结果赋给符号左边变量值,增加到变量的域在符号右边.例如将 $ 1加入变量total,表达式为toatl+=$1.列值增加很有用. ... 
- linux之cut命令简单用法
			语法 cut [-bn] [file] cut [-c] [file] cut [-df] [file] 使用说明: cut 命令从文件的每一行剪切字节.字符和字段并将这些字节.字符和字段写至标准输出 ... 
- Css3中的 calc()使用
			http://www.w3cplus.com/css3/how-to-use-css3-calc-function.html 
- python _init_学习
			今天继续学习python,接触了_init_,感觉很好玩照着教程手写了一些代码,感觉编程语言是互通的,只是换个了形式来表达 #coding=utf-8#类似于java的构造器class Person: ... 
- Zookeeper启动时报8080端口被占用
			zookeeper启动时报8080 端口被占用,导致启动失败.特别是服务器上部署了tomcat服务时需要注意. 通过查看zookeeper的官方文档,发现有3种解决途径: (1)删除jetty. (2 ... 
- BZOJ 3572: [Hnoi2014]世界树 [虚树 DP 倍增]
			传送门 题意: 一棵树,多次询问,给出$m$个点,求有几个点到给定点最近 写了一晚上... 当然要建虚树了,但是怎么$DP$啊 大爷题解传送门 我们先求出到虚树上某个点最近的关键点 然后枚举所有的边$ ... 
- c# Nlog 非xml cs方法配置
			public static void InitLog(TargetWithLayout target = null, string level = "Debug", string ... 
- ------ 新春第一炮:阶乘算法性能分析与 double fault 蓝屏故障排查 Part I ------
			-------------------------------------------------------------------------- 春节期间闲来无事想研究下算法,上机测试代码却遇到了 ... 
- mysql必知必会
			春节放假没事,找了本电子书mysql必知必会敲了下.用的工具是有道笔记的markdown文档类型. 下面是根据大纲已经敲完的章节,可复制到有道笔记的查看,更美观. # 第一章 了解SQL## 什么是S ... 
