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 ...
随机推荐
- VNC配置
简介 VNC (Virtual Network Console)是虚拟网络控制台的缩写.它 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的.VNC 是在基于 UNIX ...
- Effective Java 之-----for-each循环优于传统的for循环
如下代码: enum Face {1,2,3,4,5,6}: ...... Collection<Face> faces = Array.asList(Face.values); for( ...
- 03_Linux文件和目录
一.Linux目录结构 /:根目录,一般根目录下只存放目录,在Linux下有且只有一个根目录.所有的东西都是从这里开始.当你在终端里输入"/home",你其实是在告诉电脑,先从/( ...
- centos 编译安装net-snmp 5.6.2
1.准备环境 yum -y install make gcc gcc-c++ gcc-g77 openssl openssl-devel 常用lib安装可参照本文 2.编译和安装 首先我们需要下载Ne ...
- CF 375D. Tree and Queries【莫队 | dsu on tree】
题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...
- Vue的生命周期
1.1.实例生命周期 每个 Vue 实例在被创建之前都要经过一系列的初始化过程.例如需要设置数据监听.编译模板.挂载实例到 DOM.在数据变化时更新 DOM 等.同时在这个过程中也会运行一些叫做生命周 ...
- 浅析Xilinx 三速以太网MAC IP核
之前在使用Altera的三速以太网MAC IP的基础上,完成了UDP协议数据传输.此次为了将设计移植到xilinx FPGA上,需要用到xilinx的三速以太网MAC IP核,当然也可以自己用HDL编 ...
- 异步请求时有时会让js不起作用,那么重新加载js
function reloadSmartMenu() { var jsElem = document.createElement('script'); jsElem.src= path+'/syste ...
- Maven文件配置
Maven文件路径的配置 默认设置 修改之后的设置 Maven文件内容的配置 对于Maven 的 settings.xml 文件,需要注意. <mirror>镜像元素之间是互斥的,优先级是 ...
- Git 如何 clone 非 master 分支的代码
问题描述 我们每次使用命令 git clone git@gitlab.xxx.com:xxxxx.git 默认 clone 的是这个仓库的 master 分支.如果最新的代码不在 master 分支上 ...