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.

Example:

Given nums = [, , , ], target = ,

Because nums[] + nums[] =  +  = ,
return [, ].

1/ 首先想到的是 通过两次循环去寻找 这两个数,找到后立刻返回。

但是当提交运行的时候,会报错,运行时间过长。

2/ 想到的另一种方法是,先通过排序(nlgn),然后通过两个指针去前后遍历数组(n)

3/ 最后一种方法在网上看到的,因为自己对hashmap并不是很熟悉。一下贴出网上的hashmap的代码

class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<int> res;
int length = numbers.size();
map<int,int> mp;
int find;
for(int i = ; i < length; ++i){
// if have target-numbers[i] return target-numbers[i] ,else create a target-numbers[i] element
find=mp[target - numbers[i]];
if( find ){
res.push_back(find);
res.push_back(i+);
break;
}
mp[numbers[i]] = i;
}
return res;
}
};

two sum - leetcode的更多相关文章

  1. Path Sum [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/path-sum/ Pretty easy. /** * Definition for bin ...

  2. 39. Combination Sum - LeetCode

    Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...

  3. 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 ...

  4. 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. ...

  5. Combination Sum [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/combination-sum/ Basic idea: It seems complicat ...

  6. two Sum ---- LeetCode 001

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  7. Minimum Size Subarray Sum -- leetcode

    题目描写叙述: Given an array of n positive integers and a positive integer s, find the minimal length of a ...

  8. Minimum Size Subarray Sum —— LeetCode

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  9. 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 ...

  10. 【LeetCode】Path Sum ---------LeetCode java 小结

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

随机推荐

  1. Laravel 5如何在中间件中获取路由参数?

    以官方文档中间件篇的年龄为例子进行了修改 路由部分 Route::get('test/age/{age}',[ 'middleware' => 'old', 'uses'=>'Test@t ...

  2. smartstore-net

    记录一下,抽空下载源码了研究下

  3. OWIN概述

    关于OWIN OWIN defines a standard interface between .NET web servers and web applications. The goal of ...

  4. Oracle之自动收集统计信息

    一.Oracle 11g 在Oracle的11g版本中提供了统计数据自动收集的功能.在部署安装11g Oracle软件过程中,其中有一个步骤便是提示是否启动这个功能(默认是启用这个功能). 在这里介绍 ...

  5. MySQL全文索引应用简明教程

    本文从以下几个方面介绍下MySQL全文索引的基础知识: MySQL全文索引的几个注意事项 全文索引的语法 几种搜索类型的简介 几种搜索类型的实例 全文索引的几个注意事项 搜索必须在类型为fulltex ...

  6. Android 文件访问权限的四种模式

    Linux文件的访问权限* 在Android中,每一个应用是一个独立的用户* drwxrwxrwx* 第1位:d表示文件夹,-表示文件* 第2-4位:rwx,表示这个文件的拥有者(创建这个文件的应用) ...

  7. Maven学习——安装与修改Maven的本地仓库路径

    一.Maven的下载安装配置 1.1.下载 官网 http://maven.apache.org/download.cgi 1.2.安装配置 apache-maven-3.3.3-bin.zip 解压 ...

  8. 真机调试出现Could not find Developer Disk Image问题解决办法

    1.升级Xcode 2. 在使用Xcode进行真机调试的时候,有时根据真机的系统不同,会出现could not find developer disk image 错误,这是由于真机系统过高或者过低, ...

  9. 原生JS获取各种高度宽度、浏览器窗口滚动条的位置、元素的几何尺寸名

    1)关于 pageX, clienX,offsetX,layerX pageX:鼠标在页面上的位置,从页面左上角开始,即是以页面为参考点,不随滑动条移动而变化 clientX:鼠标在页面上可视区域的位 ...

  10. CTF中那些脑洞大开的编码和加密

    0x00 前言 正文开始之前先闲扯几句吧,玩CTF的小伙伴也许会遇到类似这样的问题:表哥,你知道这是什么加密吗?其实CTF中脑洞密码题(非现代加密方式)一般都是各种古典密码的变形,一般出题者会对密文进 ...