题目:

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

思路:

  首先对数组排序。不过由于最后返回两个数字的索引,所以需要事先对数据进行备份。然后采用2个指针l和r,分别从左端和右端向中间运动:当l和r位置的两个数字之和小于目标数字target时,r减1;当l和r位置的两个数字之和大于目标数字target时,l加1。因此只需扫描一遍数组就可以检索出两个数字了。最后再扫描一遍原数组,获取这两个数字的索引。

 var twoSum = function(nums, target) {
var temp=[];
var index=[];
for(var i=0;i<nums.length;i++){
temp[i]=nums[i];
}
temp.sort(function(a,b){return a-b;}); var l=0,r=temp.length-1; while(l<r){
if(temp[l]+temp[r]==target){
break;
}else if(temp[l]+temp[r]>target){
r--;
}else{
l++;
}
} for(var i=0,n=2;i<nums.length;i++){
if(nums[i]==temp[l]||nums[i]==temp[r]){
index.push(i+1);
--n;
if(n==0){
break;
}
}
} return index;
};

【数组】Two Sum的更多相关文章

  1. [leetcode]304Range Sum Query 2D - Immutable动态规划计算二维数组中子数组的sum

    303一维数组的升级版,方法就是用二维数组res存下从(0,0)到当前位置的sum,存的方法是动态规划,看着二维数组画圈比较好搞清楚其中的加减法 算子数组的sum的时候也是和存差不多的逻辑,就是某一部 ...

  2. [Swift]LeetCode167. 两数之和 II - 输入有序数组 | Two Sum II - Input array is sorted

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  3. LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted

    公众号: 爱写bug(ID:icodebugs) 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index ...

  4. 求一个数组的最大子数组(C/C++实现)

    最大子数组:要求相连,加起来的和最大的子数组就是一个数组的最大子数组.编译环境:VS2012,顺便说句其实我是C#程序员,我只是喜欢学C++. 其实这是个半成品,还有些BUG在里面,不过总体的思路是这 ...

  5. 【C语言学习】《C Primer Plus》第10章 数组和指针

    学习总结 1.数组初始化方式: int a[]={1,2,3} int a[SIZE]={1,2,3} //SIZE是宏定义,数组初始化个数不能大于SIZE,否则报错:当个数小 //SIZE,自动补0 ...

  6. javascript 红宝书笔记之如何使用对象 如何操作数组

    对象定义  ===  引用类型,描述的是一类对象所具有的属性和方法     新对象的创建 方法     new + 构造函数       var person = new Object(); 对象字面 ...

  7. Array 数组常用方法

    (1)基本的数组方法 1.join() Array.join()方法将数组中所有元素都转化为字符串并连接在一起,返回最后生成的字符串.可以自己指定分隔的符号,如果不指定,默认使用逗号 var arr ...

  8. 【BZOJ】1818: [Cqoi2010]内部白点(树状数组+离散+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1818 这一题一开始我就看错了,bzoj的那个绝对值109简直坑人,应该是10^9,我直接写了个暴力. ...

  9. js数组基础知识链接

    http://www.cnblogs.com/qiantuwuliang/archive/2011/01/08/1930499.html 小案例:   <script language=&quo ...

  10. iOS开发环境C语言基础 数组 函数

    1 求数组元素的最大值 1.1 问题 创建程序,实现查询数组中最大值的功能,需求为:创建一个长度为10的数组,数组内放置10个0~99之间(包含0,包含99)的随机数作为数组内容,要求查询出数组中的最 ...

随机推荐

  1. 跳转AppStore 评分

    -(void)goToAppStore { NSString *str = [NSString stringWithFormat: @"itms-apps://ax.itunes.apple ...

  2. VS2015安装失败

    [16D4:18C8][2017-06-24T13:44:01]e000: Error 0x80091007: Hash mismatch for path: D:\Visual Studio 201 ...

  3. B-spline Curves 学习之B样条基函数计算实例(3)

    B-spline Basis Functions: Computation Examples 本博客转自前人的博客的翻译版本,前几章节是原来博主的翻译内容,但是后续章节博主不在提供翻译,后续章节我在完 ...

  4. How do I determine if I'm being run under the debugger?

    #include <assert.h>#include <stdbool.h>#include <sys/types.h>#include <unistd.h ...

  5. TSQL--SQL SERVER 常用系统变量

    ----------全局变量select @@version as '版本';---------------------------返回当前数据库的版本信息 select APP_NAME ( ) a ...

  6. solrcloud 搭建资料

    SolrCloud4.9+zookeeper在CentOS上的搭建与安装 http://www.open-open.com/lib/view/open1411307048750.html 官网教程 h ...

  7. C#发送邮件及注意事项

    //参数配置 static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings[&qu ...

  8. SQL多行字符串按条件合并

    USE [ARTEA.MES]GO /****** Object: UserDefinedFunction [dbo].[UnionPart] Script Date: 11/18/2015 15:3 ...

  9. VS2015 IIS Express 无法启动 解决办法(转)

    因为安装各种乱七八糟的软件,然后不小心把IIS Express卸载掉了,网上下载了一个IIS Express 7,安装之后本地使用VS 2015无法启动调试,F5 无法启动IIS, 再次F5调试,没有 ...

  10. [uwp]数据绑定再学习

    在开始上代码前,先来假设这样一种情形: 出于某些原因,创建一个自定义控件(UserControl),然后为它定义一个依赖属性,这个属性有两个作用,一是调用控件方通过数据绑定技术为它赋值,二是控件内部的 ...