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 ...
随机推荐
- 在命令提示符中运行install adb 包名.apk文件 遇到的问题
昨天更新了SDK以后遇到一个奇怪的问题,使用eclipse直接运行程序到手机没有问题,但是如果将程序打包之后在命令提示符中执行adb install 包名.apk 就会报错:adb server is ...
- 零距离初体验uboot
一.uboot配置编译步骤 1.开发板光盘的BSP 2.复制到虚拟机,tar -jxvf qt_x210v3_130807.tar.bz2 3.配置:make x210_sd_config 4.出现C ...
- Golang gin开源实例——表设计
UML Model 基本模型定义 type Model struct { ID int `gorm:"primary_key" json:"id"` Creat ...
- js加密(十二)yy.com rsa
1. url: https://aq.yy.com/ 2. target: 登录js 3. 是一个简单的rsa加密,找到加密的js文件,全部复制出来,修改一下就好. 4. 和网页中的一样
- eshop5-maven 安装
1. Maven 安装 2.下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.0.5/binaries/ 3. 通过ta ...
- java csv文件写入
List<String> list_code = null; 方案1 控制字符集: BufferedWriter bw=new BufferedWriter(new OutputStrea ...
- Consul 简介及集群安装
简介 Consul是基于GO语言开发的开源工具,主要面向分布式,服务化的系统提供服务注册.服务发现和配置管理的功能. Consul的功能都很实用,其中包括:服务注册/发现.健康检查.Key/Value ...
- Job for nginx.service failed because the control process exited with error code. See “systemctl stat
启动nginx服务时如果遇到这个错误 Job for nginx.service failed because the control process exited with error code. ...
- JumpServer简单使用
笔记内容:简单使用jumpserver笔记日期:2018-01-22 23.9 创建jumpserver普通用户 23.10 添加机器 23.11 添加系统用户并授权 23.12 添加授权规则 23. ...
- 十四、JavaScript之不同类型变量相加
一.代码如下 二.效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" conten ...