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. java读取clob字段的几种方法

    http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380143fd3d1027fa3c215cc790f1a06 ...

  2. smarty、smarty格式化、smarty整数、smarty float、smarty各种转换方式、smarty日期转换等等 (转)

    <? require("setup.php"); define('PAGETITLE','pagtitle'); function insert_top($lid,$sid) ...

  3. Atitit.常见的4gl 第四代编程语言  与 dsl

    Atitit.常见的4gl 第四代编程语言  与 dsl 1. 4gl dsl发展历史1 2. 4gl dsl的特点1 3. 常见的4gl 第四代编程语言 dsl2 4. 未来趋势与标准2 4.1.1 ...

  4. 基于vue + axios + lrz.js 微信端图片压缩上传

    业务场景 微信端项目是基于Vux + Axios构建的,关于图片上传的业务场景有以下几点需求: 1.单张图片上传(如个人头像,实名认证等业务) 2.多张图片上传(如某类工单记录) 3.上传图片时期望能 ...

  5. ubuntu打开终端多开标签的快捷键是ctrl+ shift+ T 对比ctrl+ alt+ T 另外窗口打开一个终端

    ubuntu打开终端多开标签的快捷键是ctrl+ shift+ T 对比ctrl+ alt+ T 另外窗口打开一个终端

  6. webview长按保存图片

    private String imgurl = ""; /***     * 功能:长按图片保存到手机     */    @Override    public void onC ...

  7. Cocoa 静态显示一个对话框

    M // // form2.m // test_multi_window // // Created by on 23/7/14. // Copyright (c) 2014 EDU. All rig ...

  8. jq 获取页面中checkbox已经选中的checkbox

    var array={}; var arrChk=$("input[name='bike']:checked"); if(arrChk.length<=0){ alert(' ...

  9. Android开发 adb命令提示:Permission denied (转)

    如题:模拟器版本->android 7.1.1 遇到这样的情况把模拟器root一下就好了:su root =============2017年4月3日20:57:33============== ...

  10. mysql使用存储过程制造测试数据

    DELIMITER $$ DROP PROCEDURE IF EXISTS message_insert_procedure; CREATE PROCEDURE `test`.`message_ins ...