1、Two Sum
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的更多相关文章
- 【LeetCode】1、Two Sum
题目等级:Easy 题目描述: Given an array of integers, return indices of the two numbers such that they add u ...
- 【LEETCODE】47、985. Sum of Even Numbers After Queries
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- MongoDb 创建、更新以及删除文档常用命令
mongodb由C++写就,其名字来自humongous这个单词的中间部分,从名字可见其野心所在就是海量数据的处理.关于它的一个最简洁描述为:scalable, high-performance, o ...
- DSO、CUBE区别(覆盖、合计)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 【MySQL】技巧 之 count(*)、count(1)、count(col)
只看结果的话,Select Count(*) 和 Select Count(1) 两着返回结果是一样的. 假如表沒有主键(Primary key), 那么count(1)比count(*)快,如果有主 ...
- 常用SQL语句(增删查改、合并统计、模糊搜索)
转自:http://www.cnblogs.com/ljianhui/archive/2012/08/13/2695906.html 常用SQL语句 首行当然是最基本的增删查改啦,其中最重要的是查. ...
- 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 ...
- Java 8-Lambda表达式、方法引用、标准函数接口与流操作、管道操作之间的关系
1.Lambda表达式与接口之间的关系 只要Lambda表达式的声明形式与接口相一致,在很多情况下都可以替换接口.见如下代码 Thread t1 = new Thread(new Runnable() ...
- solr简介、学习详细过程!(超详细~)
solr是什么呢? 一.Solr它是一种开放源码的.基于 Lucene Java 的搜索服务器,易于加入到 Web 应用程序中. 二.Solr 提供了层面搜索(就是统计).命中醒目显示并且支持多种输出 ...
随机推荐
- Beego 过滤器
过滤器 beego 支持自定义过滤中间件,例如安全验证,强制跳转等. 过滤器函数如下所示: beego.InsertFilter(pattern string, position int, filte ...
- SpringBoot从1.5.1→2.2.4项目加包扫雷二:打不到符号java: org.springframework.boot.autoconfigure.web.相关配置错误支持包
import org.springframework.boot.autoconfigure.web.DefaultErrorAttributes→org.springframework.boot.we ...
- Eclipse使用Alibaba Cloud Toolkit极速部署项目
个人博客 地址:https://www.wenhaofan.com/a/20190716205809 什么是Alibaba Cloud Toolkit Cloud Toolkit 是针对 IDE 平台 ...
- Vue的H5页面唤起支付宝支付
目前项目中比较常用的第三方支付无非就是支付宝支付和微信支付.下面介绍一下Vue中H5页面如何使用支付宝支付.其实很简单的,只不过是调自己后台的一个接口而已(后台根据支付宝文档,写好支付接口). 触发支 ...
- Intel 8086 常用汇编指令表
一.数据传输指令 它们在存贮器和寄存器.寄存器和输入输出端口之间传送数据. 1. 通用数据传送指令. MOV 传送字或字节. MOVSX 先符号扩展,再传送. MOVZX 先零扩展,再传送. PUSH ...
- sql注入文件写入和读取
系统固定文件路径:https://blog.csdn.net/ncafei/article/details/54616826 /etc/passwd c:/windows/win.ini 文件读取使用 ...
- Secondary NameNode:它究竟有什么作用?
前言 最近刚接触Hadoop, 一直没有弄明白NameNode和Secondary NameNode的区别和关系.很多人都认为,Secondary NameNode是NameNode的备份,是为了防止 ...
- 题解【洛谷P1596】[USACO10OCT]Lake Counting
题面 \(\text{Flood Fill}\) 模板题. \(\text{Flood Fill}\) 可以快速求出一个图中连通块的个数. 大概就是遍历每一个点,如果它没有被遍历过且是一个新连通块,那 ...
- C++ lambda函数及其用法(转)
由于接触C++不久,很多东西比较陌生,今天看阿里云OSS的C++ SDK文件下载部分例子,发现有如下lambda表达式用法,故了解一下相关知识 /*获取文件到本地文件*/ GetObjectReque ...
- 初入python
初入python 一定要学好python 求1-100的和: i=1 s=0 while i<101: s=s+i i=i+1 print(s)