【数组】Two Sum
题目:
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的更多相关文章
- [leetcode]304Range Sum Query 2D - Immutable动态规划计算二维数组中子数组的sum
303一维数组的升级版,方法就是用二维数组res存下从(0,0)到当前位置的sum,存的方法是动态规划,看着二维数组画圈比较好搞清楚其中的加减法 算子数组的sum的时候也是和存差不多的逻辑,就是某一部 ...
- [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 ...
- LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted
公众号: 爱写bug(ID:icodebugs) 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index ...
- 求一个数组的最大子数组(C/C++实现)
最大子数组:要求相连,加起来的和最大的子数组就是一个数组的最大子数组.编译环境:VS2012,顺便说句其实我是C#程序员,我只是喜欢学C++. 其实这是个半成品,还有些BUG在里面,不过总体的思路是这 ...
- 【C语言学习】《C Primer Plus》第10章 数组和指针
学习总结 1.数组初始化方式: int a[]={1,2,3} int a[SIZE]={1,2,3} //SIZE是宏定义,数组初始化个数不能大于SIZE,否则报错:当个数小 //SIZE,自动补0 ...
- javascript 红宝书笔记之如何使用对象 如何操作数组
对象定义 === 引用类型,描述的是一类对象所具有的属性和方法 新对象的创建 方法 new + 构造函数 var person = new Object(); 对象字面 ...
- Array 数组常用方法
(1)基本的数组方法 1.join() Array.join()方法将数组中所有元素都转化为字符串并连接在一起,返回最后生成的字符串.可以自己指定分隔的符号,如果不指定,默认使用逗号 var arr ...
- 【BZOJ】1818: [Cqoi2010]内部白点(树状数组+离散+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1818 这一题一开始我就看错了,bzoj的那个绝对值109简直坑人,应该是10^9,我直接写了个暴力. ...
- js数组基础知识链接
http://www.cnblogs.com/qiantuwuliang/archive/2011/01/08/1930499.html 小案例: <script language=&quo ...
- iOS开发环境C语言基础 数组 函数
1 求数组元素的最大值 1.1 问题 创建程序,实现查询数组中最大值的功能,需求为:创建一个长度为10的数组,数组内放置10个0~99之间(包含0,包含99)的随机数作为数组内容,要求查询出数组中的最 ...
随机推荐
- WPF 绑定备忘单
Part I – Common Examples Basic Binding {Binding} Bind to current DataContext. {Binding Name} Bi ...
- iOS7修改UISearchBar的Cancel按钮的颜色和文字
两行代码搞定: [[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor ...
- append2 给append 添加回调方法
$.fn.append2 = function(html, callback) { var originalHtmlLength = $("body").html().length ...
- DMV--sys.dm_os_ring_buffers
DMV 'sys.dm_os_ring_buffers' 可以用来诊断数据库连接和数据库内存方面的问题,但MSDN上找不到相应的介绍,网上找到以下相关资料: 1>sys.dm_os_ring_b ...
- WPF 组织机构下拉树多选,递归绑定方式现实
使用HierarchicalDataTemplate递归绑定现实 XAML代码: <UserControl x:Class="SunCreate.CombatPlatform.Clie ...
- pageadmin CMS网站建设教程:站点添加自定义字段
首先看看pagedmin默认的站点设置都有什么,如下图: 这里只有一些最基本的参数设置,用过3.0版本或用过其他公司开发的cms的用户应该有这种体验,在站点设置中可以设置logo图片,备案号,底部内容 ...
- .Net开发工程师笔试试题
第一部分[数据库技能] 附上自己做的答案,提出不足之处 现在有一个SQL Server 2000版本的数据库,里面包含有三个表Info.InfoReply.User,分别表示信息.信息评论和用户表,包 ...
- 20165219 Exp1 PC平台逆向破解
20165219 Exp1 PC平台逆向破解 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串 ...
- 【ocp-12c】最新Oracle OCP-071考试题库(44题)
44.(9-12)choose all that apply View the Exhibit and examine the details of the ORDER_ITEMS table. Ev ...
- 【文文殿下】 [SDOI2016]生成魔咒
字符集大小为1e9.............使用 map 吧 统计本质不同的子串个数是SAM的经典应用之一 本质不同的子串个数其实就是\(\sum max(x)-min(x)+1\) 所以我们新建结点 ...