题目要求很简单,给你一个数组(例如,nums = [2,7,11,15])和一个target(target = 9),找到数组里两个数相加后能得到target的这两个数的index。在本例中,返回的应该是[0,1]。

  碰到这样的题目,首先应该想到的是运用HashMap,记录数组里的每个元素和对应的index,把O(n^2)的问题转变为O(n)的问题,然后再第二次遍历数组,当current_number = nums[i]时,寻找HashMap里面是否存在target - nums[i],若存在,则输出的index为[i, map.get(terget - nums[i])]。

  代码如下:

 

 public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i],i);
}
int[] res = new int[2];
for (int i = 0; i < nums.length; i++) {
int remain = target - nums[i];
if (map.containsKey(remain) && map.get(remain) != i) {
res[0] = i;
res[1] = map.get(remain);
break;
}
}
return res;
}
}

LeetCode(1) -Two Sum的更多相关文章

  1. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  2. leetcode 练习1 two sum

    leetcode 练习1  two sum whowhoha@outlook.com 问题描述 Given an array of integers, return indices of the tw ...

  3. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  4. 【一天一道LeetCode】#113. Path Sum II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  6. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  7. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

  8. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  9. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

随机推荐

  1. framework-Binder

    init进程fork servicemanager进程用来提供(server)注册service和(client)检索service功能.servicemanager维护了一个service列表,cl ...

  2. 第一篇 ERP是什么?-从道的层面浅谈我的理解

    世界上称为ERP软件的软件很多,国外的有SAP,ORACLE,国内的有金蝶,用友,浪潮.这些由不同的厂商开发制作的软件总有其软件适用的场合.这个场合就是企业,而且是市场经济中的企业.个人是不会购买ER ...

  3. 总结Selenium自动化测试方法(五)自动化测试框架

    五.自动化测试框架 1.单元测试框架unittest class loginTests(unittest.TestCase): ①开始的初始化部分 @classmethod def setUpClas ...

  4. UVa (BFS) The Monocycle

    题目不光要求要到达终点而且要求所走的步数为5的倍数,每个时刻有三个选择,前进,左转弯,右转弯. 所以在vis数组中新增加两个维度即可,vis[x][y][dir][color]表示在(x, y)格子方 ...

  5. [反汇编练习] 160个CrackMe之026

    [反汇编练习] 160个CrackMe之026. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  6. IntelliJ IDEA使用总结篇

    解决控制台中文乱码的问题: 1.windows下改intellij安装目录下bin\idea.exe.vmoptions文件 加上 -Dfile.encoding=UT 2.设置IDEA server ...

  7. php flock注意事项

    对于实际的运用,必须将其添加到所有使用的文件脚本中 但注意:其函数无法再NFS或其他网络文件系统中使用也无法在多线程服务器API中使用.

  8. Linux apt-get

    apt-get apt-get是一条linux命令,适用于deb包管理式的操作系统,主要用于自动从互联网的软件仓库中搜索.安装.升级.卸载软件或操作系统. apt-get命令一般需要root权限执行, ...

  9. 收集些日本的VPS

    http://www.serverqueen.jp/service/vps.html http://www.seeds.ne.jp http://dream.jp/vps/ http://fc2-vp ...

  10. PHP String函数分类

    1.查找字符位置函数: strpos  ($str,search,[int]):    查找search在$str中的第一次位置从int开始: stripos ($str,search,[int]): ...