给定一个整数数列,找出其中和为特定值的那两个数。

你可以假设每个输入都只会有一种答案,同样的元素不能被重用。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]

方法一


使用i,j遍历数组nums中的每一个变量,验证nums[i]+nums[j]是否等于target

代码如下:

    public static int[] twoSum(int[] nums, int target) {

        int N = nums.length;

        for (int i=0; i<N-1; i++)
for (int j=i+1; j<N; j++)
if (nums[i] + nums[j] == target)
return new int[] {i,j}; return null;
}

此方法的复杂度为O(N^2)

方法二

使用HashMap存储nums的值及对应的下标

定义变量c=target-nums[i],在map中寻找是否存在键值为c

代码如下:

    public static int[] twoSumMap(int[] nums, int target) {

        int N = nums.length;
Map<Integer, Integer> map = new HashMap<>(); // nums的值为key,下标为value
for (int i=0; i<N; i++)
map.put(nums[i], i); for (int i=0; i<N-1; i++) { int complement = target - nums[i];
// 找到的键值不能为i
if (map.containsKey(complement) && map.get(complement)!=i)
return new int[] {i, map.get(complement)};
}
return null;
}

复杂度分析:

将nums存入map中,空间复杂度O(N)

for循环寻找两个下标,时间复杂度为O(N)

该方法中,键值为数组值,value为数组下标,故数组中数值相等的元素在map中只存储了一次。

如nums[] = {2, 5, 5, 6}, map中为2-0, 5-2, 6-3,但这并不影响该算法找到正确的答案

另外,若数组nums为有序的,则可以使用两个指针分别指向数组的首尾两个元素。

比较两个元素的和与target,根据比较结果移动指针,直到两指针相遇或找到结果

实现代码如下:

    public static int[] twoSumOrder(int[] nums, int target) {

        int i = 0;
int j = nums.length - 1; while (i != j) { if (nums[i] + nums[j] == target)
return new int[] {i,j};
else if (nums[i] + nums[j] > target)
j--;
else
i++;
} return null;
}

复杂度为O(N)

两数之和 Two Sum的更多相关文章

  1. 领扣-1/167 两数之和 Two Sum MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. [Swift]LeetCode1 .两数之和 | Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  3. c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538

    第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a> ...

  4. LeetCode 1:两数之和 Two Sum

    题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中 ...

  5. 1.两数之和(Two Sum) C++

    暴力法可解决,速度很慢. 解决办法:哈希表 知识点: map的构造 遍历map使用迭代器,判断条件 插入 pair<int,int> 寻找key是否存在 class Solution { ...

  6. LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)

    653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...

  7. LeetCode_#1_两数之和 Two Sum_C++题解

    1. 两数之和 Two Sum 题目描述 Given an array of integers, return indices of the two numbers such that they ad ...

  8. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  9. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

随机推荐

  1. WinForm窗体下Excel的导入

    一.Winform窗体程序的Excel的导入 把Excel导入到内存中的DataTable 方法实现: #region ExcelToDataTable public static DataTable ...

  2. .net图表工具汇总

    概述:图形图表的可视化数据表现形式已成为一种趋势,本文推荐了10款非常好用的.NET图表控件,希望对广大.NET图表开发者能有所帮助. 读图时代,图形图表的可视化数据表现形式已成为一种趋势.因为图表能 ...

  3. js设计模式-观察者模式

    定义: 观察者模式又叫发布订阅模式,它定义了对象间的一种一对多的依赖关系.观察者模式让两个对象松耦合地联系在一起,虽然不太清楚彼此的细节,但这不影响他们之间的互相通信. 思路 定义一个对象,在对象中实 ...

  4. O(logn)二叉树中的意义----高性能(四)

    转载地址:https://zhidao.baidu.com/question/239708227508660244.html?qbl=relate_question_2&word=%CA%B1 ...

  5. POJ 2896 AC自动机 or 暴力

    DESCRIPTION :大意是说.给你n个代表病毒的字符串.m个表示网站的字符串.让你计算有多少个网站被病毒感染了.被那些病毒感染了. 刚开始就想暴力.然而,忽略了条件:每个网站最多有三个病毒.于是 ...

  6. win764位下mysql-5.6.24-x64从安装到登录成功

    1.安装 本人电脑win7,64位,需要安装mysql服务器.版本:mysql-5.6.24-x64.这里我用的是绿色版,免安装.由于免安装的原因,在服务里面并没有mysql的服务.这里我需要打开my ...

  7. tcp/ip协议之小解释

    [转载] http://www.ruanyifeng.com/blog/2009/03/tcp-ip_model.html

  8. css 让div 置于最顶层而不被其他东西挡住

    今天遇到自己写的div被其他东西给挡住了,需要设置一个属性就成功了 设置:z-index:值:比如 z-index:999. 若值设置为为-1,代表为最底层. div的图层由div的style中的z- ...

  9. 【DevExpress v17.2新功能预告】DevExtreme ASP.NET MVC新的强类型HTML Helpers

    在ASP.NET MVC中构建视图时,强类型HTML helpers非常有用.像@Html.TextBoxFor(m => m.FirstName)这样内置的Helper方法已经存在很长时间了, ...

  10. Windows下使用Nexus搭建pypi私服

    Nexus之前一直作为maven的私服而被大家所熟知,但是其实nexus可以做很多种仓库的私服,官网的说明就揭示了一切,真是又方便又强大的开源工具. 首先下载安装nexus,地址: https://w ...