Leetcode:Two Sum分析和实现
问题表示提供一个整数数组nums,以及一个目标target,要找到两个下标i与j,使得nums[i] + nums[j] = target。
最简单的思路是两次循环:
for a in nums
for b in nums
if a + b = target then
return [a.index, b.index]
这样的代码的时间复杂度是O(n^2),其中n表示nums的长度。当n为1e4时,执行时间高达?e8,其中?是一个有上界的数值。
可以利用排序和二分查找优化这一过程:
sort(nums)
for a in nums
b = target - a
bIndex = binarySearch(nums, b)
if bIndex >= 0 then
return [a.index, bIndex]
这样代码的时间复杂度为排序时间复杂度+nx二分查找时间复杂度,这里采用归并排序来排序,因此结果为O(nlogn)+nxO(logn)=O(nlogn)。当n为1e4时,执行时间为?e5左右,其中?是一个有上界的数值。
最后贴上Java的完整实现代码给有需要的人:
package cn.dalt.leetcode; import java.util.Arrays; import java.util.Comparator; public class TwoSum { public static void main(String[] arg) { System.out.println(Arrays.toString( new TwoSum().twoSum(new int[]{3, 3}, 6) )); } static class Element { final int num; final int index; public Element(int num, int index) { this.num = num; this.index = index; } @Override public int hashCode() { return num; } @Override public boolean equals(Object obj) { return num == ((Element) obj).num; } } static final Comparator<Element> ELEMENT_COMPARATOR = new Comparator<Element>() { @Override public int compare(Element o1, Element o2) { return o1.num > o2.num ? 1 : o1.num < o2.num ? -1 : 0; } }; public int[] twoSum(int[] nums, int target) { Element[] elements = new Element[nums.length]; for (int i = 0, bound = nums.length; i < bound; i++) { elements[i] = new Element(nums[i], i); } //Sort the array with ascending order Arrays.sort(elements, ELEMENT_COMPARATOR); //Quickly find solution with binarysearch for (int i = 0, bound = elements.length; i < bound; i++) { Element a = elements[i]; Element b = new Element(target - a.num, -1); if (a.num == b.num) { if (i < elements.length - 1 && elements[i + 1].num == a.num) { return new int[]{a.index, elements[i + 1].index}; } } int bindex = Arrays.binarySearch(elements, b, ELEMENT_COMPARATOR); if (bindex >= 0) { return new int[]{a.index, elements[bindex].index}; } } return null; } }
Leetcode:Two Sum分析和实现的更多相关文章
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- [LeetCode] 907. Sum of Subarray Minimums 子数组最小值之和
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
随机推荐
- C++抽象类的实现
1.什么是抽象类? 答:简单的说,抽象类就是至少有一个纯虚函数的类. 2.抽象类的作用? 答:抽象类的主要作用就是为它所组织的继承层次结构提供一个公共的基类,这样它就具有公有行为的特征,其它派生的类可 ...
- hihocoder-1483区间价值 (二分+尺取法)
题目链接: 区间价值 给定n个数A1...An,小Ho想了解AL..AR中有多少对元素值相同.小Ho把这个数目定义为区间[L,R]的价值,用v[L,R]表示. 例如1 1 1 2 2这五个数所组成的区 ...
- bzoj 1500 维修序列
Written with StackEdit. Description 请写一个程序,要求维护一个数列,支持以下 \(6\) 种操作: 请注意,格式栏 中的下划线' _ '表示实际输入文件中的空格 I ...
- 【spring源码学习】springMVC之映射,拦截器解析,请求数据注入解析,DispatcherServlet执行过程
[一]springMVC之url和bean映射原理和源码解析 映射基本过程 (1)springMVC配置映射,需要在xml配置文件中配置<mvc:annotation-driven > ...
- nginx中在超全局变量$_SERVER中增加变量
业务中可能会用到一些自定义的超全局变量,需要在nginx中生成的,比如,每次nginx请求的id,可以在nginx中配置 如: location ~ \.php$ { root / ...
- 封装Socket.BeginReceive/EndReceive以支持Timeout
Socket .NET中的Socket类提供了网络通信常用的方法,分别提供了同步和异步两个版本,其中异步的实现是基于APM异步模式实现,即BeginXXX/EndXXX的方式.异步方法由于其非阻塞的特 ...
- ORA-12541:无监听错误解决办法
http://jingyan.baidu.com/article/03b2f78c7a0ab75ea237ae33.html 1. 从开始菜单中打开“Oracle Net Configuratio ...
- dataView 工具栏
option = { tooltip : { trigger: 'axis' }, legend: { data:['最高','最低'] }, toolbox: { show : true, orie ...
- emqtt 1 (初初初初稿)
第一篇,先简单分析一下整个emqtt 的大致结构,包括两个部分: 1.message packet 类型 2.message 流向 message packet 类型 P1:mqtt_packet 的 ...
- FPGA学习中的代码阅读
不管是学FPGA还是C语言,任何一种代码的学习都离不开大量的代码阅读,也就是多看,多学习别人的代码.初学者在学习的过程中更为重要的是模仿,模仿别人的代码算法怎么去处理的,模仿多了,代码看的多了,能力自 ...