---恢复内容开始---

Two Sum

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

The function twoSum should  return  inclices 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 must assume that each input would have exactly one solution.

Input :number ={2,7,11,15},target =9;

Output :index1=1,index2=2

给定一个整数数组,找到两个数字,这样它们就可以加到一个特定的目标数。
函数二和应该返回两个数字的inclices,它们加起来到目标,在哪里。
index1必须小于index2。请注意您返回的答案(包括index1和index2)
不是从零开始的。
您必须假定每个输入都只有一个解决方案。
输入:数量= { 2、7、11、15 },目标= 9;
输出:index1 = 1,index2 = 2

---恢复内容结束---

 public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length <= ) {
return new int[];
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
// key = target - nums[i], just one solution
for (int i = ; i < nums.length; i++) {
map.put(target - nums[i], i);
}
for (int i = ; i < nums.length; i++) {
Integer v = map.get(nums[i]);
// can't use itself
if (v != null && v != i) {
return new int[] { i + , v + };
}
}
return null;
}

leetcode 刷题 数组类 Two Sum的更多相关文章

  1. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  2. (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 ...

  3. 【leetcode刷题笔记】Two Sum

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

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

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

  5. 【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 ...

  6. 【leetcode刷题笔记】Combination Sum

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

  7. LeetCode刷题总结-数组篇(上)

    数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...

  8. LeetCode刷题总结-数组篇(下)

    本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...

  9. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

随机推荐

  1. 学习笔记25—python基本运算法则

    1.矩阵的点乘: a*b, 矩阵乘法:dot(a*b),矩阵的次方:a**num (num = 2,表示2次)2.数组的并集,交集: >>> a = [1,2,3] >> ...

  2. 牛客网NOIP赛前集训营-提高组(第七场)C 洞穴

    洞穴 思路: 矩阵乘法 由于只需要知道 A ^ l 的 第a行第b个元素 所以我们每次在做矩阵乘法时只需要算第a行就可以了 还要像矩阵快速幂一样预处理A ^ (1<<d) 代码: #pra ...

  3. 电脑用HDMI线分屏后,耳机或音箱没声音之完美解决!

    现今,由于工作需要,很多人都偏爱给自己的电脑进行分屏,两个显示器同时连接在一台电脑上,并进行“扩展这些功能显示”,从而大大提高了工作积极性和工作效率.本人在进行分屏后遇到了耳机没有声音的问题,并进行了 ...

  4. [MySQL] 中 Schema和Database的区别

    在MySQL中,schema和database是同义词. CREATE SCHEMA和CREATE DATABASE是等效的. 但是其他的数据库产品(几乎所有数据库)有所不同.在oracle数据库产品 ...

  5. 二、idea + git

    1.配置git file->setting->git Test 2.配置gitHub 2.1 生成gitHub  settings->Developer settings->P ...

  6. induced pluripotent stem cell (iPSC) 诱导性多能干细胞

    参考: 诱导性多能干细胞 Induced pluripotent stem cell Induced Pluripotent Stem Cells: Problems and Advantages w ...

  7. Python实现Plugin(插件化开发)

    https://www.cnblogs.com/terencezhou/p/10276167.html

  8. exam_review to M1

    1. 情态动词,尤其是can/can’t,should/shouldn’t. for example: You should have washed the wound.你应该已经洗好了伤口 Well ...

  9. 自定义广播(BroadcastReceiver)事件 --Android开发

    本例演示自定义广播事件.我们需要做的是,在主活动中写发送广播的代码,然后在接收广播的类中写接收广播的代码. 1.主活动中点击按钮后发送广播 MainActivity.java: public clas ...

  10. Linq to XML 增删改查

    Linq to XML同样是对原C#访问XML文件的方法的封装,简化了用xpath进行xml的查询以及增加,修改,删除xml元素的操作.C#访问XML文件的常用类:XmlDocument,XmlEle ...