Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2


利用一个hashmap,把数组中的元素和它们的索引+1作为键-值对,然后对于每个元素numbers[i]寻找target-numbers[i],如果找到了就把i+1和map.get(target-numbers[i])返回。

代码如下:

 public class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] answer = new int[2];
if(numbers == null || numbers.length == 0)
return answer;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i = 0;i < numbers.length;i++)
map.put(numbers[i], i+1); for(int i = 0;i < numbers.length;i++){
if(map.containsKey(target-numbers[i])){
if(i+1 == map.get(target-numbers[i]))
continue;
answer[0] = i+1;
answer[1] = map.get(target-numbers[i]);
break;
}
} return answer;
}
}

【leetcode刷题笔记】Two Sum的更多相关文章

  1. 【leetcode刷题笔记】Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  2. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  3. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  4. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  5. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  6. LeetCode 刷题笔记 1. 两数之和(Two Sum)

    tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...

  7. (python)leetcode刷题笔记 01 TWO SUM

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  8. 【leetcode刷题笔记】Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  9. 【leetcode刷题笔记】Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

随机推荐

  1. 获取bundle文件下的资源

    NSBundle* bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle].resourcePath stringByAppendingPat ...

  2. Selenium3.14.1+Python安装和第一个Demo

    言简意赅的说下Selenium是什么 Selenium是前台测试框架,支持IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome等浏览器,我只 ...

  3. Delphi中array of const应用

    Delphi的Format函数大家都用得很多,第二个参数用着确实很方便.最近在数据库开发应用中需要自己创建一个带array of const参数的函数,对于常用的类型String,Integer,Po ...

  4. jQuery的$.fn使用

    jquery中文网为您提供jQuery的$.fn使用等资源,欢迎您收藏本站,我们将为您提供最新的jQuery的$.fn使用资源 $.fn是指jquery的命名空间,加上fn上的方法及属性,会对jque ...

  5. Windows App开发之编辑文本与绘制图形

    编辑文本及键盘输入 相信大家都会使用TextBox,但假设要让文本在TextBox中换行该怎么做呢?将TextWrapping属性设置为Wrap,将AcceptsReturn属性设置为True就好咯. ...

  6. docker 报错 Error response from daemon: driver failed programming external connectivity on endpoint mynginx

    Error response from daemon: driver failed programming external connectivity on endpoint mynginx (7d1 ...

  7. oracle如何进行索引监控分析和优化

    在生产环境.我们会发现: ① 索引表空间 I/O 非常高     ② "db file sequential read" 等待事件也比较高   这种迹象表明.整个数据库系统.索引的 ...

  8. find - exec 命令

    find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec  参数后面跟的是command ...

  9. mysql5.5.30源码安装及主从搭建

    双机热备(实验环境) 主服务器:ip地址192.168.100.244,mysql版本5.5.30,源码安装 从服务器:ip地址192.168.100.245 一.源码安装mysql5.5 启动目录: ...

  10. 要胀爆的Angular1.0

    尝试从http请求上遏制缓存: http://blog.csdn.net/u010039979/article/details/54376856 if (!$httpProvider.defaults ...