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 提供了层面搜索(就是统计).命中醒目显示并且支持多种输出 ...
随机推荐
- selenium实现网易邮箱的登录注册
#实现163网站的注册 from selenium import webdriver import time driver = webdriver.Chrome() url = 'https://ma ...
- 剑指offer-面试题57_2-和为s的连续正数序列-穷举法
/* 题目: 输入一个整数s,输出所有和为s的连续整数序列. */ /* 思路: 穷举法. */ #include<iostream> #include<cstring> #i ...
- Pr制作音乐相册
Pr制作音乐相册 设置蒙版效果
- PHP Help Guideds
how does php work with Apache? https://stillat.com/blog/2014/04/02/how-does-php-work-with-the-web-se ...
- C++中用vector定义矩阵
熟悉c风格的矩阵定义,那么用纯c++特性vector如何定义一个矩阵呢? # include<vector> # include <iostream> int main() { ...
- IO流学习之File类
File类 Java文件类以抽象的方式代表文件名和目录路径名.该类主要用于文件和目录的创建.文件的查找和文件的删除等. File对象代表磁盘中实际存在的文件和目录.就是把文件和目录转换成对象,读取到内 ...
- sqli-labs less-5 --> less-6
盲注笔记: SQL盲注攻击:只有两种回显结果,错误和正确:无法用url查询爆出数据库的信息:回显注入语句中加入判断方式,使得回显结果为true或者false: SQL盲注分类:布尔盲注,时间盲注,报错 ...
- python的循环语句
python的循环语句有两种:for 和 while,for循环是对可迭代对象进行迭代并处理,因此for的对象是一个可以迭代的对象,而while循环的条件则是一个布尔值可以是一个返回布尔值的表达式. ...
- Tiptop ERP 采购运费一键分摊
项目背景: 公司的采购运费在逐年上升,之前财务都是做在管理费用中,金额大了后已经严重造成成本失真,所以财务要求it部能帮助分摊运费 1.纸质单据 2.系统入库单apmt720 3.系统请款单apm ...
- Python编码小技巧分享【新手必学】
本次分享了python编程小技巧总结如下,希望对大家有帮助,非常实用哦注意:很多人学Python过程中会遇到各种烦恼问题,没有人帮答疑.为此小编建了个Python全栈免费答疑交流.裙 :一久武其 ...