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 ...
随机推荐
- Day5-T4
原题目 Describe:最小生成树加权 code: #include<bits/stdc++.h> #define INF 214748364 #define eps 1e-9 #def ...
- swagger获取
参考 https://www.jianshu.com/p/840320d431a1 https://www.cnblogs.com/luoluocaihong/p/7106276.html
- java随机函数用法Random
原文地址:http://blog.csdn.net/wpjava/article/details/6004492 import java.util.Random; public class Ran ...
- 008.Delphi插件之QPlugins,服务的两种调用方法
这个QPlugins自带的DEMO,大概的意思就是,创建2个服务类,程序启动的时候注册这2个服务类.点击不同的按钮,使用不同的方法来调用这个服务. 效果界面如下 unit Frm_Main; inte ...
- Nifi简介及核心概念整理
简介 Apache NiFi 是一个易于使用.功能强大而且可靠的数据拉取.数据处理和分发系统,用于自动化管理系统间的数据流. 它支持高度可配置的指示图的数据路由.转换和系统中介逻辑,支持从多种数据源动 ...
- pyhton中pandas数据分析模块快速入门(非常容易懂)
//2019.07.16python中pandas模块应用1.pandas是python进行数据分析的数据分析库,它提供了对于大量数据进行分析的函数库和各种方法,它的官网是http://pandas. ...
- spring boot引入外部jar包
两种方法: 一:使用project-properties-java build path-Libraries 此方法的问题时不能使用mav打包. 二:写到POM里 首先在新建libs文件夹(根目录或者 ...
- Django 项目搭建
django(mvt结构) 虚拟环境 创建虚拟环境 mkvirtualenv django_py3 -p python3 切换虚拟环境 wokeon 虚拟环境名称 删除虚拟环境 rmvirtualen ...
- 086-PHP数组按数字排序和按字母排序
<?php $arr=array(2,54,167,'a','A','12'); //定义一个数组 echo '数组排序之前的信息:<br />'; print_r($arr); / ...
- ZOJ 3795 Grouping 强连通分量-tarjan
一开始我还天真的一遍DFS求出最长链以为就可以了 不过发现存在有向环,即强连通分量SCC,有向环里的每个点都是可比的,都要分别给个集合才行,最后应该把这些强连通分量缩成一个点,最后保证图里是 有向无环 ...