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

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. @Value注解分类解析

    1.1.1            @Value注解 @Value的作用是通过注解将常量.配置文件中的值.其他bean的属性值注入到变量中,作为变量的初始值. (1)常量注入 @Value(" ...

  2. ECharts图表的小工具

    本文介绍一个echarts工具类EChart.js,用来制作统计图表,基于echarts3. 一.工具类特性如下: 包含柱状图.折线图和饼图,可以实现这三类统计图之间的切换: 支持标题和副标题: 支持 ...

  3. mui 配置底部tab切换方式以模板的方式访问

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 猫眼电影爬取(二):requests+beautifulsoup,并将数据存储到mysql数据库

    上一篇通过requests+正则爬取了猫眼电影榜单,这次通过requests+beautifulsoup再爬取一次(其实这个网站更适合使用beautifulsoup库爬取) 1.先分析网页源码 可以看 ...

  5. [jQuery] 通过ajax保存到服务器,成功显示信息.

    保存数据到服务器,成功时显示信息. jQuery 代码: $.ajax({ type: "POST", url: "some.php", data: " ...

  6. JavaScript 第三章总结

    Getting functional function的特点 function can be reused over and over much more readable function is p ...

  7. HTML第十四章总结 HTML forms

    第十四章主要讲了 html forms,通过 forms,我们可以得到 customers' feedback,使得网页能够 interactive,本章的内容分为三个部分: forms 的 elem ...

  8. GWAS | 全基因组关联分析 | Linkage disequilibrium (LD)连锁不平衡 | 曼哈顿图 Manhattan_plot | QQ_plot | haplotype phasing

    现在GWAS已经属于比较古老的技术了,主要是碰到严重的瓶颈了,单纯的snp与表现的关联已经不够,需要具体的生物学解释,这些snp是如何具体导致疾病的发生的. 而且,大多数病找到的都不是个别显著的snp ...

  9. JavaScript Transpilers: 为什么需要用它们?Babel的使用介绍。

    英文原文 https://scotch.io/tutorials/javascript-transpilers-what-they-are-why-we-need-them 摘译(文章内的代码有些过期 ...

  10. Spring Boot 获得帮助

    如果你在使用 Spring Boot 的时候遇到了问题,我们很乐意为你提供帮助. 请访问 IX. How-to指南 中的内容 — 在这个指南中为常见的多数问题提供了解决方案. 学习更多有关 Sprin ...