Two Sum 解答
Question:
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
Solution:
1. O(n2) runtime, O(1) space – Brute force:
The brute force approach is simple. Loop through each element x and find if there is another value that equals to target – x. As finding another value requires looping through the rest of array, its runtime complexity is O(n2).
2. O(n) runtime, O(n) space – Hash table:
We could reduce the runtime complexity of looking up a value to O(1) using a hash map that maps a value to its index.
public class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
int[] result = new int[2];
int length = nums.length;
for (int i = 0; i < length; i++) {
int left = target - nums[i];
if (hm.get(left) != null) {
result[0] = hm.get(left) + 1;
result[1] = i + 1;
} else {
hm.put(nums[i], i);
}
}
return result;
}
}
3. O(nlogn) runtime, O(1) space - Binary search
Similar as solution 1, but use binary search to find index2.
4. O(nlogn) runtime, O(1) space - Two pointers
First, sort the array in ascending order vwhich uses O(nlogn).
Then, right pointer starts from biggest number and left pointer starts from smallest number. If two sum is greater than the target number, move the right pointer. If two sum is smaller than the target number, move the left pointer.
Two Sum 解答的更多相关文章
- 3 Sum 解答
Question Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fi ...
- Combination Sum 解答
Question Given a set of candidate numbers (C) and a target number (T), find all unique combinations ...
- Path Sum 解答
Question Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- Minimum Size Subarray Sum 解答
Question Given an array of n positive integers and a positive integer s, find the minimal length of ...
- C 2015年真题
1.写出程序输出结果 void main() { char p[10]="abc"; char q[]="xyz"; int i,j; i=0; while(* ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Sum Root to Leaf Numbers 解答
Question Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent ...
- 3 Sum Closest 解答
Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- Combination Sum II 解答
Question Given a collection of candidate numbers (C) and a target number (T), find all unique combin ...
随机推荐
- Ubuntu下Qt项目的部署
部署涉及到以下内容: 1. 程序执行文件: 2. 动态链接库: 3. Qt的一些插件(plugins),例如图片插件(imageformats),数据库插件(sqldrivers): 4. 其他资源文 ...
- Android滚动截屏,ScrollView截屏
在做分享功能的时候,需要截取全屏内容,一屏展示不完的内容,一般我们会用到 ListView 或 ScrollView 一: 普通截屏的实现 获取当前Window 的 DrawingCache 的方式, ...
- My.Ioc 代码示例——Lifetime 和 ILifetimeScope
很多 Ioc 框架在创建对象的过程中,都会采取某种方式来缓存/复用/释放已构建的对象.在 My.Ioc 中,这个目的是通过 Lifetime/ILifetimeScope 来实现的.其中,Lifeti ...
- Ext4 简单的treepanel
转载:http://blog.csdn.net/zyujie/article/details/8208499 最近在学习Ext4,记录一些有关Ext4实现控件的方法: Ext4的treePanel和之 ...
- 你好,C++(5)如何输出数据到屏幕、从屏幕输入数据与读写文件?
2.2 基本输入/输出流 听过HelloWorld.exe的自我介绍之后,大家已经知道了一个C++程序的任务就是描述数据和处理数据.这两大任务的对象都是数据,可现在的问题是,数据不可能无中生有地产生 ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- html form <label>标签基础语法结构与使用案例教程(转载)
在表单布局中会遇到label标签的使用,label没有任何样式效果,有触发对应表单控件功能.比如我们点击单选按钮或多选框前文字对应选项就能被选中,这个就是对文字加了<label>标签实现. ...
- Spring4.0学习笔记(6) —— 通过工厂方法配置Bean
1.静态工厂方法: bean package com.spring.factory; public class Car { public Car(String brand) { this.brand ...
- 安装vs2013 Sqlserver 无法连接远程服务器的解决方法
以“管理员身份”启动cmd,执行“netsh winsock reset”命令.
- Yii2的相关学习记录,自定义gii模板和引用vendor中的js、css(四)
上文中后台模板框架已经搭建起来了,但还是有些不协调,像是有两个User标题,或者我们想自己在gii生成时添加或删除些公用的东西.这就需要我们定义自己的gii模板. 我们以CRUD的模板为例,默认的gi ...