问题:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

官方难度:

Easy

翻译:

给定一个整型数组和一个给定值,返回数组中两数的下标,这两数的和为给定值,假设解集唯一确定。

例子:

给定数组{2,7,11,15},给定值9,返回下标[0,1]。

方法一:

  1. 利用一个二次循环,在之后的数组中查找target-nums[i]是否存在。

方法一的解题代码:

     public static int[] method_1(int[] nums, int target) {
for (int i = 0; i < nums.length - 1; i++) {
int numberToFind = target - nums[i];
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] == numberToFind) {
return new int[] { i, j };
}
}
}
throw new IllegalArgumentException("No two sum solution");
}

method_1

方法二:

  1. 相对而言,在Java里面,数组是一种帮助方法相对较少的数据结构,我们可以期盼,有一种方法,可以快速的定位是否存在某一个给定的值,那么相对就可以减少内循环的时间开销。
  2. 第一个想法是List集合,List集合有List.contains()方法,迅速定位List集合中是否存在某一给定的元素,同时数组的帮助方法Arrays.asList(),可以直接将数组转化为List集合。同时,还有List.indexOf()方法,来定位下标。
  3. 想法总是美好的,但是这一方法的雷区实在太多。
  4. 数组和集合有一个很重要的差别在于,集合不能接收基本类型,你可能会想象Arrays.asList()能触发自动装箱,但是很可惜,int[]也是List集合能够接收的类型。所以你需要先显式地将int[]转化为Integer[]。
  5. List.indexOf(target)是获取集合中第一个为target的下标,那么就无法定位例如nums[]={2,1,2},target=4的问题。这时候自然地会想到利用List.lastIndexOf()方法。但是问题又来了,例如nums[]={3,2,4},target=6的问题,返回的数组为[0,0],所以要优先加上index1!=idnex2的判断条件。
  6. 鉴于有这么多的雷区,就时间消耗上看,也不见得有多少提升,可能还比方法一有所不如,不推荐使用这种方法。

方法二的解题代码:

     public static int[] method_2(int[] nums, int target) {
// 先将int[]转成Integer[]
Integer[] collectionArray = new Integer[nums.length];
for (int i = 0; i < nums.length; i++) {
collectionArray[i] = nums[i];
}
List<Integer> list = new ArrayList<>();
list = Arrays.asList(collectionArray);
for (Integer a : list) {
if (list.contains(target - a)) {
// 防止原数组有重复数字的情况
int index1 = list.indexOf(a);
int index2 = list.lastIndexOf(target - a);
if (index1 != index2) {
return new int[] { index1, index2 };
}
}
}
throw new IllegalArgumentException("No two sum solution");
}

method_2

方法三:

  1. 无论是方法一,还是方法二的算法,最大的缺点在于,它需要在剩余数组中寻找结果,当数组很大的时候,这种时间开销就相当庞大了。其实我们可以将已经遍历过的数字放入另一个数据结构中,在这个数据结构中寻找解集。

方法三的解题代码:

     public static int[] method_3(int[] nums, int target) {
List<Integer> list = new ArrayList<>();
list.add(nums[0]);
for (int i = 1; i < nums.length; i++) {
if (list.contains(target - nums[i])) {
return new int[] { list.indexOf(target - nums[i]), i };
}
list.add(nums[i]);
}
throw new IllegalArgumentException("No two sum solution");
}

method_3

方法四:

  1. 仔细分析方法三中算法的时间开销,不难发现List.contains()方法在List集合中查找的效率决定了整个算法的时间开销。那么List.contains()是不是最优的查找方式呢?
  2. 说到查找速度,不可避免地想到了哈希表,自然而然的,就考虑尝试使用HashMap的数据结构,将nums[i]作为key,i作为value,同时Map集合提供Map.containsKey()方法在HashMap的实现上,拥有更高的效率。

方法四的解题代码:

 public static int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length < 2) {
throw new IllegalArgumentException("Input error");
}
Map<Integer, Integer> map = new HashMap<>();
map.put(nums[0], 0);
for (int i = 1; i < nums.length; i++) {
int numberToFind = target - nums[i];
if (map.containsKey(numberToFind)) {
return new int[] { map.get(numberToFind), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}

twoSum

备注:

  1. 一般的算法,需要考虑入参的合理性,这一点在方法四的代码中有写出。
  2. 可以将第一个元素直接放入集合,循环从i=1开始,可以省去一次判断的操作。
  3. 虽然哈希表拥有很高的速度,但是Map的空间的开销要远比List来得多,如果效率提升不大的话,尽可能还是不要用Map了。当然,本题中对于时间效率的提升还是值得的。

相关链接:

https://leetcode.com/problems/two-sum/

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/easy/Q001.java

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

No.001:Two Sum的更多相关文章

  1. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  2. LeetCode 题解(一):Two Sum

    LeetCode : two sum 第一次写博客,算是熟悉这些编辑环境吧,本来是打算在csdn上用markdown写的,结果改了博客介绍就被关闭了,晕死...好了,话不多说,今天打算拿LeetCod ...

  3. Python3练习题 001:4个数字求不重复的3位数

    #Python练习题 001:4个数字求不重复的3位数#方法一import itertoolsres = [][res.append(i[0]*100 + i[1]*10 + i[2]) for i ...

  4. 实用ExtJS教程100例-001:开天辟地的Hello World

    ExtJS功能繁多,要想彻底的了解确实很困难.作为初学者,如何能找到一条快速的通道呢?我觉得,如果你有Javascript的基础,那就不要惧怕ExtJS的复杂,从动手开始,遇到问题,解决问题,积累经验 ...

  5. LeetCode-1:Two Sum

    [Problem:1-Two Sum] Given an array of integers, return indices of the two numbers such that they add ...

  6. HDU1244:Max Sum Plus Plus Plus

    题目链接:Max Sum Plus Plus Plus 题意:在n个数中取m段数使得这m段数之和最大,段与段之间不能重叠 分析:见代码 //dp[i][j]表示前i个数取了j段的最大值 //状态转移: ...

  7. SQL-W3School-函数:SQL SUM() 函数

    ylbtech-SQL-W3School-函数:SQL SUM() 函数 1.返回顶部 1. SUM() 函数 SUM 函数返回数值列的总数(总额). SQL SUM() 语法 SELECT SUM( ...

  8. 数据测试001:利用python连接数据库插入excel数据

    数据测试001:利用python连接数据库插入excel数据 最近在做数据测试,主要是做报表系统,需要往数据库插入数据验证服务逻辑,本次介绍如何利用python脚本插入Oracle和Mysql库中: ...

  9. HDU 1024:Max Sum Plus Plus(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...

随机推荐

  1. 使用PopupWindow实现Menu功能

    参考:http://www.cnblogs.com/sw926/p/3230659.html 注意: PopupWindow会给PopupView设置Padding,会导致ContentView的左右 ...

  2. 在Windows2008系统中利用IIS建立FTP服务器

    一.服务器管理器   1.2008的系统使用服务器管理器,选择角色,因为我之前已经开启了IIS服务器角色,所以我现在只要添加角色服务即可,如果你没有开启过的话,直接添加角色即可.   2.选择WEB服 ...

  3. [Android界面] 这样的选择器怎么实现?? 充值选择

    1  充值的或年纪的 或  1 先讲例子 http://blog.csdn.net/lmj623565791/article/details/48393217: 本文出自:[张鸿洋的博客] 一.概述 ...

  4. Javascript类继承-机制-代码Demo【原创】

    最近看到<Javascript设计模式>,对js模拟的”继承方式“有了更深一步的了解,虽然之前也总是用到prototype.new ,但只是知其然不知所以然,现在将类继承的方法整理如下,暂 ...

  5. Android JNI 之 JNIEnv 解析

    jni.h文件 : 了解 JNI 需要配合 jni.h 文件, jni.h 是 Google NDK 中的一个文件, 位置是 $/android-ndk-r9d/platforms/android-1 ...

  6. ACE代码编辑器,代码提示,添加自定义数据

    //设置自动提示代码 var setCompleteData = function(data) { var langTools = ace.require("ace/ext/language ...

  7. Oracle数据库入门——pctfree和pctused详解

    一.建立表时候,注意PCTFREE参数的作用 PCTFREE:为一个块保留的空间百分比,表示数据块在什么情况下可以被insert,默认是10,表示当数据块的可用空间低于10%后,就不可以被insert ...

  8. php 设计模式 - 单例

    概述: 作为对象的创建模式,单例确保某一个内在系统中只存在一个实例,它不可以创建副本. 克隆函数(__clone )以及构造函数(__construct )必须声明为私用, 防止外部程序 创建一个新类 ...

  9. [转]使用Xcode 4发布App 实例操作

    使用xcode 4发布app 实例操作是本文介绍的内容,不多说,我们直接进入话题. 1.iOS Provisioning Portal 和iTunes Connect 没有变,下载与安装.mobile ...

  10. easyui menubutton combobox 被遮盖问题

    如图一所示,menubutton 中的 combobox 被遮盖 z-Index 不够.这是作者给出的解决方案 <a href="#" class="easyui- ...