Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

描述:给出一个整形数组,若数组内任意两个元素相加等于给出的目标结果,则以数组的形式返回该两个元素的数组下标。

可以假设每个输入都只有一个解决方案,且不能使用相同的元素两次。

方法尝试:

利用HashMap, 时间复杂度为O(n)。

解决方法:

static int[] twoSum(int[] numbers, int target) {
//1、若target=9,int[] numbers = {2, 7, 11, 15, 34, 5}
int[] result = new int[2];
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numbers.length; i++) {
if (map.containsKey(target - numbers[i])) {//3、9-7=2
result[1] = i;
result[0] = map.get(target - numbers[i]);
return result;
}
map.put(numbers[i], i);///2、i=0,(2,0)
}
return result;
}

1、Two Sum的更多相关文章

  1. 【LeetCode】1、Two Sum

    题目等级:Easy 题目描述:   Given an array of integers, return indices of the two numbers such that they add u ...

  2. 【LEETCODE】47、985. Sum of Even Numbers After Queries

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  3. MongoDb 创建、更新以及删除文档常用命令

    mongodb由C++写就,其名字来自humongous这个单词的中间部分,从名字可见其野心所在就是海量数据的处理.关于它的一个最简洁描述为:scalable, high-performance, o ...

  4. DSO、CUBE区别(覆盖、合计)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. 【MySQL】技巧 之 count(*)、count(1)、count(col)

    只看结果的话,Select Count(*) 和 Select Count(1) 两着返回结果是一样的. 假如表沒有主键(Primary key), 那么count(1)比count(*)快,如果有主 ...

  6. 常用SQL语句(增删查改、合并统计、模糊搜索)

    转自:http://www.cnblogs.com/ljianhui/archive/2012/08/13/2695906.html 常用SQL语句 首行当然是最基本的增删查改啦,其中最重要的是查. ...

  7. LeetCode之Easy篇 ——(1)Two Sum

    1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...

  8. Java 8-Lambda表达式、方法引用、标准函数接口与流操作、管道操作之间的关系

    1.Lambda表达式与接口之间的关系 只要Lambda表达式的声明形式与接口相一致,在很多情况下都可以替换接口.见如下代码 Thread t1 = new Thread(new Runnable() ...

  9. solr简介、学习详细过程!(超详细~)

    solr是什么呢? 一.Solr它是一种开放源码的.基于 Lucene Java 的搜索服务器,易于加入到 Web 应用程序中. 二.Solr 提供了层面搜索(就是统计).命中醒目显示并且支持多种输出 ...

随机推荐

  1. 网站SEO中服务器优化的三个问题

    网站做好之后,站长第一件事就是想到去做SEO,但是有一些网站在做优化的时候,出现一些奇怪的情况,比如说优化已经不错的网站,排名突然就掉下来了:还有一些网站各项优化工作都是非常认真,但是排名却一直不上来 ...

  2. qt creator源码全方面分析(2-2)

    目录 Common Extension Tasks Common Extension Tasks 本节总结了可用于将UI组件添加到Qt Creator的API函数. 任务 详细 API 添加菜单或菜单 ...

  3. 理解Android线程创建流程

    copy from : http://gityuan.com/2016/09/24/android-thread/ 基于Android 6.0源码剖析,分析Android线程的创建过程 /androi ...

  4. 转: Laravel的数据库迁移 介绍的比较清晰

    原文: https://blog.sbot.io/articles/12/Laravel-数据库迁移(Database-Migrations)操作实例 很多人可能在学习Laravel框架的时候,对La ...

  5. IDEA开发Maven构建SSM项目遇到的坑,action

    Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid b ...

  6. NodeJS_0001:关于install的方式

    最近在写Node程序的时候,突然对 npm install 的-save和-save-dev 这两个参数的使用比较混乱.其实博主在这之前对这两个参数的理解也是模糊的,各种查资料和实践后对它们之间的异同 ...

  7. 2018 IEEE极限编程大赛 题解

    去年742,今年72,也算一种小小的进步. 明年前30(笑 1. Drawing Rooted Binary Trees 给定一个树的中序和前序的遍历,要求输出这棵树(包括空格的) #include ...

  8. PHP MySQLi Prepared Statements Tutorial to Prevent SQL Injection

    https://websitebeaver.com/prepared-statements-in-php-mysqli-to-prevent-sql-injection#introduction On ...

  9. 编程中的幂等性 —— HTTP幂等性

    幂等(idempotent.idempotence)是一个数学与计算机学概念,常见于抽象代数中. 在编程中.一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同.幂等函数,或幂等方法, ...

  10. 在Visual Studio中将dll以资源的形式嵌入exe中

    一.Dll的优点: 1.扩展应用程序的特性 2.简化项目管理 3.有助于节省内存 4.促进资源的共享 5.促进本地化 6.有助于解决平台间的差异 7.可用于特殊目的 有关于dll及注入相关理论资料,可 ...