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. jenkins#构建并部署springboot的jar包

    0. 前提是有一个可以用的没有问题的Jenkins环境,这是基础 1. 安装publish over ssh 插件,(如果网速太慢,请去github 克隆代码,然后自己构建,然后上传安装此插件) 2. ...

  2. 016、MySQL取本年第一季度开始日期

    #取第1季度开始日期 SELECT date_add( dy, INTERVAL ( ) MONTH ) dy FROM ( ) dy ) x ; 效果如下: 不忘初心,如果您认为这篇文章有价值,认同 ...

  3. JavaScript基于原型的继承

    在一个纯粹的原型模式中,我们会摒弃类,转而专注于对象,基于原型的继承相比基于类的继承的概念上更为简单 if( typeof Object.beget !== 'function') { Object. ...

  4. 32位CPU和64位CPU 区别

    操作系统只是硬件和应用软件中间的一个平台. 32位操作系统针对的32位的CPU设计. 64位操作系统针对的64位的CPU设计.操作系统只是硬件和应用软件中间的一个平台. 32位操作系统针对的32位的C ...

  5. Java8系列 (四) 静态方法和默认方法(转载)

    静态方法和默认方法 我们可以在 Comparator 接口的源码中, 看到大量类似下面这样的方法声明 //default关键字修饰的默认方法 default Comparator<T> t ...

  6. IDEA 分屏显示

    效果: 步骤: 对着某个标签页单击右键,选择Split Vertically或者Split Horizontally即可.

  7. jar包学习

    jar: java的压缩包,主要用于存储类文件,或者配置文件等. 命令格式: jar -cf 包名.jar 包目录 解压缩: jar -xvf 包名.jar 将jar包目录列表重定向到一个文件中: j ...

  8. SCA-CNN: Spatial and Channel-wise Attention in Convolutional Networks for Image Captioning

    题目:SCA-CNN: Spatial and Channel-wise Attention in Convolutional Networks for Image Captioning 作者: Lo ...

  9. 前端第四篇---前端基础之jQuery

    前端第四篇---前端基础之jQuery 一.jQuery介绍 二.jQuery对象 三.jQuery基础语法 四.事件 五.动画效果 六.补充each 一.jQuery简介 1.jQuery介绍 jQ ...

  10. vue学习(十二)vue全家桶 Vue-router&Vuex

    一 vue-router的安装 二 vue-router的基本使用 三 命名路由 四 动态路由的匹配和路由组件的复用 一 vue-router的安装 NPM npm install vue-route ...