原题链接:http://oj.leetcode.com/problems/two-sum/
这是一道非常经典的题目,brute force时间复杂度为O(n^2), 对每一对pair两两比较。 优化的方法一般有两种,第一种是用哈希表,时间复杂度为O(n),同时空间复杂度也是O(n),代码如下:
public int[] twoSum(int[] numbers, int target) {
int[] res = new int[2];
if(numbers==null || numbers.length==0)
return null;
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i=0;i<numbers.length;i++)
{
if(map.containsKey(target-numbers[i]))
{
res[0]=map.get(target-numbers[i])+1;
res[1]=i+1;
return res;
}
map.put(numbers[i],i);
}
return null;
}
第二种解法是先对数组进行排序,然后使用夹逼的方法找出满足条件的pair,原理是因为数组是有序的,那么假设当前结果比target大,那么左端序号右移只会使两个数的和更大,反之亦然。所以每次只会有一个选择,从而实现线性就可以求出结果。该算法的时间复杂度是O(nlogn+n)=O(nlogn),空间复杂度取决于排序算法。代码如下:
public int[] twoSum(int[] numbers, int target) {
int[] res = new int[2];
if(numbers==null || numbers.length==0)
return null;
Arrays.sort(numbers);
int l = 0;
int r = numbers.length-1;
while(l<r)
{
if(numbers[l]+numbers[r]==target)
{
res[0] = number[l];
res[1] = number[r];
return res;
}
else if(numbers[l]+numbers[r]>target)
{
r--;
}
else
{
l++;
}
}
return null;
}
注意,在这里,输出结果改成了满足相加等于target的两个数,而不是他们的index。因为要排序,如果要输出index,需要对原来的数的index进行记录,方法是构造一个数据结构,包含数字的值和index,然后排序。所以从这个角度来看,这道题第二种解法并没有第一种解法好,空间也是O(n). 在LeetCode原题中是假设结果有且仅有一个的,一般来说面试时会要求出所有的结果,这个时候会涉及到重复pair的处理,相关的内容会在3Sum那道题目中涉及到,请参见3Sum
-- LeetCode.
分享到:
相关推荐
twoSum , cases : [ { in : [ [ 2 , 7 , 11 , 15 ] , 9 ] , out : [ 0 , 1 ] } , { in : [ [ 11 , 2 , 15 , 7 ] , 9 ] , out : [ 1 , 3 ] } , ] , } npm start 特征 忽视问题 如果导出ignore: true则可以忽略问题 ...
leetcode-cli 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的网站! ⦙⦙⦙⦙⦙⦙⦙⦙ 一个很打问题的方法。 问题来缓解离线思考。 编码前的源代码。 居住和与 leetcode.com。 下载你...
leetcode-问题-爬虫 目录由cd problems && npx leetcode-problems-crawler -r 1-10生成cd problems && npx leetcode-problems-crawler -r 1-10 用法 爬行问题1到5: $ npx leetcode-problem-crawler -r 1-5 爬行问题...
leetcode-cli 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的网站! ⦙⦙⦙⦙⦙⦙⦙⦙ 一个很打问题的方法。 问题来缓解离线思考。 编码前的源代码。 居住和与 leetcode.com。 下载你...
leetcode-训练 算法训练。 java $: javac hello.java java $: java hello c $: gcc hello.c 如果没有错误会生成a.out 可执行文件 c $: ./a.out leetcode cli leetcode version leetcode help leetcode help user ...
leetcode-machine-swift :SOS_button: 请帮助在Sources/leetcode-machine-swift/leetcode.swift设置所有 leetcode 问题。 :SOS_button: 在 swift 编码时有 xcode 总是好的。 利用自动补全、类型检查...功能可以帮助...
leetcode-1-Two-Sum 这是我在 Github 的第一天。 我只是 AC leetcode 中的第一个问题。 从现在开始,我将使用Github在leetcode中记录我的生活。 我擅长 C++,但对 java 和 Python 不是很专业,我将使用最流行的 3 种...
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...
https://leetcode-cn.com/problems/two-sum/ 难度 : 简单 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。...
leetcode-练习 算法实践 15. 3和 给定一个由 n 个整数组成的数组 nums,nums 中是否有元素 a、b、c 使得 a + b + c = 0? 在数组中找到所有唯一的三元组,其总和为零。 示例输入: [-1, 0, 1, 2, -1, -4] 示例输出:...
Leetcode two sum java 解法
leetcode167 LeetCode167_Two-Sum-II 给定一个目标target,再数组中找到两个数,使其和为target,并返回对应数组中索引(从1开始)
leetcode编辑器leetcode-问题 自定义代码演示 leetcode 编辑器配置 代码文件名: $ ! velocityTool . camelCaseName(${question . titleSlug}) 代码模板: ${question . content} package ...title ex:Two ...
窗口总和610.TwoSum-差等于目标228.链表中间464.排序整数II 5.Kth最大Elemenet 607.Two Sum III-数据结构设计539.移动零点608.二次加和II 102.链表周期103.LinkedList周期II 380.两个链表的交集148.颜色排序894.煎饼...
js代码-1. 两数之和 [简单] https://leetcode-cn.com/problems/two-sum
python python_leetcode面试题解之两数之和TwoSum
leetcode 接口 该项目可帮助您使用首选的 IDE 或带有命令行界面 (CLI) 的编辑器来执行 ...two-sum leetcode --name add-two-numbers leetcode --name median-of-two-sorted-arrays 然后问题描述和启动代码将自动
Two-Sum leetcode两数之和代码 题目描述:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组...
leetcode-solution (Hot 100) leetcode 1. 两数之和题解 /** * @param {number[]} nums * @param {number} target * @return {number[]} */ // 时间复杂度为O(n^2),空间复杂度为O(1). var twoSum = function(nums, ...
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)...