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

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. python中的面向对象学习以及类的多态

    接下来类的第三个重要的特性:多态(一种接口,多种实现) 多态性(polymorphisn)是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特 ...

  2. Ubuntu 16.04 构建 Headless VNC 服务器

    终于放弃 Vino 了, 稳定性太低了. 而且,拔了显示器之后,总出现分辨率不对的问题. 于是,构建了一个 xfce4 + tightvnc 的 解决方案. 1) 把Vino相关的自启动都关了. (v ...

  3. C语言转义字符基础总结

    C语言转义字符总结 C语言中的转义字符,是字符常量中很特别的一类.初学者容易在这方面犯错误,比如说我. 错题1 答案:C解析:每一个转义字符具有一个长度,这个字符串中:\t, \x43, \', \ ...

  4. canvas学习之树叶动画

    项目地址:http://pan.baidu.com/s/1geJgqen 今天用canvas做了一个树叶发芽到凋落的动画,当然还有很多不完善的地方,不过也让我体会到了,做动画技术占2分,算法占8分.这 ...

  5. jQuery -- touch事件之滑动判断(左右上下方向)

    $("body").on("touchstart", function(e) { // 判断默认行为是否可以被禁用 if (e.cancelable) { // ...

  6. android -------- Data Binding的使用 ( 四 )ListView

    今天来说说DataBinding在列表ListView中的使用 主要分为两种,1: 基本的实体类  2:Observable 定义字段 listView布局文件 <?xml version=&q ...

  7. session一二事

    Session即回话,指一种持续性的.双向的连接.Session和Cookie在本质上没有什么区别,都是针对HTTP协议的局限性而提出的一种保持客户端和服务器间保持会话连接状态的机制. Session ...

  8. use . adb . get wifi

    adb shell 连接手机获取root权限,如果返回的字符串中不包含root字样,再输入su命令回车 继续输入cat /data/misc/wifi/*.conf命令,将会把文件打印出来 ssid表 ...

  9. jquery快速获得url 的get传值

    <script> var res = location.search.substr(1).split("&"); var arr={}; for (var i ...

  10. 在word中粘贴的图片为什么显示不完整

    一.背景 整理系统测试说明文档,截得图片粘贴到word中显示不完整. 二.错误问题 问题:在word中粘贴的图片为什么显示不完整,如图所示: 三.分析问题: 原因是原来设置的行间距是固定值,图片也作一 ...