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

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. C# 视频多人脸识别的实现

    上一篇内容的调整,提交到git了,https://github.com/catzhou2002/ArcFaceDemo基本思路如下:一.识别线程1.获取当前图片2.识别当前图片的人脸位置,并将结果存入 ...

  2. python中的进程和线程

    什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本 ...

  3. PHP添加Memcached扩展

    1.下载memcached扩展 https://pecl.php.net/package/memcache 2.tar -xzvf memcache-2.2.7.tgz    #解压memcached ...

  4. (转)c#中const与readonly区别

    const 的概念就是一个包含不能修改的值的变量.常数表达式是在编译时可被完全计算的表达式.因此不能从一个变量中提取的值来初始化常量.如果 const int a = b+1;b是一个变量,显然不能再 ...

  5. 【消息队列】从各方面比较下kafka、activemq、rabbitmq、rocketmq之间的区别

    一.单机吞吐量ActiveMQ:万级,吞吐量比RocketMQ和Kafka要低了一个数量级RabbitMQ:万级,吞吐量比RocketMQ和Kafka要低了一个数量级RocketMQ:10万级,Roc ...

  6. 直播 APP 的直播实现流程

    直播平台搭建所涉及的事项非常的广泛, 不仅需要直播源码. 直播系统开发. 后台服务 器.专门的运维人员等, 还需要技术团队切实的把控.下面, 小编就给大家确切的说下直播 平台搭建需要用到哪些步骤. 1 ...

  7. 查看cookie的快捷方法

    1.url中输入数字+javascript:alert(document.cookie)   如:2javascript:alert(document.cookie)   ,然后在浏览器中去掉2,回车 ...

  8. 【Java】【5】List随机取值

    //shuffle 打乱顺序 Collections.shuffle(list); //随机抽取1个值 System.out.println(list.get(0)); //随机抽取N个值 Syste ...

  9. 【Java】【2】String和List相互转换

    正文: 1,String转List //常见的为逗号分隔 String str = "a,b,c"; List<String> list1 = Arrays.asLis ...

  10. 使用axios请求数据,post请求出错。因为axios传递的请求参数是json格式,而后端接口要求是formData

    解决办法1:(IOS兼容性有问题,不推荐使用) // json格式转为formData格式,因为某些接口的原因 function json2formData(jsonData) { var param ...