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)。

[github-here]

 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的更多相关文章

  1. LeetCode 题解(一):Two Sum

    LeetCode : two sum 第一次写博客,算是熟悉这些编辑环境吧,本来是打算在csdn上用markdown写的,结果改了博客介绍就被关闭了,晕死...好了,话不多说,今天打算拿LeetCod ...

  2. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  3. LeetCode-1:Two Sum

    [Problem:1-Two Sum] Given an array of integers, return indices of the two numbers such that they add ...

  4. HDU1244:Max Sum Plus Plus Plus

    题目链接:Max Sum Plus Plus Plus 题意:在n个数中取m段数使得这m段数之和最大,段与段之间不能重叠 分析:见代码 //dp[i][j]表示前i个数取了j段的最大值 //状态转移: ...

  5. SQL-W3School-函数:SQL SUM() 函数

    ylbtech-SQL-W3School-函数:SQL SUM() 函数 1.返回顶部 1. SUM() 函数 SUM 函数返回数值列的总数(总额). SQL SUM() 语法 SELECT SUM( ...

  6. No.001:Two Sum

    问题: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. 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 ...

  8. leetcode:Path Sum (路径之和) 【面试算法题】

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. 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 ...

随机推荐

  1. vscode dart 插件 关闭自动注释

    vscode dart 插件 关闭自动注释 左下角设置 --> 搜索 Closing Labels --> 去掉勾选

  2. Problem B: Bulbs

    Problem B: Bulbs Greg has an m×n grid of Sweet Lightbulbs of Pure Coolness he would like to turn on. ...

  3. P1003 我要通过!

    转跳点:

  4. HihoCoder第十二周:刷油漆

    #1055 : 刷油漆 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho有着一棵灰常好玩的树玩具!这棵树玩具是由N个小球和N-1根木棍拼凑而成,这N个小球 ...

  5. 洛谷 三月月赛 C

    呵呵呵呵,这个sb题做了好久,然并卵,还是不对. 挖坑++ 然而我感觉我做的对了,偷瞄了一下题解应该没什么问题. 这个题有n个点,n条边,所以是个基环树(我也不知道是不是这个名) 要每个点有联通,就是 ...

  6. WTM框架在开发过程中如何动态迁移表和创建表

    官方迁移方法:https://wtmdoc.walkingtec.cn/#/Data/Migration 但是在实际开发过程中使用Add-Migration 方法迁移会发现,把系统内置的表也全部带出来 ...

  7. tf.argmax()函数作用

    tf.argmax()函数原型: def argmax(input, axis=None, name=None, dimension=None, output_type=dtypes.int64) 作 ...

  8. React 组件通讯

    React   父→子组件通讯   在父组件中子组件上  绑定一个 变量名={要传递的数据}:走我们去子组件中接收....      直接用  this.props.刚刚起的变量名就ok了    上代 ...

  9. 笔记本如何不按Fn键就能实现F键的功能

    笔记本的F1~F12键的附带功能如何改成 不用按Fn键就能实现F1~F12的功能 本人现在使用的是一款ThinkPad的本本,之前在台式机上愉快的玩耍的时候键盘上的F键直接按一下就可以实现相应的功能, ...

  10. 【剑指Offer】面试题09. 用两个栈实现队列

    题目 用两个栈实现一个队列.队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能.(若队列中没有元素,delete ...