题目要求很简单,给你一个数组(例如,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])]。

  代码如下:

 

  1. public class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
  4. for (int i = 0; i < nums.length; i++) {
  5. map.put(nums[i],i);
  6. }
  7. int[] res = new int[2];
  8. for (int i = 0; i < nums.length; i++) {
  9. int remain = target - nums[i];
  10. if (map.containsKey(remain) && map.get(remain) != i) {
  11. res[0] = i;
  12. res[1] = map.get(remain);
  13. break;
  14. }
  15. }
  16. return res;
  17. }
  18. }

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. SQLite的介绍 操作Sqlite 具体实例

    1.SQLite简介 SQLite是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入  式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能 ...

  2. Server-Side UI Automation Provider - WinForm Sample

    Server-Side UI Automation Provider - WinForm Sample 2014-09-14 源代码  目录 引用程序集提供程序接口公开服务器端 UI 自动化提供程序从 ...

  3. Vector示例一,二

    #include <iostream> int main(void) { double a[] = {1, 2, 3, 4, 5}; std::cout<<mean(a, 5) ...

  4. Android开源库--SlidingMenu左右侧滑菜单

    如果说我比别人看得更远些,那是因为我站在了巨人的肩上.   github地址:https://github.com/jfeinstein10/SlidingMenu   设置: 1.下载之后以依赖项的 ...

  5. Cocos2d-x 开发手记

    1.所有的源文件统一新建到Classes里,否则无法找到源文件,这样也便于跨平台编译   2.绘图坐标系,与opengl采用相同坐标系,左下角为原点 纹理坐标系,以左上角为原点   3.最近有在学习C ...

  6. 详解Android中的屏幕方向

    屏幕方向 是对Activity而言的,所以你可以在AndroidManifest.xml 文件中,通过<activity> 标记的screenOrientation 属性进行设定,例如: ...

  7. POJ 2689

    题意:求[l, r]区间中的间隔距离最大与最小的相邻两个素数,r<2200000000, r-l<10^6 题解: 对于<a的合数,其必然存在一个素因子b<=sqrt(a). ...

  8. LA 3602 DNA Consensus String

    最近审题老是一错再错,Orz 题目中说求一个Hamming值总和最小的字符串,而不是从所给字符中找一个最小的 这样的话,我们逐列处理,所求字符串当前位置的字符应该是该列中出现次数最多其次ASCII值最 ...

  9. Codeforces Round #291 (Div. 2)

    A 题意:给出变换规则,单个数字t可以变成9-t,然后给出一个数,问最小能够变成多少. 自己做的时候理解成了不能输出前导0,但是题目的本意是不能有前导0(即最高位不能是0,其余位数按照规则就好) 55 ...

  10. 计算机视觉入门 Intorduction To Computer Vision

    本文将主要介绍图像分类问题,即给定一张图片,我们来给这张图片打一个标签,标签来自于预先设定的集合,比如{people,cat,dog...}等,这是CV的核心问题,图像分类在实际应用中也有许多变形,而 ...