Q1:Two Sum
1. Two Sum
官方的链接:1. Two Sum
Description :
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].
问题描述
给定一个整数数组,返回数组中和为数字target的两个元素的下标。 可以假定每个输入target都有一组特定的输出,并且同个元素只使用一次。
思路
数组是无序的,比较便捷的方法就是按照k-v(元素-下标)形式将数据元素进行判断和存储,判断差是否在k中有存储,若有,说明存在,否则把元素进行存储。因为只需要扫描一遍,时间复杂度O(n),空间复杂度O(n)。
public class Q1_TwoSum {
public int[] twoSum(int[] nums, int target) {
if (null == nums || nums.length < 2)
return null;
//保留結果
int[] result = new int[2];
Map<Integer, Integer> resultMap = new Hashtable<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
//求差
int differ = target - nums[i];
//存在k,说明符合条件
if (resultMap.containsKey(differ)) {
int tmpResult = resultMap.get(differ);
if (tmpResult > i) {
//输出正确的数组下标
result[0] = i;
result[1] = tmpResult;
} else {
result[0] = tmpResult;
result[1] = i;
}
return result;
} else {
//k-v存储
resultMap.put(nums[i], i);
}
}
return null;
}
}
Q1:Two Sum的更多相关文章
- LeetCode 题解(一):Two Sum
LeetCode : two sum 第一次写博客,算是熟悉这些编辑环境吧,本来是打算在csdn上用markdown写的,结果改了博客介绍就被关闭了,晕死...好了,话不多说,今天打算拿LeetCod ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- LeetCode-1:Two Sum
[Problem:1-Two Sum] Given an array of integers, return indices of the two numbers such that they add ...
- HDU1244:Max Sum Plus Plus Plus
题目链接:Max Sum Plus Plus Plus 题意:在n个数中取m段数使得这m段数之和最大,段与段之间不能重叠 分析:见代码 //dp[i][j]表示前i个数取了j段的最大值 //状态转移: ...
- SQL-W3School-函数:SQL SUM() 函数
ylbtech-SQL-W3School-函数:SQL SUM() 函数 1.返回顶部 1. SUM() 函数 SUM 函数返回数值列的总数(总额). SQL SUM() 语法 SELECT SUM( ...
- No.001:Two Sum
问题: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- HDU 1024:Max Sum Plus Plus(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...
- leetcode:Path Sum (路径之和) 【面试算法题】
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- Project Euler 83:Path sum: four ways 路径和:4个方向
Path sum: four ways NOTE: This problem is a significantly more challenging version of Problem 81. In ...
随机推荐
- RAM和ROM的区别
区别如下: 1.概念 RAM(random access memory)即随机存储内存,这种存储器在断电时将丢失其存储内容,故主要用于存储短时间使用的程序.ROM(Read-Only Memory)即 ...
- Hive 中的 order by, sort by, distribute by 与 cluster by
Order By order by 会对输入做全排序, 因此只有一个Reducer(多个Reducer无法保证全局有序), 然而只有一个Reducer, 会导致当输入规模较大时, 消耗较长的计算时间. ...
- class(二)--派生类的继承
前言 从我之前的一篇笔记对象的继承中, 我们可以知道JS的继承方式依赖原型链,而比较好的继承方式是寄生组合式继承 先来温习下什么是寄生组合式继承 function Rectangle(length, ...
- pwntool基础和ida常用操作
pwntools:http://www.91ri.org/14382.html ida:https://www.jianshu.com/p/d425140c6561
- 开源DDD设计模式框架YMNNetCoreFrameWork第三篇-增加ASp.net core Identity身份认证,JWT身份认证
1.框架增加Identity注册功能 2.框架增加identity登录以后获取JWTtoken 3.请求接口通过token请求,增加验证特性 源代码地址:https://github.com/topg ...
- 2.python的基本数据类型
(1)整形和浮点型 (2)布尔 (3)字符串 (4)转义 (5)字符串的操作 (6)列表 (7)元组 (8)集合set 特性:无序.不重复 (9)字典
- mvvm双向绑定机制的原理和代码实现
mvvm框架的双向绑定,即当对象改变时,自动改变相关的dom元素的值,反之,当dom元素改变时,能自动更新对象的值,当然dom元素一般是指可输出的input元素. 1. 首先实现单向绑定,在指定对象的 ...
- 留学萌新Essay写作须知
Essay是留学生们接触比较多的一项留学生作业,但尽管如此,依旧有部分同学对于essay写作是没有足够的把握的.随着开学季的到来,很多萌新初次接触Essay写作,难免会有很多不懂得地方.所以今天小编就 ...
- SciKit-Learn 加载数据集
章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Learn 可视化数据:主成分分析(P ...
- 关于springmvc的消息转换器
之前有用到消息转换器,一直是配置configureMessageConverters()这个方法的,虽然知道也有extendMessageConverters().它们的区别的是第一个不会继承框架默认 ...