leetcode-1 Two Sum 找到数组中两数字和为指定和
问题描写叙述:在一个数组(无序)中高速找出两个数字,使得两个数字之和等于一个给定的值。如果数组中肯定存在至少一组满足要求。
《剑指Offer》P214(有序数组) 《编程之美》P176
Que:Given an array of integers, find twonumbers such that they add up to a specific target number.
The function twoSum should return indices ofthe two numbers such that they add up to the target, where index1 must be lessthan index2. Please note that your returned answers
(both index1 and index2)are not zero-based.
You may assume that each input would haveexactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
一、暴力穷举法 时间复杂度:O(N^2)
代码:
// 暴力解法。时间复杂度(O(n^2)),性能太差无法通过
public int[] twoSum1(int[] numbers, int target) {
if (numbers != null) {
int i, j = 0;
for (i = 0; i < numbers.length; i++) {
for (j = i + 1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] == target) {
int[] result = { ++i, ++j };
return result;
}
}
}
}
return null;
}
二、Hash表法,以空间换时间:时间复杂度:O(N)空间复杂度O(N)
更快的查找方法:hash表,给定一个数字,依据hash映射查找还有一个数字是否在数组中,仅仅需O(1)的时间,但须要承担O(N)的hash表存储空间。
C++能够使用STL中的hash_map,java使用HashMap
代码:
// 使用HashMap(查找的时间复杂度为O(1))
// 由题目如果知仅仅有一对数满足该情况,故每一个数都是唯一的,不存在重数的情况
public int[] twoSum2(int[] numbers, int target) {
if (numbers != null) {
// 由于Hashmap仅提供通过key获得value,故
// HashMap value放置与numers[index]匹配的数值,key放置index;,故
// 在以下循环时每一次查询map中的value是否有相等的值。有即相互匹配
// 其思想在于用index的value表示数组中的该数据,map中的key与之匹配,并在数组中寻找匹配值
HashMap<Integer, Integer> num_map = new HashMap<>();
for (int i = 0; i < numbers.length; i++) {
if (num_map.containsKey(numbers[i])) {
int index = num_map.get(numbers[i]);
int[] result = { ++index, ++i };
return result;
} else {
num_map.put(target - numbers[i], i);
}
}
}
return null;
}
三、非常easy能够想到先将查找的数组排序,然后用二分查找等方法进行查找。本题中能够直接对两个数字的和进行一个有序的遍历(当然和不用所有真正计算出来)。
1.首先对数组进行排序。时间复杂度为O(NlogN)
2.然后从i=0,j=end開始和末位的两个数字開始。计算两个之和sum。若sum大于目标值target,则须要一个较小的因子,j--;反之,i++;直至找到终于的结果。
代码:(这里没有返回相应原始位置。仅仅是返回了两个相应数据)
public int[] twoSum3(int[] numbers, int target) {
if (numbers != null) {
// 先进行排序,这里使用归并排序
new Merge_Sort().Merge_Sort(numbers, new int[numbers.length], 0,
numbers.length - 1);
// 实现该查找算法
int ahead = numbers.length - 1;
int behind = 0;
while (ahead > behind) {
// 注意result和要考虑两个较大int相加溢出的问题
long result = numbers[ahead] + numbers[behind]; if (result == target) {
int[] sum = { numbers[behind] , numbers[ahead] };
//假设要返回两个原始位置值,是否意味着还是又一次进行两次查询;
return sum;
} if (result < target) {
behind++;
} else {
ahead--;
}
}
} return null;
}
leetcode-1 Two Sum 找到数组中两数字和为指定和的更多相关文章
- Java 找到数组中两个元素相加等于指定数的所有组合
思路1:可以用hash表来存储数组中的元素,这样我们取得一个数后,去判断sum - val 在不在数组中,如果在数组中,则找到了一对二元组,它们的和为sum,该算法的缺点就是需要用到一个hash表,增 ...
- LeetCode 421. 数组中两个数的最大异或值(Maximum XOR of Two Numbers in an Array) 71
421. 数组中两个数的最大异或值 421. Maximum XOR of Two Numbers in an Array 题目描述 给定一个非空数组,数组中元素为 a0, a1, a2, - , a ...
- 2016网易实习生编程题:数组中两个数的和等于sum
题目 找出数组中两个数的和等于sum的这两个数 解题 这个题目做过很多次了,利用HashMap,key为 sum-A[i] value为 i 当 加入HashMap时候A[i] 已经存在map中,ge ...
- Leetcode 421.数组中两数的最大异或值
数组中两数的最大异或值 给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ...
- Java实现 LeetCode 421 数组中两个数的最大异或值
421. 数组中两个数的最大异或值 给定一个非空数组,数组中元素为 a0, a1, a2, - , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算 ...
- LeetCode 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- 求数组中两两相加等于20的组合(Python实现)
题目 求数组中两两相加等于20的组合. 例:给定一个数组[1, 7, 17, 2, 6, 3, 14],这个数组中满足条件的有两对:17+3=20, 6+14=20. 解析 分为两个步骤: 先采用堆排 ...
- 【ShareCode】不错的技术文章 -- 如何使用异或(XOR)运算找到数组中缺失的数?
如何使用异或(XOR)运算找到数组中缺失的数? 今天给大家分享一篇关于使用XOR(异或)运算找到数组中缺失的数的问题. 在一次Javascript面试中,有这么一个问题: 假设有一个由0到99(包含9 ...
随机推荐
- mysql 时间戳与日期格式的相互转换
1.UNIX时间戳转换为日期用函数: FROM_UNIXTIME() ); 输出:2006-08-22 12:11:10 2.日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP() Sel ...
- 从一个标准 url 里取出文件的扩展名
在php预定义函数中有一个叫做"pathinfo()"的函数,专门用于返回文件路径信息的. 那好,我们就来看一下它能为我们做些什么? 语法:pathinfo($url_ ...
- jq总结
总述 jQuery 框架提供了很多方法,但大致上可以分为3 大类: 获取jQuery 对象的方法 在jQuery 对象间跳转的方法 获取jQuery 对象后调用的方法 获取 jQuery 对象 是怎样 ...
- CTSC模拟题 树上的路径
Description 给定一棵\(N\)个结点的树,结点用正整数\(1 \dots N\)编号,每条边有一个正整数权值.用\(d(a, b)\)表示从结点\(a\)到结点\(b\)路径上经过边的权值 ...
- iOS便捷开发工具分享
项目/代码优化工具 1.objec_dep,可以了解项目中各个类的关联信息,了解项目中无效文件,知道双向应用的文件. 下载地址: https://github.com/nst/objc_dep 2.b ...
- 总结Web应用中基于浏览器的安全漏洞
1.浏览器缓存 每次打开一个网站,网页的内容会缓存到用户的机器中.如果这些内容在其他网页中需要重新加载,浏览器加载的是缓存,而不是再次下载内容.如果一些Web应用商店以及显示用户敏感信息(比 ...
- CyclicBarrier的介绍和使用
转自:http://www.itzhai.com/the-introduction-and-use-of-cyclicbarrier.html 类说明: 一个同步辅助类,它允许一组线程互相等待,直到到 ...
- elasticsearch spring 集成
elasticsearch spring 集成 项目清单 elasticsearch服务下载包括其中插件和分词 http://download.csdn.net/detail/u0142011 ...
- Git创建一个自己的本地仓库
如果我们要把一个项目加入到Git的版本管理中,可以在项目所在的目录用git init命令建立一个空的本地仓库,然后再用git add命令把它们都加入到Git本地仓库的暂存区(stage or inde ...
- leetcode面试准备: Jump Game
1 题目 Given an array of non-negative integers, you are initially positioned at the first index of the ...